LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Strings passed to / from VB

Has anyone successfully passed strings (pointer to an array of chars) between a CVI dll and a VB application?

 

We have a dll written with CVI that we're trying to use from VB - we added a type library, and all functions work with the apparent exception of those functions that are trying to pass a string in VB back to dll code written with CVI.   It seems to us that strings originating in the dll are successfully passed into VB, but not the other way around.

 

Shouldn't the type library fix it up in both directions?  

 

Thanks for any help with this.

 

Menchar

0 Kudos
Message 1 of 6
(3,865 Views)

Hello menchar,

 

What type do you use in your VB code to represent a pointer to an array of characters?  Do you use an array of strings, a two-dimensional array of characters, etc.?  Thanks in advance!

0 Kudos
Message 2 of 6
(3,840 Views)

Matt -

 

Thanks for the response.

 

I mis-stated the probelm - strings originating in VB are passed successfully into the DLL as "in" parameters to the DLL function, but strings declared in the

VB environement and passed by reference to the DLL as a destination for data being returned from the dll to VB (an "out" parameter) do not work.

 

We declare the VB array using the String data type:      MyStuff As String

 

Someone thought that blank-filling the VB array might be needed.

 

Or declaring the VB string to be of fixed length AND blank filling it.

 

MyStuff As String * 80

space (MyStuff, 80)

 

Menchar

Message Edited by menchar on 12-04-2008 02:27 PM
0 Kudos
Message 3 of 6
(3,834 Views)

how do you declare the string on the C side ? which version of VB are you using ? (i seem to remember you are using VB6...)

can you give us the relevant portion of the type library ?

 

(strings in COM objects are a delicate subject: it's one of my worst nightmare from the days i was writing ActiveX controls in C++ for VB6 users)

0 Kudos
Message 4 of 6
(3,819 Views)

thinking more about this: isn't VB using the BSTR type when passing a String to a DLL ? if this is the case, you need to use the SysAllocString() SDK function on the C side to be able to share the allocated memory between your DLL and VB.

 

(SysAllocStr() is part of OLE Automation. it is declared in oleauto.h, link with oleaut32.lib)

0 Kudos
Message 5 of 6
(3,794 Views)

Thanks for the responses.

 

We think we've solved it.  We pass the string name by value from VB to the C function in the DLL.  And we make sure the string has been allocated by blankfilling it first.

 

We think the VB string names are references  to the first char in the string (as in C) but unlike C, VB strings are dynamically allocated on first reference at runtime.  Since the C function was making the first reference to the string, no call forcing allocation had been made.  By blank filling the string first, allocation is forced before the C function is invoked.  

 

And we pass the string name by value since it is already a reference to the first element of the string.

 

In C, there are no "pass by reference" parameters - all parameters are pass by value, it's just that sometimes the value happens to be a reference 🙂

 

Menchar

0 Kudos
Message 6 of 6
(3,779 Views)