LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

ActiveX-Server Parameter-Transfer Problem

Hello,
now, my ActiveX-Server is runnig, but I've other problems now.
I'd like to transfer variables from Excel to CVI and from CVI to Excel.
One way ist runnig without problems, Excel to CVI, but the other way makes still problems.

My ActiveX-Server-Function:

HRESULT CVIFUNC MyObjectMyInterfaceInput (CAServerObjHandle objHandle,
char ** msg)
{
* msg = "test123";
return 0;
}


My Visual Basic-Call:
Dim test
Set obj = New MyObject

test = 11
MsgBox (test)
test = MyObject.Input
MsgBox (test)

The first time the CVI-Function returns me "test123", but when I try to call it again, I get following error:

"Assignment of pointer to freed memo
ry"

Have anybody ideas? Or does anybody know whats my mistake?

Regards

Markus
0 Kudos
Message 1 of 8
(3,240 Views)
Hi Markus,

In ActiveX servers built in CVI, you should always allocate the memory for string and array output parameters using the CA_AllocMemory function. The caller will free this memory.

If you assign output string/array parameters to static memory or memory allocated using other functions like malloc, then you will get these kinds of errors. This is because the caller will free the memory using the ActiveX CA_FreeMemory function and so the memory will get trashed.

See the SimpleExe and DataTypes example servers that ship with CVI (installed in samples/activex/servers) for code that shows how to do this.

Best regards,
Mohan
Message 2 of 8
(3,240 Views)
Hi,
I've tried to solve my problem, but it doesn't work. The Examples helped me to understand it, but my program won't work.
I've changed in CVI following:

HRESULT CVIFUNC MyObjectMyInterfaceInput (CAServerObjHandle objHandle, char ** msg)
{
*msg = CA_AllocMemory (sizeof (char));
*msg = "test123";
return 0;
}

I hope someone can understand a beginner in CVI like me and help me 🙂

Regards Markus
0 Kudos
Message 3 of 8
(3,240 Views)
Hi Markus,

You are not allocating enough memory. You are only allocating memory for one byte. Also after allocating the memory you are just assigning the pointer to some other memory and thus leaking memory. In your case, the solution is as follows:

HRESULT CVIFUNC MyObjectMyInterfaceInput (CAServerObjHandle objHandle, char ** msg)
{
// Allocate memory for entire string
// Need extra byte for trailing '\0'
*msg = CA_AllocMemory (strlen("test123") + 1);
if (*msg == NULL)
return E_OUTOFMEMORY;

// Copy the string into allocated memory
strcpy(*msg, "test123");

return 0;
}

Hope the above helps,
Cheers,
Mohan
0 Kudos
Message 4 of 8
(3,240 Views)
Many Thanks, it works fine!!
But now, I've tried to solve an double parameter the same way, but I
get no solution.
Here is my program:

HRESULT CVIFUNC MyObjectMyInterfaceServer_Client (CAServerObjHandle
objHandle, double* msg)
{
double h;
h=666;
msg = CA_AllocMemory (sizeof(double));
if (msg == NULL)
return E_OUTOFMEMORY;
*msg = h;
return 0;
}

In Visual Basic I call following program:
Private Sub CommandButton1_Click()
Dim hilf As Variant
Set obj = New MyObject
hilf = MyObject.Server_Client
MsgBox (hilf)
End Sub

But the routine returns always an empty string, not a double value.

In the examples (SimpleExe or DataTypes) I only found solutions for
1D/2D array and strings/char, not for a standalone double.

Is there a po
ssibility to solve my problem on this way?

And I've another question too. How can I convert from String-->Int and
Int-->String?

I hope my questions are not too stupid. 😉

Markus
0 Kudos
Message 5 of 8
(3,240 Views)
Hi Markus,

You need to allocate memory only for output strings and arrays. For a
scalar double output parameter, you should not allocate memory. You
can just set the value in the pointer. For example, your code should
be:

HRESULT CVIFUNC MyObjectMyInterfaceServer_Client (CAServerObjHandle
objHandle, double* msg)
{
*msg = 666;
return 0;
}

Try the above and see if you get the correct value in your Visual
Basic client program. Also, the cvi sample program in
\samples\activex\servers\datatypes has example code for more
complicated datatypes.

Best regards,
Mohan
0 Kudos
Message 6 of 8
(3,240 Views)
Hi,
many thanks, it runs. But I have to declare my variable as "Static"
otherwise it won't work!
My problem is solved... I think there is no more I'll to know. Thanks
and regards

Markus
0 Kudos
Message 7 of 8
(3,240 Views)
Hi,
I've got a similar problem, which I can't solve on myself. But this time, I think it's not a problem with LabWindow, it's a "Visual Basic" Problem.
I've tried to transfer 2 parameter in one Funktion like here:

HRESULT CVIFUNC MyObjectMyInterfaceMyMethod (CAServerObjHandle objHandle,
unsigned char* return1,
unsigned char* return2)
{
*return1 = 11;
*return2 = 22;
return S_OK;
}

In The ActiveX-Server-Wizard I've declared them as Output.
My Visual Basic Call looks like this:

Set obj2 = New MyObject
MyObject.MyMethod value1, Value2

But I get following Error:

"Argumenttype ByRef incompatibly"

First, I've tried to solve it with 1D Arr
ays, but this way ist more difficult, and VB says to me, that there is a forbidden Interface (the varialbe).

I hope someone can help me and say to me where the problem is.

Regards

Markus
0 Kudos
Message 8 of 8
(3,240 Views)