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;
        }
    }


你可能感兴趣的:(EXE之间通讯)