控制台程序使用SendMessage进行进程间的通信

时间:2015-07-18 17:04:33   收藏:0   阅读:1142

消息发送者代码
本实例中创建窗体类ProxyForm,负责发送和接收数据。
Main方法代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleSender
{
    class Program
    {
        static void Main(string[] args)
        {
            ProxyForm proxy = new ProxyForm();
            Console.WriteLine("Sender:美女,晚上出去玩玩吧!");
            proxy.SendMessage("Sender:美女,晚上出去玩玩吧!");
            Console.WriteLine("按下任意键退出程序!");
            Console.ReadKey();
        }
    }
}

窗体类ProxyForm代码

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ConsoleSender
{
    public partial class ProxyForm : Form
    {
        private const int WM_COPYDATA = 0x004A;
        private IntPtr hWndDest = IntPtr.Zero;// handle to destination window

        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(
        IntPtr hWnd,              // handle to destination window
        int Msg,                  // message
        int wParam,               // first message parameter
        ref CopyDataStruct lParam // second message parameter
        );

        public ProxyForm()
        {
            InitializeComponent();
        }

        protected override void DefWndProc(ref System.Windows.Forms.Message m)
        {
            switch (m.Msg)
            {
                case WM_COPYDATA:
                    CopyDataStruct cds = new CopyDataStruct();
                    cds = (CopyDataStruct)m.GetLParam(cds.GetType());
                    Console.WriteLine("Reciver:" + cds.lpData.ToString());
                    base.DefWndProc(ref m);
                    break;

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

        public int SendMessage(string msg)
        {
            try
            {
                Process[] procs = Process.GetProcesses();
                foreach (Process p in procs)
                {
                    if (p.ProcessName.Equals("ConsoleReciver"))
                    {
                        hWndDest = p.MainWindowHandle;
                    }
                }
                CopyDataStruct cds;
                cds.dwData = (IntPtr)100;
                cds.lpData = msg;
                cds.cbData = (msg.Length + 1) * 2;
                return SendMessage(hWndDest, WM_COPYDATA, (int)this.Handle, ref cds);               
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    /// <summary>
    /// 使用COPYDATASTRUCT来传递数据
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct CopyDataStruct
    {
        public IntPtr dwData;
        public int cbData;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string lpData;
    }
}

消息接收者代码

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ConsoleReciver
{
    public partial class Form1 : Form
    {
        private const int WM_COPYDATA = 0x004A;

        private IntPtr hWndDest = IntPtr.Zero;// handle to destination window

        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(
        IntPtr hWnd,              // handle to destination window
        int Msg,                  // message
        int wParam,               // first message parameter
        ref CopyDataStruct lParam // second message parameter
        );

        public Form1()
        {
            InitializeComponent();
        }

        protected override void DefWndProc(ref System.Windows.Forms.Message m)
        {
            switch (m.Msg)
            {
                case WM_COPYDATA:

                    CopyDataStruct recivedCds = new CopyDataStruct();
                    recivedCds = (CopyDataStruct)m.GetLParam(recivedCds.GetType());
                    hWndDest = m.WParam;
                    CopyDataStruct sendCds = new CopyDataStruct();
                    sendCds.dwData = (IntPtr)100;
                    sendCds.lpData = "你发给我的信息我已经收到,内容是:" + recivedCds.lpData.ToString();
                    sendCds.cbData = (sendCds.lpData.Length + 1) * 2;

                    SendMessage(hWndDest, WM_COPYDATA, (int)this.Handle, ref sendCds);

                    base.DefWndProc(ref m);
                    break;

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

        /// <summary>
        /// 使用COPYDATASTRUCT来传递数据
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct CopyDataStruct
        {
            public IntPtr dwData;
            public int cbData;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string lpData;
        }
    }
}

程序执行结果
技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!