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.

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 client {public partial class frmclient: FORM {public frmclient () {initializecomponent ();} private void frmclient_load (Object sender, eventargs e) {} [dllimport ("us Er32.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); public struct copydatastruct {public Intptr dwdata; Public int cbdata; [financialas (unmanagedtype. lpstr)] Public String lpdata;} const int wm_copydata = 0x004a; private void btnsend_click (Object sender, eventargs e) {int window_handler = findwindow (null, @ "C # inter-process communication (server)"); 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 );}}}}
Acceptor code:
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 server {public partial class frmserver: FORM {public frmserver () {initializecomponent ();} private void form1_load (Object sender, eventargs E) {} public struct copydatastruct {public intptr dwdata; Public int cbdata; [financialas (unmanagedtype. lpstr)] Public String lpdata;} const int wm_copydata = 0x004a; 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); lblmsg. TEXT = "received message:" + mystr. lpdata; MessageBox. show ("received message:" + mystr. lpdata); break; default: base. defwndproc (ref m); break ;}}}}

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.