I have the following C structure
struct XYZ
{
void *a;
char fn[MAX_FN];
unsigned long l;
unsigned long o;
};
And I want to call the following function from C #:
extern "C" int func(int handle, int *numEntries, XYZ *xyzTbl);
Where xyzTbl is an numEntires size XYZ array that is allocated by the caller
I defined the following C # structure:
[StructLayoutAttribute(Sequential, CharSet = CharSet.Ansi)]
public struct XYZ
{
public System.IntPtr rva;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 128)]
public string fn;
public uint l;
public uint o;
}
and method:
[DllImport(@"xyzdll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern Int32 func(Int32 handle, ref Int32 numntries,
[MarshalAs(UnmanagedType.LPArray)] XYZ[] arr);
Then I try to call the function:
XYZ xyz = new XYZ[numEntries];
for (...) xyz[i] = new XYZ();
func(handle,numEntries,xyz);
Of course, this will not work. Can someone shed light on what I am doing wrong?
source
share