Send Message from one to another running application

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.
Ebook Download
View all
Learn
View all