LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

struct as parameter in DLL called from Viusal Basic

Is it possible to use a struct as a parameter  in a dll function called from Visual Basic?  The function panel facility used to generate a type library in CVI doesn't seem to allow the use of structs, either by value or by reference.

 

The "type" type in VB would seem to correspond to a C struct.   Maybe the packing issues were to complicated to allow the use of this aggrgate type in the type library.

 

Thanks,

 

Menchar

0 Kudos
Message 1 of 6
(3,400 Views)
just to clarify: the DLL is in C and the client application in VB ? which version of VB are you using ?
0 Kudos
Message 2 of 6
(3,399 Views)

Yup, CVI dll, VB 6.0.

 

I don't think it can be done, VB docs say user defined types, while struct-like, are not interoperable with other systems, or even .NET

 

What's a bit interesting is that CVI doesn't appear to provide for the use of structs as parameters in the function panel, even for a normal, all-C implementation.

 

 

Message Edited by menchar on 10-20-2008 05:48 PM
0 Kudos
Message 3 of 6
(3,393 Views)

i remember it can be done, however microsoft did not like it. that 's the way we used to call SDK functions to supplement features not provided by VB.

it was something a bit like that (example provided without any warranty, i don't have VB6 anymore to test it)

 

VB:

Type composed

    Dim field1 As Long

    Dim field2 as Integer

EndType

 

Declare Function test Lib "test.dll" ( ByRef param As composed ) Return Long

 

Sub call()

    Dim c As composed

    Dim result As Long

 

    c.field1 = 42

    c.field2 = 7

 

    result = test( c )

EndSub

 

C:

 

typedef struct

{

    int field1;

    short field2;

} composed;

 

int test(  composed *param );

 

 

this is straight out of my mind. it may not work as is, as i can't verify it...

 

there was some pitfalls: VB integer type is 16 bits, strings are unicode, arrays are accessed as a SAFEARRAY in C (difficult to use). strings and arrays are to be avoided as they are very heavy in VB. there was a keyword like AddressOf to get a pointer to almost anythin. a function parameter in a declare statement can be declared as type "Any" which is the equivalent of C's "void". a special syntax of the Declare statement that i don't remember allowed to specify the real function name in the DLL to take care of name decorations (typically, the function above would be named "_test@4"). there are many many more things to say about that... the best way to find out is to open the API Viewer, which allows you to import Windows SDK functions into VB, see how things are declared there and match it with the C declaration in the SDK.

 

if you encounter some problem, tell them, i may be able to tell you what's wrong...

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

Hello Menchar,

 

To use structs/unions/enums in an .fp file (Type Library), they must be typedef'ed in order to add them to the .fp file. Please take a look at the below linked KB on how to add an enum to an .fp file. It works the same for a struct. I've done it.

 

http://digital.ni.com/public.nsf/allkb/226759075AE83ADF862571E0006D2DB3?OpenDocument 

 

 

Manooch H.
National Instruments
0 Kudos
Message 5 of 6
(3,363 Views)

Thanks both of you for the feedback.

 

I'm a bit averse to digging that far under the hood to get the thing to work - sounds like you really got into it to make the VB user defined type work for you.  I'd be afraid of depending upon implementation detail that MS could change on a whim and then having everything break.

 

I normally typedef structs, so no problem there.

 

I wonder what CVI would do then, if you told it to make a type library out of a function panel that had a struct as a parameter?

 

Menchar

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