LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Designing a specific DLL Prototype in LabVIEW


I have seen similar posts around here, but nothing specifc enough to my situation.
I need some help creating a LabVIEW DLL to fit a particular prototype (which is being called by another program).

The (required) prototype:
int InitFromVifBom (DWORD nCode, void *p)

DWORD is simply "typedef unsigned long       DWORD;"

and void *p is later defined as:
pInit = (INIT_VIFDLL *) p;

where INIT_VIFDLL is a structure defined as:
typedef struct  {
  HINSTANCE      VifBomDLL;          // Handle of VifBom.DLL
  unsigned char   Res[128];          // reserved for future use.
} INIT_VIFDLL;

...OK,  if you're still with me, I will describe what I have tried.

I have managed to generate a LV DLL with a prototype:
long __stdcall InitFromVifBom(unsigned long nCode, TD1 *p);

My main question is concerning the pointer 'p'.  It is currently a cluster of an I32 value, and an array of 128 chars. (as per the above code).

This is crashing the program which calls my DLL, and I am guessing that it is my fault (this is likely a safe bet.)

Any suggestions?  Thanks in advance.


0 Kudos
Message 1 of 2
(2,413 Views)


@Pjama wrote:

I have seen similar posts around here, but nothing specifc enough to my situation.
I need some help creating a LabVIEW DLL to fit a particular prototype (which is being called by another program).

The (required) prototype:
int InitFromVifBom (DWORD nCode, void *p)

DWORD is simply "typedef unsigned long       DWORD;"

and void *p is later defined as:
pInit = (INIT_VIFDLL *) p;

where INIT_VIFDLL is a structure defined as:
typedef struct  {
  HINSTANCE      VifBomDLL;          // Handle of VifBom.DLL
  unsigned char   Res[128];          // reserved for future use.
} INIT_VIFDLL;

...OK,  if you're still with me, I will describe what I have tried.

I have managed to generate a LV DLL with a prototype:
long __stdcall InitFromVifBom(unsigned long nCode, TD1 *p);

My main question is concerning the pointer 'p'.  It is currently a cluster of an I32 value, and an array of 128 chars. (as per the above code).

This is crashing the program which calls my DLL, and I am guessing that it is my fault (this is likely a safe bet.)


Yes, it is a safe bet indeed 😉

Your problem is that you assume that an "unsigned char res[128]" element inside a structure is a pointer. Nothing is further from the truth than this.
Any fixed size element in a structure is always inlined by every C compiler I know of. And a string with explicit number of elements is fixed size.

And your other problem is that you assume that using a LabVIEW string inside a cluster will create a C string pointer in that cluster. A LabVIEW string is a pointer to a pointer to memory where an int32 at the begining tells you how many characters are following. This is nothing a C compiler could deal with without calling specific LabVIEW datatype support functions. But since the string is not a pointer but a fixed size element here, this does not bother us in this case.

So what you want to do is really create an array of 4 + 128 byte and pass this to the DLL as a C array pointer. Or in your case configure such a beast as function parameter. Then in your LabVIEW VI you will have to seperate the bytes into the respective elements. And since you are probably going to typecast at least the handle back into an uInt32 you should also account for the Endianees swapping of LabVIEW by adding Swap Bytes and Swap Words after the Typecasting or in LabVIEW 8 select Little Endian format at the Typecast function.

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
Message 2 of 2
(2,376 Views)