LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Import Shared Library with custom C struct

Hi,

I am trying to import a Labwindows Dll into Labview but I have functions that use struct type define like this:

extern struct audioResult {
    double logBandPower;
    double logSignalToNoise;
    double logDistortion;
    char dbUnit[ 32 ];
    };                    /* provided either to pwmInput() or to vtofInput() and computed by fft() */

__declspec(dllimport) double analogInput( struct audioResult *audio, double frequency, double range );

How can I import the structure audioResult into something Labview would understand?

Thanks,
Vu
0 Kudos
Message 1 of 5
(4,634 Views)
Your going to want to create an array of U8's for the audioResult struct.  DBL's are 8 bytes, and you've got 32 bytes in your string.  Three DBL's + 1 32byte string = 56 bytes.  So initialize an array of U8 to have 56 elements.  Set your DLL call to "Adapt to Type" for your first argument.

The output from the call will be 56 bytes long.  If you take the first 8 bytes of the array, you can typecast it to a DBL, repeat this for the 2nd and 3rd DBL.  Take the remainder of the array, do a 1D-search for 0, which is your termination character.  Take the portion of the array up to the index before the 0 and use convert U8 array to string to get your string.
0 Kudos
Message 2 of 5
(4,618 Views)


@Matthew Kelton wrote:
Your going to want to create an array of U8's for the audioResult struct.  DBL's are 8 bytes, and you've got 32 bytes in your string.  Three DBL's + 1 32byte string = 56 bytes.  So initialize an array of U8 to have 56 elements.  Set your DLL call to "Adapt to Type" for your first argument.

The output from the call will be 56 bytes long.  If you take the first 8 bytes of the array, you can typecast it to a DBL, repeat this for the 2nd and 3rd DBL.  Take the remainder of the array, do a 1D-search for 0, which is your termination character.  Take the portion of the array up to the index before the 0 and use convert U8 array to string to get your string.


Unless you use LabVIEW 8.2 or better where you can select the endianess on the Unflatten from string function I think I would solve this as a cluster instead. Create a cluster that contains three double values and another cluster inside with 32 U8 integers. Pass this with Adapt to Type to the Call Library Node. No typecasting necessary anymore and no worries about byte swapping.

Rolf Kalbermatter

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 3 of 5
(4,584 Views)

Better clusters.

 

For example. For this structure (PassThru data packed):

 

typedef struct
{
unsigned long ProtocolID;
unsigned long RxStatus;
unsigned long TxFlags;
unsigned long Timestamp;
unsigned long DataSize;
unsigned long ExtraDataIndex;
unsigned char Data[4128];
} PASSTHRU_MSG;

 

the instrument is as attached.

 

Regards,.

 

 

 

 

0 Kudos
Message 4 of 5
(2,959 Views)

That's not gonna work well. LabVIEW doesn't like clusters with more than 512 elements so your 4000 something embedded array is not going to be easily created like that. In this case you have to pass an array of enough elements to the Call Library Node and configure the parameter to be passed as Array Data Pointer, then fill in any input data needed into that array and retrieve the output data after the call.

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