05-03-2016 05:40 AM
Hello,
I have difficulty in handling an array pointer from a DLL function.
The format of the function is:
DECLSPEC int_cdecl FUNCTIONNAME(int P1, unsigned short *Data, unsigned int P2, unsigned int P3)
I have no problem with the input P1, P2, and P3. However, I am not sure how to handle the output *Data. In the C header, Data is defined as:
unsigned short* Data,
which is a 2D array. The function is for getting a 2D image.
I used the LabView to automatically convert the dll to User Library, and the "Data" shown in the Call Library Function Window as:
Name: Data
Type: Numberic
Data type: Unsigned 16-bit integer
Pass: Pointer to Value
-------------------------------------------------------------------------
It seems to me that LabView expect the output as a "pointer" which points to an array. Therefore, I guess there should be an array initialization at first because calling this function. However, I am not sure how to link this output pointer to the array.
Would someone be kind enough to answer my question and show me some example?
Thanks a lot!
05-03-2016 07:31 AM - edited 05-03-2016 07:33 AM
@jackjern wrote:
Name: Data
Type: Numberic -> Array
Data type: Unsigned 16-bit integer -> unsigned 32 bit integer
Dimensions: 1 (maybe 2 if it is really a 2D array as you claim.
Pass: Pointer to Value -> array data pointer
Then you need to place an Initialize Array node on your diagram and initialize the array to the size as your function will need and wire the resulting array to the left side of this Call Library Node parameter. the right side of this parameter will return the array as it was filled in by the function.
05-03-2016 08:50 AM
If it is a single pointer (*Data, not **Data), then does it not return one dimension array?
Then you will need to initialize it as size_hor x size_vert number of elements and use reshape array.vi in labview to turn it into 2D.
Help -> Find examples -> Call DLL.vi (lv2011) for examples of calling dlls with different data types.
05-03-2016 09:26 AM - edited 05-03-2016 09:28 AM
@Alexander_Sobolev wrote:If it is a single pointer (*Data, not **Data), then does it not return one dimension array?
Then you will need to initialize it as size_hor x size_vert number of elements and use reshape array.vi in labview to turn it into 2D.
Help -> Find examples -> Call DLL.vi (lv2011) for examples of calling dlls with different data types.
Remember C is only a tiny layer above assembly programming! A pointer in C is simply a pointer, its datatype CAN indicate something to the programmer and if used properly does allow the C compiler to do SOME datatype safety checks.
But to take this specific example: There is absolutely no standard defined by C how to define arrays and especially multidimensional arrays.
A 2D array could be an array of pointers to 1D arrays but more often is simply a single memory block with x * y data elements, so looks syntactically exactly the same as an 1D array buffer.
Also your doubly referenced pointer does really not tell anything about the underlaying memory organization. It could be an array of pointers, but it could be a reference to a single pointer and this single pointer could be an array or simply another reference to a single skalar. Nothing in the C syntax is designed to standaradize these things in any way and it is up to the implementer to decide what he wants to use and what this particular syntax really means in his software.
LabVIEW arrays are actually also all implemented as a single block of memory, totally independent of the number of dimensions. The inly differenc is the number of size integers that the LabVIEW handle contains in front of the actual data memory block (one size integer for every dimension). The data block itself is then the multiplication of all size integer number of array elements.
05-07-2016 05:13 PM
The above posters are absolutely correct, you need to pass a pointer to C with pre-allocation as otherwise you will have memory violations and LabVIEW will crash.
This document should provide further clarity as to your options: