Several methods of communication between processes

xiaoxiao2021-03-06  139

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.

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 () {initializationComponent ();

Protected Override Void Dispose (bool disposing) {if (disponents! = 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.button1_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 windowint Msg, // messageint wParam, // first message parameterref COPYDATASTRUCT lParam // second message parameter );

[DLLIMPORT ("User32.dll", entrypoint = "findwindow")] private static extern int findwindow (StringlpWindown);

Private void button1_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 (THISTBOX1.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 cobdata; [marshalas (unmanagedtype.lpstr)] public string lpdata;

}

/ / -------------------------------------------------------------------------------------------- --- // Acceptant // ----------------------------------------- ---------- Using System; use 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 () {initializationComponent ();

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 cobdata; [marshalas (unmanagedtype.lpstr)] public string lpdata;}}

转载请注明原文地址:https://www.9cbs.com/read-127353.html

New Post(0)