Use SendMessage to implement C # inter-process communication

Source: Internet
Author: User

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. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using System. Runtime. InteropServices;

Namespace WindowsFormsApplication1
{
Public partial class Form1: Form
{
Const int WM_COPYDATA = 0x004A;

Public Form1 ()
{
InitializeComponent ();
}

Private void Form1_Load (object sender, EventArgs e)
{

}

[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, EventArgs e)
{
Int WINDOW_HANDLER = FindWindow (null, @ "title of the window to be sent ");
If (WINDOW_HANDLER! = 0)
{
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;
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.