struct containing array of pointers to struct (C dll to C#)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zman77
    New Member
    • Sep 2007
    • 17

    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:

    [PHP]struct vector
    {
    uint elem_count;
    uint size;
    int (*cmp)(const void *, const void *);
    void *table;
    };

    typedef struct vector VECTOR;

    #define CTABLE VECTOR
    #define CSTATS VECTOR

    struct 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_offse t;
    };[/PHP]

    And here is the C# code:

    [PHP] [UnmanagedFuncti onPointer(Calli ngConvention.Wi napi)]
    unsafe public delegate int cmp(IntPtr a, IntPtr b);

    [StructLayout(La youtKind.Sequen tial, Pack = 1)]
    unsafe public struct VECTOR
    {
    public uint elem_count;
    public uint size;
    public uint elem_size;
    [MarshalAs(Unman agedType.Functi onPtr)]
    public cmp cp; //C-func: public int (*cmp)(const void *, const void *);
    public IntPtr table;
    }

    [StructLayout(La youtKind.Sequen tial, 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_offse t;
    }

    [DllImport("comp r.dll")]
    public static extern IntPtr screate(); // this function in C dll returns pointer to struct

    [DllImport("comp r.dll")]
    public static extern IntPtr tcreate(); // this function in C dll returns pointer to struct;[/PHP]

    Now, I know how to get the IntPtr to a struct. For example:
    [PHP]MyClass.VECTOR stext = new MyClass.VECTOR( );
    IntPtr st = MyClass.screate ();
    stext = (MyClass.VECTOR )Marshal.PtrToS tructure(st, typeof(MyClass. VECTOR));[/PHP]
    However, I do not know how to represent, or work with, "CTABLE *table[2];" or "CSTATS *stats[3];" from my struct in C, in my C# struct (CSTREAM).
    I'd appreciate any help.
Working...