LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I call a DLL function that needs an initialized array of structures that can be written to by the DLL?

Solved!
Go to solution

I need to call the following function in a C DLL:

 

__declspec(dllexport) int MSIL_EnumSpectrometers(SPECTROMETER_ID* Spectrometers);

 

where the single parameter is a pointer to:

 

typedef struct
{
 char Model[32];
 char Serial[32];
 int Channel;
} SPECTROMETER_ID;


Prior to LabVIEW 2011 I would have use a CIN allocate the memory and call the function, but in LabVIEW 2011, the CIN has been removed and I have to use a Call Library Function Node instead. When I go to define the parameter, it looks like I need to set the type to "Adapt to Type" and the Data Format to "Array Data Pointer", then pass in an initialized array of SPECTROMETER_ID with the proper number of elements in it so the DLL can populate the array with the information. (See the attached picture.) When I do this however, it crashes LabVIEW to the desktop.  I assume it is because the DLL is corrupting the LabVIEW memory space but I do not see why since I am basically passing in a void pointer to an initialized array of the correct size. Note that the first CLF Node returns the number of elements required in the array.  Anyone have a clue what I am doing wrong here?

0 Kudos
Message 1 of 3
(2,338 Views)
Solution
Accepted by topic author MrSmith

In LabVIEW, when an array is in a cluster - the cluster won't actually hold the array but some kind of pointer pointing to the array. If you need the cluster to hold 32 elements you have to add 32 elements to the cluster. See the attached VI for how it's supposed to be done.

Download All
Message 2 of 3
(2,326 Views)

Brilliant, that worked perfectly, thank you very much! I will remember that about clusters not holding the actual array but a pointer to it instead.  I did not realize that was the case.

0 Kudos
Message 3 of 3
(2,318 Views)