From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Labview created DLL with 2D array

Solved!
Go to solution

I've created a DLL from a Labview VI (2014), and I am trying to call it from Labwindows CVI (2013). The DLL has one function call that takes a 2D array.  When Labview created the DLL, the header has special function calls to allocate/resize/deallocate memory for the array.  The Array type is a struct, with pointers to the size of each dimension, and a pointer to the element data.  

 

The issue is when I make the call to the Allocate routine, it does not pass back a handle.  I see no way to get an error return.  I've searched forums and found that I can use a different method to allocate the memory.  When using the second method, the I can properly call the Labview function and get the correct return.  However, on the second call the DLL crashes.  It seems that there is some memory sharing violation in the DLL, so I'm thinking that there is a step missing in the second method. 

 

Does anyone have any experience with calling a Labview DLL that takes in a 2D array?

 

Working method:

 
hROI = (Uint8ArrayBase**) DSNewHandle (sizeof (Uint8ArrayBase));
(*hROI)->dimSizes[0] = 60;
(*hROI)->dimSizes[1] = 30;
(*hROI)->elt[0] = 0;

 

Non-working method:

Uint8Array hROI;
int32 size=2;

hROI = AllocateUint8Array (&size) ;

// hROI always returns 0x000000 in this method.

 

0 Kudos
Message 1 of 2
(2,766 Views)
Solution
Accepted by topic author bjollies

Your doing this pretty wrong!

 

hROI = (Uint8ArrayBase**) DSNewHandle (sizeof (Uint8ArrayBase));
(*hROI)->dimSizes[0] = 60;
(*hROI)->dimSizes[1] = 30; 
(*hROI)->elt[0] = 0;

This only allocates a memory area that can hold the dimSizes header and a single data element. Don't try to write to (*hROI)->elt[1] ever or you are going to corrupt memory, and if you are lucky it will crash. If you have bad luck nothing seemingly bad will happen but you have destroyed memory contents belonging to something else in LabVIEW, by writing over the end of the allocated buffer.

 

Instead you need to do this:

int32 dimSizeArr[2];
dimSizeArr[0] = 60;
dimSizeArr[1] = 30;

Uint8Array array = AllocateUint8Array(dimSizeArr);
if (!array)
      err = mFullErr;
else
{
     // do whatever you need to do with the array
     err = DeallocateUint8Array(&array);
}

However it is likely that your function actually creates this array! In that case you would not need to first create it. It really depends if your function somehow initializes this array or simple just references it.

 

If you happen to have two loops in there, with both autoindexing array output tunnels that get returned into this array parameter at the end you can actually do something like this:

 

Uint8Array array = NULL;
double retval = SlantedEdgeSFR4(frequency, y, x, &array);
if (array)
{
// do something with the data in the array
for (i = 0; i < (*array)->dimsSizes[0]; i++)
{
for (j = 0; j < (*array)->dimSizes[1]; j++)
{
(*array)->elm[(*array)->dimSizes[1] * i + j] == something;
}
}
err = DeallocateUint8Array(&array);
}

In case you have already a dizzy head from all the pointers, handles, array subscripts and more, now you know why you prefer to program in LabVIEW rather than in C! Smiley Very Happy

 

And just to show you what the functions most likely look like internally:

Uint8Array AllocateUint8Array(int32 *dimSizeArr)
{
    return (Uint8Array)DSNewHClr(sizeof(int32) * 2 + dimSizeArr[0] * dimSizeArr[1];
}

MgErr __cdecl ResizeUint8Array(Uint8Array *hdlPtr, int32 *dimSizeArr)
{
    return = NumericArrayResize(uB, 2, (UHandle*)hdlPtr, dimSizeArr[0] * dimSizeArr[1];
}

MgErr __cdecl DeAllocateUint8Array(Uint8Array *hdlPtr)
{
    if (hdlPtr)
{
MgErr err = noErr;
if (*hdlPtr) {
err = DSDisposeHandle((UHandle)*hdlPtr);
*hdlPtr = NULL;
}
return err;
}
return mgArgErr;
}
Rolf Kalbermatter
My Blog
Message 2 of 2
(2,743 Views)