LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Pass an array of cluster including a double array to a C DLL

Hi all!
 
I want to pass an array of cluster including a double array to a C DLL. I use a Call Library Node to Call the DLL.  The Datatype of the input parameter is Adapt to Type with Array Data Pointer. It is working with an integer 32 array but when i want to change to a double(float64) array LabView even crashes.
 
Here is the source code of the working DLL.
 
#include "stdafx.h"
#include "extcode.h"
#include <stdlib.h>
 
/*Typedefs*/
typedef struct {
 int32 dimSize;
 int32 elt[1];
} TD1;
typedef TD1 **TD1Hdl;
 
typedef struct {
 TD1Hdl elt1;
 TD1Hdl elt2;
 TD1Hdl elt3;
 LStrHandle elt4;
} TD2;

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
      )
{
    return TRUE;
}

_declspec(dllexport) void ImportClusterArray(TD2 *input, TD2 *output, long *arraylength, long *i1, long *i2, LStrHandle LVString);
 
_declspec(dllexport) void ImportClusterArray(TD2 *input, TD2 *output, long *arraylength, long *i1, long *i2, LStrHandle LVString)
{
 int i,j;
 
 for(i=0; i<*arraylength;i++)
 {
  //output[i].elt2 = (long) input[i].elt2 * 2;
 }
 *i1 = (int32) (*(input[0].elt1))->dimSize;
 (*(output[0].elt1))->dimSize = *i1;
 for(i=0;i<*i1;i++)
 {
  (*(output[0].elt1))->elt[i] = (int32) ((*(input[0].elt1))->elt[i]) * 2;
 }

 *i2 = (int32)(*(input[0].elt2))->dimSize;
 (*(output[0].elt2))->dimSize = *i2;
 for(i=0;i<*i2;i++)
 {
  (*(output[0].elt2))->elt[i] = (int32) ((*(input[0].elt2))->elt[i]) * 2;
 }
 
 (*(output[0].elt3))->dimSize = (int32)(*(input[0].elt3))->dimSize;
 for(i=0;i<(*(output[0].elt3))->dimSize;i++)
 {
  (*(output[0].elt3))->elt[i] = (int32) ((*(input[0].elt3))->elt[i]) * 2;
 }

 for(j=0;j<*arraylength;j++)
 {
  for(i=0;i<(*(input[j].elt4))->cnt;i++)
  {
   (*(output[j].elt4))->str[i] = (*LVString)->str[i];
  }
 }
}
 
When i want to change the integer Array to a double array
 
/*Typedefs*/
typedef struct {
 int32 dimSize;
 float64 elt[1];
} TD1;
typedef TD1 **TD1Hdl;
 
typedef struct {
 TD1Hdl elt1;
 TD1Hdl elt2;
 TD1Hdl elt3;
 LStrHandle elt4;
} TD2;
 
And the rest of the code is adapted to float64 too. If I changed the array type, LabView chrashes with this error:
 
 
 
Can anyone help me please?
Thanks a lot.
 
Eumel
0 Kudos
Message 1 of 2
(2,937 Views)


@Eumel wrote:
Hi all!
 
I want to pass an array of cluster including a double array to a C DLL. I use a Call Library Node to Call the DLL.  The Datatype of the input parameter is Adapt to Type with Array Data Pointer. It is working with an integer 32 array but when i want to change to a double(float64) array LabView even crashes.
 
Here is the source code of the working DLL.
 
#include "stdafx.h"
#include "extcode.h"
#include <stdlib.h>
 
/*Typedefs*/
typedef struct {
 int32 dimSize;
 int32 elt[1];
} TD1;
typedef TD1 **TD1Hdl;
 
typedef struct {
 TD1Hdl elt1;
 TD1Hdl elt2;
 TD1Hdl elt3;
 LStrHandle elt4;
} TD2;



One thing I miss here is the #pragma pack(1) before the structure declaration.
You can add another #pragma pack() after the structure declarations.

LabVIEW uses byte packing for structures whereas Visual C assumes 8byte alignment for structures. This does not make a difference for int32 arrays in your setup but it certainly makes a difference for the float64 arrays as Visual C will insert an extra int32 filler between dimsize and the actual float64 array. So your VIsual C code will basically access four bytes beyond the end of the array that LabVIEW allocated.

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 2 of 2
(2,859 Views)