LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

DLL custom data type

Solved!
Go to solution

I am trying to use a DLL in LabView which talks to a frame grabber. One of functions requires a custom data type (MID2250_MDConfig_t) that LabView does not support by default. It is defined as follows in the C header file:

 

typedef struct
{
    int CoordsX;
    int CoordsY;
} MID2250_PointCorrds_t;

 

typedef struct
{
    MID2250_PointCorrds_t ULPoint[ 4 ];
    MID2250_PointCorrds_t BRPoint[ 4 ];
    unsigned short u32SADThresholdValues[ 4 ];
    unsigned short u32MVThresholdValues[ 4 ];
    unsigned short u32SensitivityValues[ 4 ];
} MID2250_MDConfig_t;

 

Is there a way I can incorporate this data type in LabView properly. I have seen people talk about wrapper DLLs on this forum but I'm a bit confused about that. Can I just create a similar cluster in LabView and pass it to the function using "Call Library Node"?

 

0 Kudos
Message 1 of 26
(4,881 Views)
Solution
Accepted by abdel2

 


@abdel2 wrote:

I am trying to use a DLL in LabView which talks to a frame grabber. One of functions requires a custom data type (MID2250_MDConfig_t) that LabView does not support by default. It is defined as follows in the C header file:

 

typedef struct
{
    int CoordsX;
    int CoordsY;
} MID2250_PointCorrds_t;

 

typedef struct
{
    MID2250_PointCorrds_t ULPoint[ 4 ];
    MID2250_PointCorrds_t BRPoint[ 4 ];
    unsigned short u32SADThresholdValues[ 4 ];
    unsigned short u32MVThresholdValues[ 4 ];
    unsigned short u32SensitivityValues[ 4 ];
} MID2250_MDConfig_t;

 

Is there a way I can incorporate this data type in LabView properly. I have seen people talk about wrapper DLLs on this forum but I'm a bit confused about that. Can I just create a similar cluster in LabView and pass it to the function using "Call Library Node"?

 


 

Since the arrays are all fixed sized (and not huge) they are really inlined in the structure. This means you can simulate them by a cluster containing as many elements in it as there are array elements. So the first UL Point would be a cluster containing 4 cluster with each two int32 in it. Same for the second element. The third is a cluster with 4 uInt32 and so on.

 

Then configure the parameter as adapt to type and wire this cluster wire to it, et voila.

Rolf Kalbermatter
My Blog
0 Kudos
Message 2 of 26
(4,870 Views)

Thank you! That worked very nicely. Now I'm also having an issue with calling a different function. I believe I have the data types correct but still don't see the correct result (no memory related exceptions or crashes from LabView though). I am currently side tracked by something else but I'll post about it as soon as I get back to movement detection.

 

0 Kudos
Message 3 of 26
(4,809 Views)

Hi Rolf,

 

I came across this thread now and it is basically solving the exact problem I currently have - except with a different data type: I would like to call a DLL generated from C code generated by Matlab, of which one of the parameters in one function is a user-defined type variable (emxArray_real_T):

 

struct emxArray_real_T
{
double *data;
int *size;
int allocatedSize;
int numDimensions;
boolean_T canFreeData;
};

 

I tried using a cluster of clusters with the same primitive types as you suggest, but it just doesn't work. I tried

1) a cluster of array of doubles, 

2) a cluster of array of int32_t, 

3) a cluster of int32_t, 

4) a cluster of uint8_t, 

 

Is there anything too obviously wrong that I am doing here? Would you be able to shed some light? 🙂

Thanks Rolf,

 

Edgar 

 

 

 

0 Kudos
Message 4 of 26
(4,619 Views)

Are you familiar with C pointers? If not, you need to understand them before you can continue. If you do understand pointers in C, then search this forum for threads about pointers in clusters or structs. Several people have asked similar questions.

 

Your structure is not analogous to the one in the original post: the original message describes a struct continaining fixed-size arrays, where your struct contains a pointer to a variable-sized array. These two situations are handled completely differently. In the fixed-size array case, the array is inline with the structure (that is, the memory for the array is part of the memory for the struct). In the variable-size array case, the struct contains only a pointer (a memory address) containing the actual location of the array.

 

The two ways to handle this are:

1) Create a wrapper DLL that translates to data types that LabVIEW can handle easily

2) Create a cluster in LabVIEW containing a numeric element that is actually the memory address, and use the LabVIEW memory manager functions to copy the values into or out of that address. This can get tricky if your code needs to run on both 32- and 64-bit platforms.

0 Kudos
Message 5 of 26
(4,603 Views)

Also, since this thread is three years old, already marked solved, and not really the same as your question, you may have better luck starting a new thread, or continuing an open one that more closely resembles your question. Many people won't look at threads marked solved.

0 Kudos
Message 6 of 26
(4,601 Views)

Hi nathand,

Thanks for your reply. Will give it a go. If unsuccessful, will create a new thread.

Cheers,

E

0 Kudos
Message 7 of 26
(4,579 Views)

Hi nathand,

this just doesn't work - i've continued this thread as I saw that you solved many issues with people trying to access DLLs with user-defined type - so maybe you're able to help me here if possible.  I've attached a png with a description of the VI that is calling the DLL

My "output cluster" variable is exactly as the emxArray_real_T:

 

struct emxArray_real_T
{
double *data;
int *size;
int allocatedSize;
int numDimensions;
boolean_T canFreeData;
};

 

The "global_locs" struct is supposed to return me some peak locations in a signal that I'm reading from a CSV file.

 

I've tried using the Memory manager functions to create a pointer for *data and  *size, but the call_library_node always crashes - although it returns at least data on the FD_avged array. Tried changing the datatypes to U

Anything obvious of what am I doing wrong???

 

Thanks very much,

Edgar

 

0 Kudos
Message 8 of 26
(4,543 Views)

A pointer variable "char *somename" is absolutely not the same as a fixed size array "char somename[n]"!

 

The first can't be easily implemented in LabVIEW since the LabVIEW strings and arrays are very different in memory from those C string and array pointers. The easiest way to deal with that is writing an intermediate wrapper DLL in C which translates between the LabVIEW datatype and the C structure.

 

The other solution Nathan has at times hinted at is way complexer and basically a nightmare to maintain. And, if you are unable to write a DLL in C, you have simply no chance to get this method right, and with right I mean not a solution that just doesn't crash, but one which does not even silently corrupt memory in the background!

Rolf Kalbermatter
My Blog
0 Kudos
Message 9 of 26
(4,531 Views)

I am not completely convinced that a wrapper DLL is necessary, but Rolf does have substantially more experience in this area than me. However, I think Rolf has a good point that if you do not know enough C to be able to build a wrapper DLL, then you are unlikely to get this right in LabVIEW.

 

We would need more documentation about the function you are calling in order to help. For example, even though the structure defines both data and size to be pointers, most likely data is a pointer to an array but size is a pointer to a single element. If that's the case, and if size is the size of the array, then you need to know if it's the number of elements or the number of bytes. Initializing them both to point to blocks of 1024 bytes seems random, and storing a pointer in a floating point cluster element (the data element) is wrong.

 

If you can provide documentation for the function, the header file (.h) containing the function prototype and definitions of any relevant structures, and your VI (code, not a screenshot) then we might be able to fix it.

0 Kudos
Message 10 of 26
(4,511 Views)