LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

DLL operating on complex array (VC++)

I've tried writing DLL in Borland C++ Builder 6, but there were errors with access to complex data. So, I've changed compilator on MS Visual C++ 6.0 and here also I'cant change the complex data. I've defined following structures:
 
typedef struct {
        double re;
        double im;
        } Complex64;
 
typedef struct {
 int dimSize;
 Complex64 cmplx[0];
 } ComplexArr;
typedef ComplexArr **ComplexHdl;
 
and simple function like that:
 
extern "C" __declspec(dllexport) int MyComplexFunction(ComplexHdl arr)
{
    int size = (*arr)->dimSize;
    Complex64 * cmplx = (*arr)->cmplx;
    double tmp;
    for (int i = 0; i < size; i++, cmplx++)
    {
    cmplx->re *=2;
    cmplx->im *=3;
    }
    return size;
}
 
I can't change anything complex data, these settings just don't work. Is there anybody who now what's going on and what I should change to fix it? I attach also picture with LabView block diagram, because maybe there I do something incorrectly..
 

Thanks for any help,
Darek
0 Kudos
Message 1 of 3
(2,974 Views)


@daras wrote:
I've tried writing DLL in Borland C++ Builder 6, but there were errors with access to complex data. So, I've changed compilator on MS Visual C++ 6.0 and here also I'cant change the complex data. I've defined following structures:
 
typedef struct {
        double re;
        double im;
        } Complex64;
 
typedef struct {
 int dimSize;
 Complex64 cmplx[0];
 } ComplexArr;
typedef ComplexArr **ComplexHdl;
 
and simple function like that:
 
extern "C" __declspec(dllexport) int MyComplexFunction(ComplexHdl arr)
{
    int size = (*arr)->dimSize;
    Complex64 * cmplx = (*arr)->cmplx;
    double tmp;
    for (int i = 0; i < size; i++, cmplx++)
    {
    cmplx->re *=2;
    cmplx->im *=3;
    }
    return size;
}
 
I can't change anything complex data, these settings just don't work. Is there anybody who now what's going on and what I should change to fix it? I attach also picture with LabView block diagram, because maybe there I do something incorrectly..
 

A few things to check:

1) Have you made sure the Call Library Node has configured the array parameter as Adapt to Type, Handles by Value?

2) In your example code you use the loop variable i but don't declare it. A C compiler won't be able to compile this.

3) Make sure you use byte alignement. LabVIEW byte packs data structures, whereas Visual C (and probably Borland C) by default will use 8 byte alignement. This means that a DLL created with Visual C will align the Complex array element cmplx[0] in your ComplexArr structure to 8 bytes (the size of the double value inside the Complex64 structure) while LabVIEW will just pack it and place the first complex element at offset 4 in the ComplexArr structure, resulting in total misalignment and therefore rubbish between your DLL and LabVIEW.

You can set the default alignment of Visual C in the project settings under C/C++->Code Generation (Visual C 6.0) or maybe even better yet add a

#pragma pack(1)

<your type declarations for structures that you want to receive from LabVIEW>

#pragma pack()

to your source file. Borland C has similar makros but not exactly the same. They specifically don't use #pragma.

Rolf Kalbermatter


Message Edited by rolfk on 07-06-2006 10:13 AM

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 2 of 3
(2,960 Views)
OK, I writed correct version. The problem was with double type, when I use single complex type (float complex in C++) everything is ok, I have access to every field of this structure and I can change it without Access Violation error.
 
Thanks for help!

If anyone is interested in source code which works properly :
 
typedef struct {
 long dimSize;
 cmplx64 elt[1];
 } TDclx;
typedef TDclx **Complex64Handle;
extern "C" __declspec(dllexport) int make_something(Complex64Handle input, Complex64Handle output)
{
        int array_size = (*input)->dimSize;        for (int i=0; i<FFT_rozmiar; i++)
        for (int i=0; i<array_size; i++)
         {
           (*output)->elt[i].re=2*(*input)->elt[i].re;
           (*output)->elt[i].im=3*(*input)->elt[i].re;
         }
        return array_size;
}
And we must remember to initialise output array in LabView with correct size and data type;
0 Kudos
Message 3 of 3
(2,926 Views)