LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I pass a pointer to an array of structs to a DLL function?

Thanks again Rolfk for your prompt response.

 

I'm actually not interested in porting my code to LV64-bit later on, so no need to worry about struct sizes, etc. And also my code doesn't use any "fancy" pointers or dynamic structures that might show incompatibility between LV32 & LV64. It's pretty simple I would say. I did read about the memory alignment that LV32 uses (1 byte) vs. C which uses 8byte.

 

So anyway, I have added the lv_prolog.h and lv_epilog.h header files between my struct and it all seemed to work fine at first, but when I tried accessing a fixed int array inside my struct, LV crashes.

 

Here is my DLL code, again very simple.

 

 

#include "extcode.h"
static int example;

#include "lv_prolog.h"
struct channel
{
    int32 a;
    double x;
    double y;
    int32 c[3]; //this is the array that makes LV crash. 
};
#include "lv_epilog.h"

_declspec(dllexport) void CLUSTERSimple(struct channel *input);
_declspec(dllexport) void CLUSTERSimple2(struct channel *input);

_declspec(dllexport) void CLUSTERSimple(struct channel *input)
{
    example = input->a;
    input->x += 1;
    input->y += 1;
}
_declspec(dllexport) void CLUSTERSimple2(struct channel *input)
{
    input->a = example * example;
    input->c[0] = 1; //I believe here is where it crashes.
    input->c[1] = 2;
    input->c[2] = 3;
}

And the VI looks the exact same as the previous one, except I modified the cluster to match this new struct:

 

ScreenHunter_234 Feb. 16 04.53.jpg

ScreenHunter_235 Feb. 16 04.53.jpg

Using "Adapt to Type" and "Handles by Value". I32 -> int32, DBL -> double.

 

I don't know what's the error since LV closes immediately and doesn't show anything. By the way, I tried updating to LV2013 SP-1 and f2 patch or something like that but it didn't make a difference. Would you recommend to try this in LV2015 maybe? It's really not something very complicated what I'm trying to do, I just don't know what's going on.

 

Thanks again. Cheers!

0 Kudos
Message 21 of 22
(630 Views)

Fixed size arrays inside a structure are inlined by the C compiler. That means they are in LabVIEW equivalent to a cluster inside the cluster with as many elements as the fixed size array contains, of the type the array element is. In Addition even if there was a pointer in the struct that is NOT the same as a LabVIEW array or string. LabVIEW variable sized elements like arrays and strings are handles, that is a pointer to a pointer and only the LabVIEW manager functions can be used to create, resize and destroy them. A C function expeting there to be a char * element will go completely awry when trying to read (or even worse write) into that.

 

But comments like "I tried updating to LV2013 SP-1 and f2 patch or something like that but it didn't make a difference." in an attempt to fix this, really show that you are treading in way to deep water for your own good. Smiley Wink

Rolf Kalbermatter
My Blog
Message 22 of 22
(625 Views)