Procedure is exited upon marshalling a C struct received via WM_COPYDATA into a C# struct -


here problem. trying marshal c struct c# struct. c struct sent c application c# application via wm_copydata message. sending , acknowledging message not problem , works fine.

i run c application in debug, attach debugger c# application , send message. received , identified correctly when try marshal data, marshal.ptrtostructure function seems exit switch statement before executing rest of (see marked line in code).

the procedure managecomplexmessage therefore not executed , data remains raw , unusable.

this copydatastruct use in c# :

private struct copydatastruct {      public intptr dwdata;      public int cbdata;      public intptr lpdata;  } 

this struct use in c# store data sent through wm_copydata :

private struct complexdata {     [marshalas(unmanagedtype.byvalarray, sizeconst = 75)]     public int[] integers;     [marshalas(unmanagedtype.byvalarray, sizeconst = 75)]     public double[] doubles;     [marshalas(unmanagedtype.byvalarray, arraysubtype = unmanagedtype.lpstr, sizeconst = 25)]     public string[] strings; }; 

the method processes wm_copydata message :

protected override void wndproc(ref message m) {     int id;      base.wndproc(ref m);      switch (m.msg)     {         case wm_copydata:             copydatastruct rawdata = (copydatastruct) marshal.ptrtostructure(m.lparam, typeof(copydatastruct)); // call ptrtostructure works fine.             id = rawdata.dwdata.toint32();              if (enum.isdefined(typeof(complexmessageid), id))             {                 complexdata data = (complexdata) marshal.ptrtostructure(rawdata.lpdata, typeof(complexdata)); // call makes procedure exit.                 managecomplexmessage((complexmessageid) id, data);                 m.result = (intptr) 1;             }             else             {                 m.result = (intptr) 2;             }             break;     } } 

the struct use in c send data c# application :

struct complexdata {         int integers[75]; // maximum 75 integers     double doubles[75]; // maximum 75 doubles     char strings[25][256]; // maximum 25 strings of maximum length of 256 characters };   

and, finally, procedure sends message :

int sendcopydatamessage(int messageid, struct complexdata messagedata) {     copydatastruct datatosend;      int result;      datatosend.dwdata = messageid;     datatosend.cbdata = sizeof(messagedata);     datatosend.lpdata = &messagedata;      if(windowhandle != null)      {         result = sendmessage(windowhandle, wm_copydata, (wparam) ((hwnd) execwnd), (lparam) ((pvoid) &datatosend));         if(!result)          {             return messagenotprocessed;         }         if(result == 2)         {             return undefinedmessageid;         }     }      return ok; } 

i suspect problem when marshal array of strings can't find problem.

thank help! hope can tell me what's wrong or point me toward right direction!

edit : after more testing, integer part , double part of struct works fine. if send single string works. crashes when try send array of string. problem in marshalas statement string array, still can't find out how write properly.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -