#region 进程发送消息 public const int WM_COPYDATA = 0x004A; [DllImport("User32.dll", EntryPoint = "FindWindow")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); /// <summary> /// 定义用户要传递的消息的数据 /// </summary> [StructLayout(LayoutKind.Sequential)] public struct CopyDataStruct { public IntPtr dwData; public int cbData; //字符串长度 [MarshalAs(UnmanagedType.LPStr)] public string lpData; //字符串 } [DllImport("User32.dll", EntryPoint = "SendMessage")] private static extern int SendMessage( IntPtr hWnd, //目标窗体句柄 int Msg, //WM_COPYDATA int wParam, //自定义数值 ref CopyDataStruct lParam //结构体 ); /// <summary> /// 发送消息 /// </summary> /// <param name="windowName">window的title,建议加上GUID,不会重复</param> /// <param name="strMsg">要发送的字符串</param> public static void SendMessage(string windowName, string strMsg) { if (strMsg == null) return; IntPtr hwnd = FindWindow(null, windowName); //MessageBox.Show("接收消息窗口句柄:" + ((int)hwnd).ToString()); 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); } } #endregion