Introduction:
This article focuses on the transfer of data between two processes if used in WM_COPYDATA messages.
Several methods of communication between processes:
In the Windows program, there is often a need to exchange data between each process for data communication. Common methods have
Use memory map files to share WM_COPYDATA messages to another process with SendMessage with sendMessage
The WM_COPYDATA message is undoubtedly an economic affordable approach than the previous two complex implementations.
The main purpose of the WM_CopyData message is to allow read-only data to be delivered between processes. Windows does not provide inheritance synchronization mode during transmission through WM_COPYDATA messages. The SDK document recommends that the user uses the sendMessage function, the acceptor does not return before the data copy is completed, so the sender cannot delete and modify the data:
The prototype of this function and the structure thereof should be as follows:
SendMessage (hwnd, wm_copydata, wparam, lparam); where WM_COPYDATA corresponds to 0x004A
WPARAM is set to the handle of the window containing the data. LPARAM points to a CopyDataStructure structure: typef struct tagcopyDataStruct {DWORD DWDATA; // User Defines Data DWORD CBDATA; // Data Size PVOID LPDATA; / / Pointer Pointer Pointer Pointer} CopyDataStruct; This structure is used to define user data.
The specific process is as follows:
First, in the sender, find the acceptor's handle with FindWindow and send a WM_COPYDATA message to the reception.
The acceptor is handled in the DEFWNDPROC event. Since Chinese coding is two bytes, it is clear to pass the Chinese.
There is an appropriate explanation in the code, please see yourself.
The specific code is as follows: // -------------------------------------------- -------//sender://------------------------------------ ---------------
Using system.drawing; using system.collections; using system.windows.form; using system.data; using system.runtime.interopservices;
namespace WindowsFormGetMsg {public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.TextBox textBox1; private System.ComponentModel.Container components = null; const int WM_COPYDATA = 0x004A;
Public form1 () {initializationComponent ();
Protected Override Void Dispose (bool disposing) {if (disponents! = null) {components.dispose ();}} Base.Dispose (Disposing);
#region Windows Form Designer generated code private void InitializeComponent () {this.textBox1 = new System.Windows.Forms.TextBox (); this.SuspendLayout (); // // textBox1 // this.textBox1.Location = new System. Drawing.point (176, 32); this.TextBox1.name = "textbox1"; this.textbox1.size = new system.drawing.size (160,
21); this.TextBox1.tabindex = 0; this.textBox1.text = "textbox1"; // // Form1 // this.autoscalebasesize = new system.drawing.size (6,
14); this.clientsize = new system.drawing.size (432, 266); this.Controls.addrange (New
System.windows.Forms.Control [] {
THIS.TEXTBOX1}; this.name = "form1"; this.text = "Receiver"; this.ResumeLayout (FALSE);
} #Endregion
[Stathread] static void main () {application.run (new form1 ());}
Protected Override Void DefWndProc (Ref
System.Windows.Forms.Message M) {switch (m.msg) {// Receive custom message User and displays its parameters Case WM_COPYDATA: CopyDataStruct MyStr = New
CopyDataStruct (); type mytype = mystr.gettype ();
mystr = (copyDataStruct) m.getlparam (mytype); this.TextBox1.text = mystr.lpdata;
Break; default: base.defwndproc (Ref m); Break;
}
}
} [Structlayout (layoutkind.sequential] public struct copyDataStruct {public intptr dwdata; public int cobdata; [Marshalas (unmanagedtype.lpstr)] public string lpdata;}}
/ / -------------------------------------------------------------------------------------------- --- // Acceptant // ----------------------------------------- .
namespace WindowsFormGetMsg {public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.TextBox textBox1; private System.ComponentModel.Container components = null; const int WM_COPYDATA = 0x004A; public Form1 () {InitializeComponent (); }
Protected Override Void Dispose (bool disposing) {if (disponents! = null) {components.dispose ();}} Base.Dispose (Disposing);
#region Windows Form Designer generated code private void InitializeComponent () {this.textBox1 = new System.Windows.Forms.TextBox (); this.SuspendLayout (); // // textBox1 // this.textBox1.Location = new System. Drawing.point
(176, 32); this.TextBox1.name = "textbox1"; this.textbox1.size = new system.drawing.size (160,
21); this.TextBox1.tabindex = 0; this.textBox1.text = "textbox1"; // // Form1 // this.autoscalebasesize = new system.drawing.size (6,
14); this.clientsize = new system.drawing.size (432, 266); this.Controls.addrange (New
System.windows.Forms.Control [] {
THIS.TEXTBOX1}; this.name = "form1"; this.text = "Receiver"; this.ResumeLayout (FALSE);
} #Endregion
[Stathread] static void main () {application.run (new form1 ());}
Protected Override Void DefWndProc (Ref
System.Windows.Forms.Message M) {switch (m.msg) {// Receive custom message User and displays its parameters Case WM_COPYDATA: CopyDataStruct MyStr = New
CopyDataStruct (); type mytype = mystr.gettype ();
mystr = (copyDataStruct) m.getlparam (mytype); this.TextBox1.text = mystr.lpdata;
Break; default: base.defwndproc (Ref m); Break;
}
}