Click here to Skip to main content
15,867,890 members
Articles / Desktop Programming / Windows Forms
Article

Inter-Process Communication with C#

Rate me:
Please Sign up or sign in to vote.
3.51/5 (20 votes)
11 Jul 2007CPOL 109.1K   5.1K   49   13
This app shows how to send and receive messages between apps using WM_COPYDATA.

Introduction

This app shows how to send and receive messages between two apps using WM_COPYDATA.

There are two samples. One is testmessage app and one is testMessage2 app. The testmessage will check and open app 2 if it is not running and if so, will start another instance with a different window header.

  • The 'Send' button will send the text from the textbox to the testMessage2 app.
  • The received data will show up in the textbox.

Using the Code

The exchange of data is performed by finding the other application (using FindWindow) and sending a WM_COPYDATA message to that window:

C#
 public static bool SendArgs(IntPtr targetHWnd, string args)
        {
            Win32.CopyDataStruct cds = new Win32.CopyDataStruct();
            try
            {
                cds.cbData = (args.Length + 1) * 2;
                cds.lpData = Win32.LocalAlloc(0x40, cds.cbData);
                Marshal.Copy(args.ToCharArray(), 0, cds.lpData, args.Length);
                cds.dwData = (IntPtr)1;
                Win32.SendMessage(targetHWnd, Win32.WM_COPYDATA, IntPtr.Zero, ref cds);
            }
            finally
            {
                cds.Dispose();
            }

            return true;
        } 
protected override void WndProc(ref Message m){
            switch(m.Msg){
                case Win32.WM_COPYDATA:
                    Win32.CopyDataStruct st =
            	  (Win32.CopyDataStruct)Marshal.PtrToStructure(m.LParam,             
		  typeof(Win32.CopyDataStruct));
                    string strData = Marshal.PtrToStringUni(st.lpData);
                    txtmessagereceive.Text = strData;
                    break;

                default:
                    // let the base class deal with it
                    base.WndProc(ref m);
                    break;
            }
        }

History

  • 12th July, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Vietnam Vietnam
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionProblems May Faced with Prior Versions after Windows XP Pin
samaun20-Aug-14 20:26
samaun20-Aug-14 20:26 
QuestionWindows 7 Pin
iamfromk25-May-12 3:36
iamfromk25-May-12 3:36 
AnswerRe: Windows 7 Pin
Member 838424327-May-12 21:16
Member 838424327-May-12 21:16 
GeneralOne little Update Pin
Lukas Holota27-Feb-09 22:51
Lukas Holota27-Feb-09 22:51 
GeneralJust what I needed Pin
Daniel_Hochee3-Feb-09 15:55
Daniel_Hochee3-Feb-09 15:55 
Generalvery useful Pin
Priyank Bolia3-Feb-09 7:51
Priyank Bolia3-Feb-09 7:51 
Generalusing winrar replace winzip Pin
tran manh tuan7-Dec-08 17:27
tran manh tuan7-Dec-08 17:27 
GeneralThe zip file is bad Pin
David Thielen20-Oct-08 17:37
David Thielen20-Oct-08 17:37 
If anyone has a good copy, please upload.

thanks - dave
GeneralExcellent (Y) Pin
iHenry Smith14-Mar-08 6:35
iHenry Smith14-Mar-08 6:35 
GeneralExcelent work... Pin
tarantula322-Sep-07 1:08
tarantula322-Sep-07 1:08 
GeneralExcellent use of WM_COPYDATA in .NET! Pin
DigiOz Multimedia8-Sep-07 17:04
DigiOz Multimedia8-Sep-07 17:04 
GeneralIn .NET we have Remoting Pin
leppie12-Jul-07 3:36
leppie12-Jul-07 3:36 
GeneralRe: In .NET we have Remoting Pin
Daniel_Hochee3-Feb-09 16:00
Daniel_Hochee3-Feb-09 16:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.