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

消息发送者代码
本实例中创建窗体类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;
        }
    }
}

程序执行结果
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值