Click here to Skip to main content
Licence CPOL
First Posted 11 Jul 2007
Views 31,041
Downloads 1,068
Bookmarked 38 times

Inter-Process Communication with C#

By | 11 Jul 2007 | Article
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)

About the Author

tran manh tuan

Web Developer

Vietnam Vietnam

Member



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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWindows 7 Pinmemberiamfromk13hrs 37mins ago 
GeneralOne little Update PinmemberLukas Holota22:51 27 Feb '09  
GeneralJust what I needed PinmemberDaniel_Hochee15:55 3 Feb '09  
Generalvery useful PinmemberPriyank Bolia7:51 3 Feb '09  
Generalusing winrar replace winzip Pinmembertran manh tuan17:27 7 Dec '08  
GeneralThe zip file is bad PinmemberDavid Thielen17:37 20 Oct '08  
GeneralExcellent (Y) Pinmemberi-henry6:35 14 Mar '08  
GeneralExcelent work... Pinmembertarantula31:08 22 Sep '07  
GeneralExcellent use of WM_COPYDATA in .NET! PinmemberDigiOz Multimedia17:04 8 Sep '07  
GeneralIn .NET we have Remoting Pinmemberleppie3:36 12 Jul '07  
GeneralRe: In .NET we have Remoting PinmemberDaniel_Hochee16:00 3 Feb '09  

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.

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