Click here to Skip to main content
Click here to Skip to main content

Inter-Process Communication with C#

By , 11 Jul 2007
 

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:

 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)

About the Author

tran manh tuan
Web Developer
Vietnam Vietnam
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionWindows 7memberiamfromk25 May '12 - 3:36 
AnswerRe: Windows 7memberMember 838424327 May '12 - 21:16 
GeneralOne little UpdatememberLukas Holota27 Feb '09 - 22:51 
GeneralJust what I neededmemberDaniel_Hochee3 Feb '09 - 15:55 
Generalvery usefulmemberPriyank Bolia3 Feb '09 - 7:51 
Generalusing winrar replace winzipmembertran manh tuan7 Dec '08 - 17:27 
GeneralThe zip file is badmemberDavid Thielen20 Oct '08 - 17:37 
GeneralExcellent (Y)memberi-henry14 Mar '08 - 6:35 
GeneralExcelent work...membertarantula322 Sep '07 - 1:08 
GeneralExcellent use of WM_COPYDATA in .NET!memberDigiOz Multimedia8 Sep '07 - 17:04 
GeneralIn .NET we have Remotingmemberleppie12 Jul '07 - 3:36 
GeneralRe: In .NET we have RemotingmemberDaniel_Hochee3 Feb '09 - 16:00 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 12 Jul 2007
Article Copyright 2007 by tran manh tuan
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid