Click here to Skip to main content
13,550,829 members
Click here to Skip to main content
Add your own
alternative version


46 bookmarked
Posted 11 Jul 2007
Licenced CPOL

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.


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();
                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);

            return true;
protected override void WndProc(ref Message m){
                case Win32.WM_COPYDATA:
                    Win32.CopyDataStruct st =
                    string strData = Marshal.PtrToStringUni(st.lpData);
                    txtmessagereceive.Text = strData;

                    // let the base class deal with it
                    base.WndProc(ref m);


  • 12th July, 2007: Initial post


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
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionProblems May Faced with Prior Versions after Windows XP Pin
samaun20-Aug-14 20:26
membersamaun20-Aug-14 20:26 
QuestionWindows 7 Pin
iamfromk25-May-12 3:36
memberiamfromk25-May-12 3:36 
AnswerRe: Windows 7 Pin
Member 838424327-May-12 21:16
memberMember 838424327-May-12 21:16 
GeneralOne little Update Pin
Lukas Holota27-Feb-09 22:51
memberLukas Holota27-Feb-09 22:51 
GeneralJust what I needed Pin
Daniel_Hochee3-Feb-09 15:55
memberDaniel_Hochee3-Feb-09 15:55 
Generalvery useful Pin
Priyank Bolia3-Feb-09 7:51
memberPriyank Bolia3-Feb-09 7:51 
Generalusing winrar replace winzip Pin
tran manh tuan7-Dec-08 17:27
membertran manh tuan7-Dec-08 17:27 
GeneralThe zip file is bad Pin
David Thielen20-Oct-08 17:37
memberDavid Thielen20-Oct-08 17:37 
GeneralExcellent (Y) Pin
i-henry14-Mar-08 6:35
memberi-henry14-Mar-08 6:35 
GeneralExcelent work... Pin
tarantula322-Sep-07 1:08
membertarantula322-Sep-07 1:08 
GeneralExcellent use of WM_COPYDATA in .NET! Pin
DigiOz Multimedia8-Sep-07 17:04
memberDigiOz Multimedia8-Sep-07 17:04 
GeneralIn .NET we have Remoting Pin
leppie12-Jul-07 3:36
memberleppie12-Jul-07 3:36 
GeneralRe: In .NET we have Remoting Pin
Daniel_Hochee3-Feb-09 16:00
memberDaniel_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.

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