LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Passing 3D array to LabVIEW via Call Library Function Node

Solved!
Go to solution

Hi everyone,

I try to write a program in LabVIEW that retrieves data from a c dll function in form of a 3-D array.

In Detail (see attached file):

The dll function "getrawdata" produces the data I want to process. In my real project I don't have access to that function.

It is called by the dll function "handletest" or "matrixtest" where the data is processed. In LabVIEW I want to call the "handletest" or "matrixtest" function via a call library function node and visualize the processed data. But i can't figure out how to manage the data passing from the DLL to LabVIEW. In my real project there will be very many data points. The program should be able to handle at least 2'000'000 data points.

I studied the NI-example VI "External Code (DLL) Execution" and tried to copy the programm structure.

The function "handletest" is made in the prgramm structre of the example "3D-Array-Handle" of the mentioned example VI and "matrixtest" is made in the programm structure of the example "3-D Array".

However, in my project it is not possible to allocate memory by connecting an input array with the correct size to the call library function node. So I used the memory managmant functions as mentioned in the example VI.

But when I use these functions (DSNewPtr or DSNewHandle) inside the "matrixtest" function I keep getting access violation errors and LabVIEW crashes (actually sometimes the VI finishes without any errors when  "cols" and "lines" are small).

When I call "handletest" with LabVIEW, the output array does not contain any data.

 

I use LabVIEW 2015 on a Windows 7 32 Bit PC.

I hope I could clearly explain my problems and you can help me getting this to work!

Thank you!

0 Kudos
Message 1 of 3
(2,249 Views)
Solution
Accepted by topic author partl

You're making a dead sin by blindly trying to manage LabVIEW data handles that you shouldn't do like this!!

 

Int32 DLL_EXPORT handletest(Int32 cols, Int32 lines, TD1Hdl matrix, Int32 mar[100]/*This array is to check some function-intern values if necessary*/)
{
    Int32 rc = 0;                               // return code
    Int32 i = 0;
    Int32 j = 0;
    Int32 k = 0;
    Int32* data;                                //Declare the pointer for the raw data
    //matrix = NULL;
    matrix = (TD1Hdl) DSNewHandle(sizeof(TD1Hdl) + 2 * lines * cols * sizeof((*matrix)->output[0]));    //allocate memory for the processed data and the dimension sizes
    if(matrix == NULL)                          //check if memory allocation was successful
    {
        rc = -14;
        return -14;
    }

In this code segment you pass an array handle from LabVIEW, which because it is not passed by reference, will ALWAYS be an allocated array handle from the LabVIEW diagram. Then you simply overwrite it blindly with a new handle you create and LabVIEW won't  be able to see that changed handle since it was passed by value, but the handle you created in the function will be lost after the function returns to the caller and you leak its memory that way.

 

If you want to keep this code you need to replace the DSNewHandle() call with a DSResizeHandle() call. Cleaner would be following approach:

 

MgErr DLL_EXPORT handletest(Int32 cols, Int32 lines, TD1Hdl *matrix, Int32 mar[100]/*This array is to check some function-intern values if necessary*/)
{
    MgErr err = 0;                               // return code
    Int32 i = 0;
    Int32 j = 0;
    Int32 k = 0;
    Int32* data;                                //Declare the pointer for the raw data

    err = NumericArrayResize(iL, 3, (UHandle*)matrix, 2 * lines * cols);  // resize memory for the processed data and the dimension sizes
    if (err)                         //check if memory allocation/resize was successful
    {
        return err;
    }
    ...
}

And of course you need to change the Call Library Node configuration to pass this as Array Handle Pointer instead of Array Handle.

Rolf Kalbermatter
My Blog
Message 2 of 3
(2,236 Views)

Hi rolfk,

thanks a lot for your reply and explanation.

Exactly this was the problem and your suggested code works very well!

0 Kudos
Message 3 of 3
(2,224 Views)