Several methods for communication between processes:
In Windows, processes often need to exchange data for data communication. Common methods include:
Use memory ing files
Share memory through DLL
Use sendmessage to send the wm_copydata message to another process.
Compared with the previous two complex implementations, wm_copydata messages are undoubtedly a cost-effective method. (zt)
The main purpose of a wm_copydata message is to allow the transfer of read-only data between processes. During wm_copydata message transmission, Windows does not provide the inheritance synchronization mode. We recommend that you use the sendmessage function in the SDK documentation. The receiver does not return the message before the data copy is completed, so that the sender cannot delete or modify the data:
The prototype of this function and the structure to be used are as follows:
Sendmessage (hwnd, wm_copydata, wparam, lparam );
The hexadecimal number of wm_copydata is 0x004a.
Wparam is set to the handle of the window containing data. Lparam points to a copydatastruct structure:
Typedef struct tagcopydatastruct {
DWORD dwdata; // user-defined data
DWORD cbdata; // data size
Pvoid lpdata; // pointer to data
} Copydatastruct;
This structure is used to define user data.
The specific process is as follows:
First, find the receiver's handle using findwindow on the sender, and then send the wm_copydata message to the receiver.
The receiver processes the message in the defwndproc event. Because the Chinese encoding is two bytes, the length of the byte must be clear when the Chinese character is passed.
The body code is as follows:
//---------------------------------------------------
// Sender:
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. runtime. interopservices;
Namespace winformsendmsg
{
Public class form1: system. Windows. Forms. Form
{
Private system. Windows. Forms. textbox textbox1;
Private system. Windows. Forms. Button button1;
Private system. componentmodel. Container components = NULL;
Const int wm_copydata = 0x004a;
Public form1 ()
{
Initializecomponent ();
}
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
Private void initializecomponent ()
{
This. textbox1 = new system. Windows. Forms. Textbox ();
This. button1 = new system. Windows. Forms. Button ();
This. suspendlayout ();
//
// Textbox1
//
This. textbox1.location = new system. Drawing. Point (184, 24 );
This. textbox1.name = "textbox1 ";
This. textbox1.size = new system. Drawing. Size (128, 21 );
This. textbox1.tabindex = 0;
This. textbox1.text = "textbox1 ";
//
// Button1
//
This. button1.location = new system. Drawing. Point (344, 16 );
This. button1.name = "button1 ";
This. button1.size = new system. Drawing. Size (112, 32 );
This. button1.tabindex = 1;
This. button1.text = "button1 ";
This. button1.click + = new system. eventhandler (this. button#click );
//
// Form1
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (536,142 );
This. Controls. addrange (new system. Windows. Forms. Control [] {
This. button1,
This. textbox1 });
This. Name = "form1 ";
This. Text = "sender form ";
This. resumelayout (false );
}
Static void main ()
{
Application. Run (New form1 ());
}
[Dllimport ("user32.dll", entrypoint = "sendmessage")]
Private Static extern int sendmessage (
Int hwnd, // handle to destination window
Int MSG, // message
Int wparam, // first Message Parameter
Ref copydatastruct lparam // second Message Parameter
);
[Dllimport ("user32.dll", entrypoint = "findwindow")]
Private Static extern int findwindow (string lpclassname, string
Lpwindowname );
Private void button#click (Object sender, system. eventargs E)
{
Int window_handler = findwindow (null, @ "receiver form ");
If (window_handler = 0)
{
}
Else
{
Byte [] Sarr = system. Text. encoding. Default. getbytes (this. textbox1.text );
Int Len = Sarr. length;
Copydatastruct CDs;
CDs. dwdata = (intptr) 100;
CDs. lpdata = This. textbox1.text;
CDs. cbdata = Len + 1;
Sendmessage (window_handler, wm_copydata, 0, ref CDs );
}
}
}
Public struct copydatastruct
{
Public intptr dwdata;
Public int cbdata;
[Financialas (unmanagedtype. lpstr)] Public String lpdata;
}
}
//---------------------------------------------------
// Receiver
//---------------------------------------------------
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
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 ()
{
Initializecomponent ();
}
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
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 form ";
This. resumelayout (false );
}
Static void main ()
{
Application. Run (New form1 ());
}
Protected override void defwndproc (Ref system. Windows. Forms. Message m)
{
Switch (M. msg)
{
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;
}
}
}
Public struct copydatastruct
{
Public intptr dwdata;
Public int cbdata;
[Financialas (unmanagedtype. lpstr)] Public String lpdata;
}
}