P/Invoke from C to C# without knowing size of array -


right know in code have structure declared this, fixed 16, know @ compile time.

struct console_screen_buffer_infoex { [marshalas(unmanagedtype.byvalarray, sizeconst = 16)] public int colortable[]; } 

but need able have structure:

struct console_screen_buffer_infoex {    int arraysize;    [marshalas(unmanagedtype.byvalarray, sizeconst = 0)]    public int colortable[]; } 

get arraysize c function response, initialize colortable array proper size, put result of response colortable.

not sure if it's possible, doing investigation right now, , comments welcome.

you can enough manual marshalling using marshal class. example:

[dllimport(@"mylib.dll")] private static extern void foo(intptr structptr);  private static intptr structptrfromcolortable(int[] colortable) {     int size = sizeof(int) + colortable.length*sizeof(int);     intptr structptr = marshal.allochglobal(size);     marshal.writeint32(structptr, colortable.length);     marshal.copy(colortable, 0, structptr + sizeof(int), colortable.length);     return structptr; }  private static int[] colortablefromstructptr(intptr structptr) {     int len = marshal.readint32(structptr);     int[] result = new int[len];     marshal.copy(structptr + sizeof(int), result, 0, len);     return result; }  static void main(string[] args) {     int[] colortable = new int[] { 1, 2, 3 };     intptr structptr = structptrfromcolortable(colortable);     try     {         foo(structptr);         colortable = colortablefromstructptr(structptr);     }         {         marshal.freehglobal(structptr);     } } 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -