C # Two Independent exe programs communicate directly,

Source: Internet
Author: User

C # Two Independent exe programs communicate directly,

How do two independent exe programs complete communication? The first method is to use the file generation method, that is, the sender generates a file under a directory.

A file containing the message to be sent. The receiving end reads the file from this directory and obtains the message. This method can also implement communication, but it always feels like

Click here. Can two exe programs communicate directly and send messages?

The answer is yes! There are also several methods. 1 send message transfer 2 share memory transfer 3 use the COM out-of-process server...

This article mainly describes how to send messages to enable communication between two exe files. If you are interested in the other two methods, you can search for them and find the corresponding

The demo is not added here.

This demo is divided into two parts: the sending end and the receiving end. As the name suggests, they are responsible for sending and receiving messages respectively.

Add the two forms

Sending code:

?
1234567891011121314151617181920212223242526272829303132333435363738 // The data structure required by the WM_COPYDATA messagepublic struct CopyDataStruct{    public IntPtr dwData;    public int cbData;     [MarshalAs(UnmanagedType.LPStr)]     public string lpData;} public const int WM_COPYDATA = 0x004A; // Find the handle of the window by the title of the window[DllImport("User32.dll", EntryPoint = "FindWindow")]private static extern int FindWindow(string lpClassName, string lpWindowName); // Message sending function in DLL library[DllImport("User32.dll", EntryPoint = "SendMessage")]private static extern int SendMessage    (    int hWnd,                         // Handle of the target window    int Msg,                          // Here is WM_COPYDATA    int wParam,                       // The first message Parameter    ref  CopyDataStruct lParam        // The second Message Parameter   ); private void button1_Click(object sender, EventArgs e){    // Send the value in the text box to the receiver    string strURL = txtImpinj.Text;    CopyDataStruct cds;    cds.dwData = (IntPtr)1; // Some custom data can be imported here, but it can only be a 4-byte integer    cds.lpData = strURL;    // Message string    cds.cbData = System.Text.Encoding.Default.GetBytes(strURL).Length + 1// Note that the length here is calculated in bytes.    SendMessage(FindWindow(null, "Receiver"), WM_COPYDATA, 0, ref cds);       // Modify the title of the receiving window to "acceptor"    //this.Close();}

Acceptor code:

?
12345678910111213141516171819202122 // The data structure required by the WM_COPYDATA messagepublic struct CopyDataStruct{    public IntPtr dwData;    public int cbData;     [MarshalAs(UnmanagedType.LPStr)]    public string lpData;} private const int WM_COPYDATA = 0x004A;// Message receiving Methodprotected 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();  // Display text information to the text box        //MessageBox.Show(cds.lpData);    }    base.WndProc(ref e);}

After compilation, find the Directory :~ Run the exe file under \ bin \ Debug. Effect:

The above code is just a demo to illustrate the problem. We can encapsulate these methods into a class to facilitate reuse.

In general, the principle of this method is to use the FindWindow function to find the process of the other party through the form title, and then obtain the window

Handle, and then send the message to the receiver through the SendMessage function in the DLL library, so that the direct communication of the program is completed.

Other methods can also be implemented. Please contact us.

Related Article

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.