barcel
11/19/2006 5:01:00 PM
Hello,
I have an old C-DLL that I need to call from my C# application. There
is one struct in the dll which I need to marshal; it is defined like
this:
typedef struct _fp
{
WORD rows;
WORD cols;
double Array[1];
} FP;
typedef FP far *LPFP;
WORD is an unsigned short.
The functions in the dll use the LPFP to take in and return arrays (of
variable length) with data.
A function that takes in an array might look like this.
double LINKDLL Test_c( double a, double b, LPFP c) { ... }
My problem is how I can setup and pass this struct from C# to the C DLL
and vice versa?
One way that seems to work is to Marshal the array using this struct in
C#:
public struct LPFP
{
ushort rows;
ushort cols;
[MarhsalAs(UnmanagedType.ByValArray, SizeConst=1000)]
double[] array;
}
and call my C# function like this:
double result = Test_c( a, b, ref LPFP c);
where Test is declared as: public extern static double Test_c (double
a, double b, ref LPFP c);
This means I always pass an array of 1000 elements, but even if this
works it is not that nice to assume a specific length of the array when
it is supposed to be variable.
Also would the solution change if the c-function needs to return this
struct instead of only taking it as an input? (Like: LPFP LINKDLL
Test2_c (double a, double b, LPFP c) )
Any help is appreciated (especially a detailed explanation since I am
new to the Marshalling bit).
Thanks