Blog

Send Message from one to another running application

Posted by Neeraj Pandey Blogs | Windows Forms C# Aug 30, 2011
Send and receive message from running two application.
Download Files: Send and Receive.rar
This is how to send message to running two different windows application using c#.

One application send message to another application using windows handle.

It will find all running application and then it will send message to particular windows that we are define in your project.

With the help of this dll we get handle of running windows

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, ref COPYDATASTRUCT lParam);

private const int WM_COPYDATA = 0x4A;

int hWnd;
public static int NewWnd;
[StructLayout(LayoutKind.Sequential)]
struct COPYDATASTRUCT
{
    public int dwData;
    public int cbData;
    public int lpData;
}

Receive Message  using code (Receive Form)

protected override void WndProc(ref Message m)
{
    switch (m.Msg)
    {
        case WM_COPYDATA:
            COPYDATASTRUCT CD = (COPYDATASTRUCT)m.GetLParam(typeof(COPYDATASTRUCT));
            byte[] B = new byte[CD.cbData];
            IntPtr lpData = new IntPtr(CD.lpData);
            Marshal.Copy(lpData, B, 0, CD.cbData);
            string strData = Encoding.Default.GetString(B);
            listBox1.Items.Add(strData);
            break;

        default:
            base.WndProc(ref m);
            break;
    }
}

Send and receive message from running two application

For demo application please download project.
comments
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts