LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Access element of an array within a cluster from a DLL

Solved!
Go to solution

I have a labview model and I am trying to add some functions using a DLL.

 

There are a lot of nested clusters, but I can access everything OK by using structures in my C code.  I am struggling however to access elements of an array that is within a cluster.

 

I created a Call Library Function Node block and added the appropriate signals and clusters, and then created a C file from it to see how labview would generate the typedefs.  I see where it has the table defined, but I can't figure out the proper syntax to access an element of the array.  Generated code is below... Any help you can provide is appreciated. Everything I have tried crashes LabView.

 

typedef struct {
	int32_t dimSize;
	double Numeric[1];
	} TD4;
typedef TD4 **TD4Hdl;

typedef struct {
	TD4Hdl aCoefficient;
	TD4Hdl bCoefficient;
	TD4Hdl table;
	} TD11;

double calcHarmonComp(TD11 angHarmon, double angle, double tableSize, bool isHarmonics );

I tried accessing it with
correction = (**angHarmon.table).Numeric[0];

but this crashes LabView
0 Kudos
Message 1 of 2
(2,068 Views)
Solution
Accepted by topic author DSabol

@DSabol wrote:

I have a labview model and I am trying to add some functions using a DLL.

 

There are a lot of nested clusters, but I can access everything OK by using structures in my C code.  I am struggling however to access elements of an array that is within a cluster.

 

I created a Call Library Function Node block and added the appropriate signals and clusters, and then created a C file from it to see how labview would generate the typedefs.  I see where it has the table defined, but I can't figure out the proper syntax to access an element of the array.  Generated code is below... Any help you can provide is appreciated. Everything I have tried crashes LabView.

 

typedef struct {
	int32_t dimSize;
	double Numeric[1];
	} TD4;
typedef TD4 **TD4Hdl;

typedef struct {
	TD4Hdl aCoefficient;
	TD4Hdl bCoefficient;
	TD4Hdl table;
	} TD11;

double calcHarmonComp(TD11 angHarmon, double angle, double tableSize, bool isHarmonics );

I tried accessing it with
correction = (**angHarmon.table).Numeric[0];

but this crashes LabView

First you need to pass the first parameter as reference. LabVIEW does not allow you to pass clusters by value ever!

 

Have you made sure to pass in a non-empty array. The element

(**angHarmon->table).Numeric[0]

or as I prefer to write:

(*(angHarmon->table))->Numeric[0]

only referes to a valid memory location if the array contains at least one element. Passing in a default cluster will pass in a struct with three NULL pointers for the arrays.

 

You are programming here in C not in LabVIEW anymore. That means that you have to make sure yourself that an array is properly allocated before trying to access it. In addition LabVIEW uses NULL handles to represent empty arrays.

 

So your code should at least do something like this before trying to access an element:

 

double calcHarmonComp(TD11 *angHarmon, double angle, double tableSize, bool isHarmonics )
{
    if (angHarmon->table && (*(angHarmon->table))->dimSize >= 1)
correction = (*(angHarmon->table))->Numeric[0];
else
argument error!!
}

 

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