How does the communication between two independent EXE programs complete? The first idea is to use the method of generating the file, that is, the sender in a directory
into a file that contains the message to be sent, the receiving end reads the text from this directory and get the message. This method can also achieve communication, but it always feels
Point More this A swoop. Can you allow two EXE programs to communicate directly, send a message?
The answer is YES! And there are several ways. 1 Send Message Delivery 2 Shared memory Pass 3 using COM out-of-process server ...
This article mainly explains how to use the Send message to make two EXE complete communication. Two other methods interested can search under, can find the corresponding
of the demo, this is no longer an additional explanation.
This demo is divided into two parts, the sender and the receiving end. As the name implies is responsible for sending messages and receiving messages respectively.
Add the two forms
send-side code:
The data structure required by the WM_COPYDATA message is public struct Copydatastruct {public IntPtr dwdata; public int cbdata; [MarshalAs (UNMANAGEDTYPE.LPSTR)] public string lpdata; } public const int wm_copydata = 0X004A; Find the window handle through the window's caption [DllImport ("User32.dll", EntryPoint = "FindWindow")] private static extern int FindWindow (String lpclassname, string lpwindowname); Send Message function in DLL library [DllImport ("User32.dll", EntryPoint = "SendMessage")] private static extern int SendMessage (int hWnd,//The handle of the target window int MSG,//Here is Wm_copydata int WParam,//First message parameter ref COPYDATASTRUCT LParam//second message parameters); private void Button1_Click (object sender, EventArgs e) {//The value in the text box is sent to the receive-side string strURL = Txtimpinj.text; COPYDATASTRUCT CDs; Cds.dwdata = (IntPtr) 1; Here you can pass in some custom data, but only a 4-byte integer cds.lpdata = strURL; The message string cds.cbdata = System.Text.Encoding.Default.GetBytes (strURL). Length + 1; Note that the length here is calculated by Byte SendMessage (FindWindow (NULL, "Receive Side"), Wm_copydata, 0, ref CDs); This is to be changed to the receiving window's title "Receiving End"//this. Close (); }
Receive-side code:
The data structure required by the WM_COPYDATA message is public struct copydatastruct {public IntPtr dwdata; public int cbdata; [MarshalAs (UNMANAGEDTYPE.LPSTR)] public string lpdata; } Private Const int wm_copydata = 0x004a; Receive Message method protected override void WndProc (ref System.Windows.Forms.Message E) { if (e.msg = = Wm_copydata ) { copydatastruct CDs = (COPYDATASTRUCT) E.getlparam (typeof (Copydatastruct)); TextBox1.Text = Cds.lpData.ToString (); Displays text information to the text box //messagebox.show (cds.lpdata); } Base. WndProc (ref e); }
After compiling, locate the directory: exe file under ~\bin\debug to run. The effect is:
The code above is just a demo. In order to illustrate the problem, we can encapsulate these methods into a class to facilitate reuse.
In general, the principle of this approach is to use the FindWindow function to find the other party's process through the form header, and then get the window
Handle, and then sends the message to the receiving end through the SendMessage function in the DLL library, thus completes the direct communication of the program.
Other approaches can also be achieved, welcome to exchange.
C # two independent EXE program direct communication