控制台程序使用SendMessage进行进程间的通信

消息发送者代码 本实例中创建窗体类ProxyForm,负责发送和接收数据。 Main方法代码

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleSender{class Program{static void Main(string[] args){ProxyForm proxy = new ProxyForm();Console.WriteLine(“Sender:美女,晚上出去玩玩吧!”);proxy.SendMessage(“Sender:美女,晚上出去玩玩吧!”);Console.WriteLine(“按下任意键退出程序!”);Console.ReadKey();}}}

窗体类ProxyForm代码

using System;using System.Diagnostics;using System.Runtime.InteropServices;using System.Windows.Forms;namespace ConsoleSender{ProxyForm : Form{WM_COPYDATA = 0x004A;private IntPtr hWndDest = IntPtr.Zero;// handle to destination window[DllImport(“User32.dll”, EntryPoint = “SendMessage”)](IntPtr hWnd,// handle to destination windowint Msg,// messageint wParam,// first message parameterref CopyDataStruct lParam // second message parameter);public ProxyForm(){InitializeComponent();}(ref System.Windows.Forms.Message m){switch (m.Msg){case WM_COPYDATA:CopyDataStruct cds = new CopyDataStruct();cds = (CopyDataStruct)m.GetLParam(cds.GetType());Console.WriteLine(“Reciver:” + cds.lpData.ToString());base.DefWndProc(ref m);break;default:base.DefWndProc(ref m);break;}}(string msg){try{Process[] procs = Process.GetProcesses();foreach (Process p in procs){if (p.ProcessName.Equals(“ConsoleReciver”)){hWndDest = p.MainWindowHandle;}}CopyDataStruct cds;cds.dwData = (IntPtr)100;cds.lpData = msg;cds.cbData = (msg.Length + 1) * 2;return SendMessage(hWndDest, WM_COPYDATA, (int)this.Handle, ref cds);}catch (Exception ex){throw ex;}}}使用COPYDATASTRUCT来传递数据/// </summary>[StructLayout(LayoutKind.Sequential)]public struct CopyDataStruct{public IntPtr dwData;public int cbData;[MarshalAs(UnmanagedType.LPWStr)]public string lpData;}}

消息接收者代码

using System;using System.Runtime.InteropServices;using System.Windows.Forms;namespace ConsoleReciver{Form1 : Form{WM_COPYDATA = 0x004A;private IntPtr hWndDest = IntPtr.Zero;// handle to destination window[DllImport(“User32.dll”, EntryPoint = “SendMessage”)](IntPtr hWnd,// handle to destination windowint Msg,// messageint wParam,// first message parameterref CopyDataStruct lParam // second message parameter);public Form1(){InitializeComponent();}(ref System.Windows.Forms.Message m){switch (m.Msg){case WM_COPYDATA:CopyDataStruct recivedCds = new CopyDataStruct();recivedCds = (CopyDataStruct)m.GetLParam(recivedCds.GetType());hWndDest = m.WParam;CopyDataStruct sendCds = new CopyDataStruct();sendCds.dwData = (IntPtr)100;sendCds.lpData = “你发给我的信息我已经收到,内容是:” + recivedCds.lpData.ToString();sendCds.cbData = (sendCds.lpData.Length + 1) * 2;SendMessage(hWndDest, WM_COPYDATA, (int)this.Handle, ref sendCds);base.DefWndProc(ref m);break;default:base.DefWndProc(ref m);break;}}使用COPYDATASTRUCT来传递数据/// </summary>[StructLayout(LayoutKind.Sequential)]public struct CopyDataStruct{public IntPtr dwData;public int cbData;[MarshalAs(UnmanagedType.LPWStr)]public string lpData;}}}

程序执行结果

,蝙蝠黑暗中闯荡,树木默默的成长,蝴蝶破蛹后飞翔,

控制台程序使用SendMessage进行进程间的通信

相关文章:

你感兴趣的文章:

标签云: