Marshaling Object
-
Hi, I have applications that need to communicate with each other using HWND_BROADCAST Here is the prototype for unmanged code function SendMessage
[DllImport("user32.dll")] extern static IntPtr SendMessage(IntPtr hWnd, int msg, MyClass myClass, IntPtr lParam);
where WParam is passed as an object reference. In the overriden WndProc method on the other application, the WParam variable is marshaled back to the original object like thisprotected override void WndProc(ref Message m) { if (m.Msg == CommonMessageID) { ... // myClass contains junk data here MyClass myClass = (MyClass)Marshal.PtrToStructure(m.WParam, typeof(MyClass)); ... } base.WndProc(ref m); }
However, it does not hold the information that was initialized by the other app. What did I do wrong here? Jup -
Hi, I have applications that need to communicate with each other using HWND_BROADCAST Here is the prototype for unmanged code function SendMessage
[DllImport("user32.dll")] extern static IntPtr SendMessage(IntPtr hWnd, int msg, MyClass myClass, IntPtr lParam);
where WParam is passed as an object reference. In the overriden WndProc method on the other application, the WParam variable is marshaled back to the original object like thisprotected override void WndProc(ref Message m) { if (m.Msg == CommonMessageID) { ... // myClass contains junk data here MyClass myClass = (MyClass)Marshal.PtrToStructure(m.WParam, typeof(MyClass)); ... } base.WndProc(ref m); }
However, it does not hold the information that was initialized by the other app. What did I do wrong here? JupHi! You cannot simply stuff an object into an arbitrary SendMessage call and expect it to appear unchanged at another application. SendMessage is sending pointers as parameters (wPar, lPar), but these pointers point to an adress in the current process adress space! You can transmit simple datatypes (int) that way but no objects. In Win32 there's a special message WM_COPYDATA that's used to transfer data from one process to another, but I strongly doubt that this will solve your problem. For transfering objects (or rather object references) you should use .NET Remoting. Regards, mav -- Black holes are the places where god divided by 0...
-
Hi! You cannot simply stuff an object into an arbitrary SendMessage call and expect it to appear unchanged at another application. SendMessage is sending pointers as parameters (wPar, lPar), but these pointers point to an adress in the current process adress space! You can transmit simple datatypes (int) that way but no objects. In Win32 there's a special message WM_COPYDATA that's used to transfer data from one process to another, but I strongly doubt that this will solve your problem. For transfering objects (or rather object references) you should use .NET Remoting. Regards, mav -- Black holes are the places where god divided by 0...