struct containing array of pointers to struct (C dll to C#)
-
Hi. In C dll, there is a struct that contains an array of struct pointers. I do not know how to represent that in C#. That's my problem. Here is the relevant C code:
struct vector
{
uint elem_count;
uint size;
int (*cmp)(const void *, const void *);
void *table;
};typedef struct vector VECTOR;
#define CTABLE VECTOR
#define CSTATS VECTORstruct cstream
{
int mode;
int byte_count;
uchar *buff;
int buff_len;
int buff_pos;
int bit_pos;
bool growable;
bool prepared;
CTABLE *table[2];
CSTATS *stats[3];
uchar last_mode;
int last_mode_offset;
};And here is the C# code:
[UnmanagedFunctionPointer(CallingConvention.Winapi) ]
unsafe public delegate int cmp(IntPtr a, IntPtr b);\[StructLayout(LayoutKind.Sequential, Pack = 1)\] unsafe public struct VECTOR { public uint elem\_count; public uint size; public uint elem\_size; \[MarshalAs(UnmanagedType.FunctionPtr)\] public cmp cp; //C-func: public int (\*cmp)(const void \*, const void \*); public IntPtr table; } \[StructLayout(LayoutKind.Sequential, Pack = 1)\] unsafe public struct CSTREAM { public int mode; public int byte\_count; public IntPtr buff; public int buff\_len; public int buff\_pos; public int bit\_pos; public bool growable; public bool prepared; public VECTOR\[2\]; // CTABLE \*table\[TTABLE\_COUNT\]; public VECTOR\[3\]; // CSTATS \*stats\[STATS\_COUNT\]; public byte last\_mode; public int last\_mode\_offset; } \[DllImport("compr.dll")\] public static extern IntPtr screate(); // this function in C dll returns pointer to struct \[DllImport("compr.dll")\] public static extern IntPtr tcreate(); // this function in C dll returns pointer to struct;
Now, I know how to get the IntPtr to a struct. For example:
MyClass.VECTOR stext = new MyClass.VECTOR();
IntPtr st = MyClass.screate();
stext = (MyClass.VECTOR)Marshal.PtrToStructure(st, typeof(MyClass.VECTOR));However, I do not know how to represent, or work with, "CTABLE *table[2];" or "CSTATS *stats[3];" from the struct in C, in the C# struct (CSTREAM). Does anyone know how to do this? Thank you for any help.