Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. struct containing array of pointers to struct (C dll to C#)

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

Scheduled Pinned Locked Moved C#
helptutorialcsharpgraphicsdata-structures
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dfn
    wrote on last edited by
    #1

    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 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_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.

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups