控制台程序使用SendMessage进行进程间的通信
来源:互联网 发布:太原师范学院教务网络 编辑:程序博客网 时间:2024/05/22 17:49
消息发送者代码
本实例中创建窗体类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{ public partial class ProxyForm : Form { private const int WM_COPYDATA = 0x004A; private IntPtr hWndDest = IntPtr.Zero;// handle to destination window [DllImport("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage( IntPtr hWnd, // handle to destination window int Msg, // message int wParam, // first message parameter ref CopyDataStruct lParam // second message parameter ); public ProxyForm() { InitializeComponent(); } protected override void DefWndProc(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; } } public int SendMessage(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; } } } /// <summary> /// 使用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{ public partial class Form1 : Form { private const int WM_COPYDATA = 0x004A; private IntPtr hWndDest = IntPtr.Zero;// handle to destination window [DllImport("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage( IntPtr hWnd, // handle to destination window int Msg, // message int wParam, // first message parameter ref CopyDataStruct lParam // second message parameter ); public Form1() { InitializeComponent(); } protected override void DefWndProc(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; } } /// <summary> /// 使用COPYDATASTRUCT来传递数据 /// </summary> [StructLayout(LayoutKind.Sequential)] public struct CopyDataStruct { public IntPtr dwData; public int cbData; [MarshalAs(UnmanagedType.LPWStr)] public string lpData; } }}
程序执行结果
0 0
- 控制台程序使用SendMessage进行进程间的通信
- C#中使用SendMessage进行进程通信的实例
- windows使用消息进行控制台进程间通信
- C#中使用SendMessage进行进程通信,可以发送字符串。
- 控制台程序进程间WM_COPYDATA通信
- MFC使用SendMessage()发送自定义消息实现进程间通信
- MFC使用SendMessage()发送自定义消息实现进程间通信
- MFC使用SendMessage()发送自定义消息实现进程间通信
- MFC使用SendMessage()发送自定义消息实现进程间通信
- MFC使用SendMessage发送自定义消息实现进程间通信
- MFC使用SendMessage()发送自定义消息实现进程间通信
- C#基于sendmessage方法的进程间通信
- SendMessage进程间简单通信问题
- SendMessage进程间简单通信问题
- 利用SendMessage实现C#进程间通信
- SendMessage进程间简单通信问题
- 利用SendMessage实现C#进程间通信
- 利用SendMessage实现C#进程间通信
- nyoj915*+-字符串
- 南阳oj 动态规划 心急的c小加 题目236
- 内部类的使用
- 【bzoj2435】 NOI2011 道路修建 水题
- C++ protected等属性及继承之间的关系
- 控制台程序使用SendMessage进行进程间的通信
- 将一个整形数据转化为对应的字符串如1234 -> "1234"(可以用库函数,也可以写算法)
- 责任链模式 - 行为模式
- 关于java.security.MessageDigest类
- 解决MyEclipse jdk 8 报错问题
- 文章标题
- 发布高品质软件的系统方法
- leetcode: Summary Ranges
- 2015年ALPC暑期专题练习I (计算几何) A