c#进程间通信(Inter-Process Communication)
c#进程间通信(IPC, Inter-Process Communication)
接收端:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; //for MarshalAs namespace receive { public partial class Receive : Form { public Receive() { InitializeComponent(); } protected override void DefWndProc(ref System.Windows.Forms.Message m) //重构此函数接收数据 { switch (m.Msg) { case Message.WM_TEST: //确定类型后,处理数据 string message = string.Format("收到消息!参数为:{0},{1},{2}", m.Msg, m.WParam, m.LParam); label1.Text = message; break; default: base.DefWndProc(ref m); break; } } } class Message { public const int USER = 0X0400; public const int WM_TEST = USER + 101; public const int WM_MSG = USER + 102; } }
发送端:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; //for DllImport namespace send { public partial class Form1 : Form { [DllImport("User32.dll", EntryPoint = "SendMessage")] private static extern int SendMessage(int hWnd, int msg, int wParam, int lParam); //发送消息 [DllImport("User32.dll", EntryPoint = "FindWindow")] private static extern int FindWindow(string lpClassName, string lpWindowName); //获取另一个进程的窗口句柄 public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int hWnd = FindWindow(null, @"Receive"); SendMessage(hWnd, Message.WM_TEST, 99, 100);//参数1、窗口句柄。2 、消息类型。 3、数据1。 4、数据2 } } class Message { public const int USER = 0X0400; public const int WM_TEST = USER + 101; //简单的数据传输类型,传两个整数。 public const int WM_MSG = USER + 102; } }
比较复杂的字符串传输:
发送端:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; //for DllImport namespace send { public partial class Form1 : Form { [DllImport("User32.dll", EntryPoint = "SendMessage")] // private static extern int SendMessage(int hWnd, int msg, int wParam, int lParam); //发送消息 private static extern int SendMessage(int hWnd, int msg, int wParam, ref COPYDATASTRUCT lParam); [DllImport("User32.dll", EntryPoint = "FindWindow")] private static extern int FindWindow(string lpClassName, string lpWindowName); //获取另一个进程的窗口句柄 const int WM_COPYDATA = 0x004A; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int hWnd = FindWindow(null, @"Receive"); string message = "你好消息成功发送!"; int i = message.Length; byte[] sarr = System.Text.Encoding.Default.GetBytes(message); COPYDATASTRUCT cds; cds.dwData = (IntPtr)100; cds.lpData = message; cds.cbData = sarr.Length+1; //此值错误会引发接收端崩溃 SendMessage(hWnd, WM_COPYDATA, 0, ref cds); } } public struct COPYDATASTRUCT { public IntPtr dwData; public int cbData; [MarshalAs(UnmanagedType.LPStr)] public string lpData; } }
接收端:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; //for MarshalAs namespace receive { public partial class Receive : Form { const int WM_COPYDATA = 0x004A; const int WM_MYSYMPLE = 0x005A; public Receive() { InitializeComponent(); } protected override void DefWndProc(ref System.Windows.Forms.Message m) { switch (m.Msg) { case WM_COPYDATA: COPYDATASTRUCT myStr = new COPYDATASTRUCT(); Type myType = myStr.GetType(); myStr = (COPYDATASTRUCT)m.GetLParam(myType); //m中获取LParam参数以myType类型的方式,让后转换问结构体。 label1.Text = myStr.lpData; break; default: base.DefWndProc(ref m); break; } } } public struct COPYDATASTRUCT { public IntPtr dwData; public int cbData; [MarshalAs(UnmanagedType.LPStr)] public string lpData; } }
下载:链接:http://pan.baidu.com/s/1c0lWpt2 密码:yvzz
其他方法参考:
http://blog.csdn.net/feiren127/article/details/5459827
附:在程序中使用代码启动程序
using System.Diagnostics; // for ProcessStartInfo ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = @"C:\Users\dell\Documents\Visual Studio 2010\Projects\receive\receive\bin\Debug\receive.exe"; Process pro = new Process(); pro.StartInfo = startInfo; pro.Start(); //开启 IntPtr hWnd = pro.MainWindowHandle; //进程的句柄 pro.Kill(); //关闭
参考: http://blog.csdn.net/u011000290/article/details/48108557