EXE之间通讯

而且还有好几种方法。1 发送消息传递 2 共享内存传递 3 使用COM进程外服务器 ...

这篇文章主要说明一下,如何利用发送消息使两个exe完成通信。其他两个方法感兴趣可以搜索下,都可以找到相应

发送端:

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        [StructLayout(LayoutKind.Sequential)]
        public struct CopyDataStruct
        {
            public IntPtr dwData;
            public int cbData;  // 字符串长度
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpData; // 字符串
        }
        public const int WM_COPYDATA = 0x004A;

            //通过窗口的标题来查找窗口的句柄
            [DllImport("User32.dll", EntryPoint = "FindWindow")]
            public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

             //在DLL库中的发送消息函数
            [DllImport("User32.dll", EntryPoint = "SendMessage")]
            private static extern int SendMessage(
                IntPtr hWnd,                   //目标窗体句柄
                int Msg,                       //WM_COPYDATA
                int wParam,                //自定义数值
                ref CopyDataStruct lParam             //传递消息的结构体,
            );


            public static void SendMessage(string windowName, string strMsg)
            {
                if (strMsg == null) return;
                IntPtr hwnd = FindWindow(null, windowName);
                if (hwnd != IntPtr.Zero)
                {
                    CopyDataStruct cds;
                    cds.dwData = IntPtr.Zero;
                    cds.lpData = strMsg;
                    //注意:长度为字节数
                    cds.cbData = System.Text.Encoding.Default.GetBytes(strMsg).Length + 1;
                    // 消息来源窗体
                    int fromWindowHandler = 0;
                    SendMessage(hwnd, WM_COPYDATA, fromWindowHandler, ref cds);
                }
            }
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                //将文本框中的值,发送给接收端
                string strUrl = txtSend.Text;
                SendMessage("接收端", strUrl);

            }
        }

接收端:

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        //WM_COPYDATA消息所要求的数据结构
        [StructLayout(LayoutKind.Sequential)]
        public struct CopyDataStruct
        {
            public IntPtr dwData; //窗口句柄
            public int cbData;  // 字符串长度
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpData; // 字符串
        }



        private const int WM_COPYDATA = 0x004A;
        //消息循环捕捉信息(Winform里是重载WndProc)
        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
            if (hwndSource != null)
            {
                IntPtr handle = hwndSource.Handle;
                hwndSource.AddHook(new HwndSourceHook(WndProc));
            }
        }

        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == WM_COPYDATA)
            {
                CopyDataStruct cds = (CopyDataStruct)System.Runtime.InteropServices.Marshal.PtrToStructure(lParam, typeof(CopyDataStruct));
                texAccept.Text = cds.lpData;
              
            }
            return hwnd;
        }
    }


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值