By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,919 Members | 1,625 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,919 IT Pros & Developers. It's quick & easy.

C#-APP: WM_COPYDATA and Serialization

P: 71
Hi everyone, I am trying to combine WM_COPYDATA and Serialization. I am converting an object into string using Serialization then pass that string as the LParam of my COPYDATASTRUCT. I can successfully send the string message but the recipient application cannot recieve it, it just have an empty string. Can someone tell me what I am missing. I really want to understand it. Please see the source code below:

Expand|Select|Wrap|Line Numbers
  1. public struct STRINGCOPYDATASTRUCT
  2.     {
  3.         public IntPtr dwData;
  4.         public int cbData;
  5.         [MarshalAs(UnmanagedType.LPStr)]
  6.         public string lpData;
  7.     }
  8.  
  9. public string StructToStrSer(object structstate)
  10.         {
  11.             MemoryStream memStream = new MemoryStream();
  12.             BinaryFormatter binform = new BinaryFormatter();
  13.             binform.Serialize(memStream, structstate);
  14.             byte[] newbyte = memStream.ToArray();
  15.             return Encoding.Default.GetString(newbyte);
  16.         }
  17.  
  18. private void CreateRequestMessage(string id, string[] val)
  19.         {
  20.             SendStrMsg("Game Play Client", this.Handle, StructToStrSer(obj));
  21.         }
  22.  
  23. public static void SendStrMsg(string targetname, IntPtr sourcehandle, string msg)
  24.         {
  25.             //string sTargetWindowTitle = "The Form";
  26.             int hWnd = GetTargetWindow(targetname, sourcehandle);
  27.             int wParam = 100; //for string message
  28.             byte[] sarr = System.Text.Encoding.Default.GetBytes(msg);
  29.             int len = sarr.Length;
  30.  
  31.             STRINGCOPYDATASTRUCT cds;
  32.             cds.dwData = (IntPtr)100;
  33.             cds.lpData = msg;
  34.             cds.cbData = len + 1;
  35.  
  36.             SendMessage(hWnd, WM_COPYDATA, wParam, ref cds);
  37.         }
  38.  
  39.  
The Reciepient Application will have these codes:

Expand|Select|Wrap|Line Numbers
  1. protected override void WndProc(ref Message m)
  2.         {
  3.             string msg;
  4.  
  5.             switch (m.Msg)
  6.             {              
  7.                 case WMSendMessage.WM_COPYDATA:
  8.                     switch ((int)m.WParam)
  9.                     {
  10.                         case 100:
  11.                             {
  12.                                 // Recieved String
  13.                                 STRINGCOPYDATASTRUCT myobj = new STRINGCOPYDATASTRUCT();
  14.                                 Type mytype = myobj.GetType();
  15.                                 myobj = (STRINGCOPYDATASTRUCT)m.GetLParam(mytype); 
  16.  
  17.                                 this.tbConsole.Text = myobj.lpData + \n\n + myobj.cbData.ToString() + \n\n + myobj.dwData.ToString();
  18.                             }
  19.                             break;
  20.                         default:
  21.                             break;
  22.                     }
  23.                     break;
  24.                 default:
  25.                     break;
  26.             }
  27.             base.WndProc(ref m);
  28. }
  29.  
  30.  
  31.  
Thanks in advance.
Jan 7 '09 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.