473,324 Members | 2,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

C#-APP: WM_COPYDATA and Serialization

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
0 1944

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

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
1
by: andrewcw | last post by:
There is an error in XML document (1, 2). I used XML spy to create the XML and XSD. When I asked to have the XML validated it said it was OK. I used the .net SDK to generate the class. I have...
3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
6
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or...
3
by: Alexander | last post by:
When i store rule on PC with .NET.SP1 i cant restore them from PC without SP1. An i get this Error: System.Runtime.Serialization.SerializationException: Possible Version mismatch. Type...
3
by: Paulo Morgado [MVP] | last post by:
Hi all ..NET Framework 1.1 I have created several types that are serailized to XML as strings. Someting like this: public struct MyInt32 : IXmlSerializable { int value;
4
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I...
0
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
1
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.