I have a C++ DLL which exports functions that use structs
as input and output .
I want to call the DLL from a C# application. The struct
definition in C++ looks something like this:
struct stIn
{
double A;
double B;
double C;
int D;
double dArray[3];
double dArra2;
double E;
double mat[10][4];
double F;
int G;
}
I have declared a C# struct with the LayoutKind.Sequential
attribute.
The arrays in the struct are declared with [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
attribute
The mat is declared with [MarshalAs(UnmanagedType.SafeArray)]
.
I have noticed that the array layout in the memory is not in the order of the declaration - the arrays are at the end of the "memory chunk" of the struct
(the memory sequence is A B C D E F G, darray etc. ), and as a result the call to the DLL function returns erroneous results.
What did I miss? Is the mat declaration wrong? is there another attribute to declare in order to get the right parameter sequence into memory?
Thanks.