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

Inter-Process Communication with C#

, 11 Jul 2007
Rate this:
Please Sign up or sign in to vote.
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:

 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)

Share

About the Author

tran manh tuan
Web Developer
Vietnam Vietnam
No Biography provided

Comments and Discussions

 
QuestionProblems May Faced with Prior Versions after Windows XP Pinmembersamaun20-Aug-14 20:26 
QuestionWindows 7 Pinmemberiamfromk25-May-12 3:36 
AnswerRe: Windows 7 PinmemberMember 838424327-May-12 21:16 
GeneralOne little Update PinmemberLukas Holota27-Feb-09 22:51 
GeneralJust what I needed PinmemberDaniel_Hochee3-Feb-09 15:55 
Generalvery useful PinmemberPriyank Bolia3-Feb-09 7:51 
Generalusing winrar replace winzip Pinmembertran manh tuan7-Dec-08 17:27 
GeneralThe zip file is bad PinmemberDavid Thielen20-Oct-08 17:37 
GeneralExcellent (Y) Pinmemberi-henry14-Mar-08 6:35 
GeneralExcelent work... Pinmembertarantula322-Sep-07 1:08 
GeneralExcellent use of WM_COPYDATA in .NET! PinmemberDigiOz Multimedia8-Sep-07 17:04 
GeneralIn .NET we have Remoting Pinmemberleppie12-Jul-07 3:36 
GeneralRe: In .NET we have Remoting PinmemberDaniel_Hochee3-Feb-09 16:00 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 12 Jul 2007
Article Copyright 2007 by tran manh tuan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid