LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How Can I Marshal The Error Cluster In a .NET App?

I've been struggling with trying to pass error cluster data in and out of a Labview 7.1-built DLL from a C# program.  I've found many entries in the discussion forum that come close to answering my problem but most of the discussions are dealing with C or C++ or they are dealing with passing the data from Labview to a DLL made by C or C++. 

One of the challenges with C# is that you have limited use of pointers in "unsafe" code blocks. The C definitions associated with the Labview error cluster are:

typedef

struct { // The Error Cluster as a structure
LVBoolean status;
int32 code;
LStrHandle source;
} TD1;

typedef struct { // The String member of the error cluster as a structure
int32 cnt;
/* number of bytes that follow */
uChar str[
1]; /* cnt bytes */
} LStr, *LStrPtr, **LStrHandle;

Note that the string member is really a Handle, .i.e., a pointer to a pointer.  C# doesn't allow the use of pointers to managed data like Strings and Arrays.  It also doesn't allow pointers to structures containing managed data.  I have been unable to find a way to marshal data from the "unmanaged" DLL code to the "managed" C# code.  I have been able to marshal in a Labview string when it is a separate parameter.  The problem occurs when the LV string is a member of a cluster.

0 Kudos
Message 1 of 2
(2,948 Views)
You don't need to use unsafe blocks and pointers necessarily. The PInvoke marshalling instructions should be able to handle most of this. I'll admit that P/Invoke is not trivial at all, but www.pinvoke.net can get you a lot of examples for the Win32 API - those have some complex structures as well.

However, if I were trying to do this, I'd create a wrapper assembly using C++/CLI. This .NET language makes it very easy to convert the non-.NET C code into .NET types. This wrapper assembly then would expose the DLL functions as .NET methods, which you would call from your C# code.
0 Kudos
Message 2 of 2
(2,935 Views)