LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I pass a complex data array into a C dll

I can pass a double array into a dll created using C simply by passing a handle to the array (i.e. the memory location of the first element in the array).

However I need to do this with complex data array. However Labview does not allow me to select this data type, so I select adapt to type. This is fine for scalars as it merely passes a pointer as above. For arrays however, it decides to pass a handle, or worse still a pointer to a handle. As complex arrays are just arrays of doubles, twice as long (e.g. cmpx0, cmplx1, complx2....complxN-1 is simply real0, imag0, real1, imag1...realN-1, imagN-1). So how do I pass this complex array into a dll written in C.

I need to be able to do this without copying any of the data - I want access to the memory where this data recides.

Any help would be apreciated.
0 Kudos
Message 1 of 5
(4,250 Views)

Wybird wrote: I can pass a double array into a dll created using C simply by passing a handle to the array (i.e. the memory location of the first element in the array). However I need to do this with complex data array. However Labview does not allow me to select this data type, so I select adapt to type. This is fine for scalars as it merely passes a pointer as above. For arrays however, it decides to pass a handle, or worse still a pointer to a handle. As complex arrays are just arrays of doubles, twice as long (e.g. cmpx0, cmplx1, complx2....complxN-1 is simply real0, imag0, real1, imag1...realN-1, imagN-1). So how do I pass this complex array into a dll written in C. I need to be able to do this without copying any of the data - I want access to the memory where this data recides. Any help would be apreciated.
If you have the source code and can modify the DLL the most simple solution is to adapt it to handle the Adapted LabVIEW datatype. It will be something like this:
typedef struct {
    double re;
    double im;
} Complex64;

typedef struct {
    int32 size;
    Complex64 cmplx[0];
} ComplexArr, **ComplexHdl;

int32 MyComplexFunction(ComplexHdl arr)
{
    int32 size = (*arr)->size;
    Complex cmplx = (*arr)->cmplx;
    double tmp;

    for (i = 0; i < size; i++, cmplx++)
    {
       tmp = cmplx.re + cmplx.im; 
    }
    return 0;
}

As long as you don't need to resize the array this will be all that is necessary. When resizing is necessary you would have to lool into NumericArrayResize() from the External Code Reference Manual (and link your DLL with labview.lib in the cintools directory, which makes your DLL only work in LabVIEW anymore.)

Rolf Kalbermatter

Message Edited by rolfk on 01-16-2006 10:13 PM

Rolf Kalbermatter
My Blog
0 Kudos
Message 2 of 5
(4,235 Views)
Thanks - I had a brief look at this option and I think that this is what I need to do (I am actually writing the dll myself, using some other libraries to improve on Labviews functionality).

I think it's a bit strange Labview doesn't allow you to select the complex data type because in C you just take the pointer and act on it like a double array of twice the size.

Something which does work (albeit slightly slowly) is to cast the complex type into type double in Labview and then pass the pointer. Labview's not clever enough to realise you are not actually changing the data here, thus it actually transforms the data from type double[2] to 2 lots of doubles and ending up with exactly the same data in memory (????)

Well I'll see if I can get the structure to work (thanks for the example) and check for speed.
0 Kudos
Message 3 of 5
(4,218 Views)


@Wybird wrote:
Thanks - I had a brief look at this option and I think that this is what I need to do (I am actually writing the dll myself, using some other libraries to improve on Labviews functionality).

I think it's a bit strange Labview doesn't allow you to select the complex data type because in C you just take the pointer and act on it like a double array of twice the size.


Not necessarily strange. I agree that it wouldn't be a big problem for LabVIEW to add this type as well to the Call Library Node. On the other hand there is no standard ANSI C type for complex types, just a common convention, but I'm sure there are analysis libraries that actually switch the real and imaginair part of treat complex arrays as two separate arrays 😉

Rolf Kalbermatter
Rolf Kalbermatter
My Blog
0 Kudos
Message 4 of 5
(4,224 Views)

rolfk wrote: On the other hand there is no standard ANSI C type for complex types, just a common convention
That is true, but my point was that Labview only handles complex data as doubles (or whichever bitdepth you select). This is also true of any library routine but some do [re,im,re,im,...,im] and others do [re,re,...,re,im,im,...,im] and some do [re,re,...], [im,im,...], but the information is simply stored as an array (or two) of defined type (e.g. double) and Labview should allow a pointer to the position of that array to be passed to a dll for the complex type just as it is for real (normal) data.

I can certainly see advantages of using the structure method (for instance it contains the array size) and you still end up with direct access to the data as before.


Thanks
0 Kudos
Message 5 of 5
(4,205 Views)