C# 跨进程通信

跨进程间通信

发送方

 1     public const int WM_InsertChart_Completed = 0x00AA;
 2 
 3     //查找窗口
 4     [DllImport("User32.dll", EntryPoint = "FindWindow")]
 5     public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
 6 
 7     //发送信息
 8     [DllImport("User32.dll", EntryPoint = "SendMessage")]
 9     public static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
10 
11     private void PostMessage()
12     {
13         var procs = Process.GetProcessesByName("MyExample");
14 
15         foreach (var process in procs)
16         {
17             SendMessage(process.MainWindowHandle, WM_InsertChart_Completed, IntPtr.Zero, IntPtr.Zero);
18         }
19     }

还有一个PostMessage方法,和SendMessage类似。

接收方

在winform中,不同进程间窗口通信

1 protected override void DefWndProc(ref Message m)
2 {
3     ......
4 }

在WPF中,如何在俩个不同进程的窗口之间通信.

 1     protected override void OnSourceInitialized(EventArgs e)
 2     {
 3         base.OnSourceInitialized(e);
 4         var hwndSource = PresentationSource.FromVisual(this) as HwndSource;
 5         hwndSource?.AddHook(new HwndSourceHook(WndProc));
 6     }
 7     public const int WM_InsertChart_Completed = 0x00AA;
 8     public static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
 9     {
10         if (msg == WM_InsertChart_Completed )
11         {
12                       .............
13         }
14 
15         return hwnd;
16     }

传递具体的数据,可参考:http://www.360doc.com/content/18/0131/15/45112790_726705014.shtml

猜你喜欢

转载自www.cnblogs.com/kybs0/p/9234601.html