I have the following C function declaration which I would like to call from C# (Note I cannot access the source code of the dll):
int setData(OuterStruct *data);
And the relevant structs for this C function are as follows:
typedef struct InnerStruct
{
unsigned int a
unsigned int b
unsigned char *c
} InnerStruct;
typedef struct OuterStruct
{
unsigned int d
InnerStruct *e
} OuterStruct;
So far what I have done is:
Created my managed struct variants
[StructLayout(LayoutKind.Sequential)]
public struct OuterStruct
{
public uint d;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
public InnerStruct[] e
}
[StructLayout(LayoutKind.Sequential)]
public struct InnerStruct
{
public uint a;
public uint b;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
public byte[] c
}
created a DllImport method
[DllImport("myDll.dll", EntryPoint ="setData", CallingConvention= CallingConvention.StdCall]
public static extern int setData(ref OuterStruct data);
Called my method and ended up with a constant "AccessViolationException". At the moment I'm not sure if it's an issue on my side or with the .dll and how it processes the input, but want to cover any loose ends I might have first.
Note I have created a variety of other P/Invoke methods for this .dll which use simple structs, only this one fails. Additionally both the .dll and the C# project is running as 32bit
UnmanagedType.ByValArray? All of these are using pointers.