C#进程间通信--API传递参数(SendMessage)
2016-03-11 16:05
761 查看
C#进程间通信--API传递参数(SendMessage)
我们不仅可以传递系统已经定义好的消息,还可以传递自定义的消息(只需要发送消息端和接收消息端对自定义的消息值统一即可)。下面的发送和接收端同时演示了系统消息和自定义的消息。消息统一采用4位16进制的数。
1.系统消息使用的是0x0100(WM_KEYDOWN);0x0000--0x0400是系统自定义的消息,其中0x0000为WM_NULL,0x0400为WM_USER。0x0400以后的数值我们可以作为自定义的消息值。(见附录)
2.自定义消息Message,代码如下:
点击(此处)折叠或打开
class Message
{
public const int USER = 0x0400;
public const int WM_TEST = USER + 101;
public const int WM_MSG = USER + 102;
}
3.接收端和发送端
接收端Form1:
点击(此处)折叠或打开
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int msg, uint wParam, uint lParam);
//[DllImport("MessageDLL.dll", EntryPoint = "StartSendMessage")]//MessageDLL找不到
//private static extern int StartSendMessage(IntPtr hWnd);
//自己触发自定义消息
private void button1_Click(object sender, EventArgs e)
{
SendMessage(this.Handle, Message.WM_TEST, 100, 200);
}
private void button2_Click(object sender, EventArgs e)
{
//StartSendMessage(this.Handle);
}
//响应和处理自定义消息
protected override void DefWndProc(ref System.Windows.Forms.Message m)
{
string message;
switch (m.Msg)
{
case Message.WM_TEST://处理消息
message = string.Format("收到从应用程序发出的消息!参数为:{0},
{1}", m.WParam, m.LParam);
MessageBox.Show(message);
break;
//case Message.WM_MSG:
// message = string.Format("收到从DLL发出的消息!参数为:{0}, {1}", m.WParam, m.LParam);
// MessageBox.Show(message);
// break;
default:
base.DefWndProc(ref m);
break;
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
this.label1.Text = e.KeyValue.ToString();
}
}
发送端Form2:button1启动接收端,button2关闭接收端,button3传递系统消息和自定义的消息。
点击(此处)折叠或打开
public partial class Form1 : Form
{
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
//[DllImport("MessageDLL.dll", EntryPoint = "StartSendMessage")]//MessageDLL找不到
//private static extern int StartSendMessage(IntPtr hWnd);
//internal string local = "this is a local internal";
public Form1()
{
InitializeComponent();
}
//ClassLibrary1.Class1 cla = new ClassLibrary1.Class1();
ProcessStartInfo startInfo = new ProcessStartInfo();
Process pro = new Process();
private void Form1_Load(object sender, EventArgs e)
{
startInfo.FileName = @"F:\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe";
pro.StartInfo = startInfo;
}
private void button1_Click(object sender, EventArgs e)
{
pro.Start();
}
private void button2_Click(object sender, EventArgs e)
{
pro.Kill();
}
private void button3_Click(object sender, EventArgs e)
{
IntPtr hWnd = pro.MainWindowHandle;
int data = Convert.ToInt32(this.textBox1.Text);
SendMessage(hWnd, 0x0100, data, 0);
SendMessage(hWnd, Message.WM_TEST, 300, 300);
}
4.界面显示:
点击button1:
左侧窗体为发送端,右侧窗体为接收端。
左侧窗体在文本框中输入数值,然后点击button2后,左右窗体显示如下:
4.关于消息驱动
现在VS上已经由事件驱动取代了消息驱动,不过像消息驱动还是在进程间通信常用的。此外本来试着通过控制台应用程序向窗体发送消息,没有发送成功(如果有高人解决了,麻烦告诉me一下下哈)。不过一般消息发送与处理都是窗口之间进行的。
5.附录:系统消息
C#下的SendMessage消息值.txt
http://blog.chinaunix.net/attachment/attach/26/79/05/512679055162e0ae58c2399f459bd9b30fd3494534.txt
6.链接资料网站
发送和接收消息:http://www.cnblogs.com/cdo/archive/2005/08/05/208128.html
SendMessage和PostMessage函数:http://blog.csdn.net/sbz0409/article/details/3772965
转载:http://blog.chinaunix.net/uid-26790551-id-3180355.html
我们不仅可以传递系统已经定义好的消息,还可以传递自定义的消息(只需要发送消息端和接收消息端对自定义的消息值统一即可)。下面的发送和接收端同时演示了系统消息和自定义的消息。消息统一采用4位16进制的数。
1.系统消息使用的是0x0100(WM_KEYDOWN);0x0000--0x0400是系统自定义的消息,其中0x0000为WM_NULL,0x0400为WM_USER。0x0400以后的数值我们可以作为自定义的消息值。(见附录)
2.自定义消息Message,代码如下:
点击(此处)折叠或打开
class Message
{
public const int USER = 0x0400;
public const int WM_TEST = USER + 101;
public const int WM_MSG = USER + 102;
}
3.接收端和发送端
接收端Form1:
点击(此处)折叠或打开
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int msg, uint wParam, uint lParam);
//[DllImport("MessageDLL.dll", EntryPoint = "StartSendMessage")]//MessageDLL找不到
//private static extern int StartSendMessage(IntPtr hWnd);
//自己触发自定义消息
private void button1_Click(object sender, EventArgs e)
{
SendMessage(this.Handle, Message.WM_TEST, 100, 200);
}
private void button2_Click(object sender, EventArgs e)
{
//StartSendMessage(this.Handle);
}
//响应和处理自定义消息
protected override void DefWndProc(ref System.Windows.Forms.Message m)
{
string message;
switch (m.Msg)
{
case Message.WM_TEST://处理消息
message = string.Format("收到从应用程序发出的消息!参数为:{0},
{1}", m.WParam, m.LParam);
MessageBox.Show(message);
break;
//case Message.WM_MSG:
// message = string.Format("收到从DLL发出的消息!参数为:{0}, {1}", m.WParam, m.LParam);
// MessageBox.Show(message);
// break;
default:
base.DefWndProc(ref m);
break;
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
this.label1.Text = e.KeyValue.ToString();
}
}
发送端Form2:button1启动接收端,button2关闭接收端,button3传递系统消息和自定义的消息。
点击(此处)折叠或打开
public partial class Form1 : Form
{
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
//[DllImport("MessageDLL.dll", EntryPoint = "StartSendMessage")]//MessageDLL找不到
//private static extern int StartSendMessage(IntPtr hWnd);
//internal string local = "this is a local internal";
public Form1()
{
InitializeComponent();
}
//ClassLibrary1.Class1 cla = new ClassLibrary1.Class1();
ProcessStartInfo startInfo = new ProcessStartInfo();
Process pro = new Process();
private void Form1_Load(object sender, EventArgs e)
{
startInfo.FileName = @"F:\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe";
pro.StartInfo = startInfo;
}
private void button1_Click(object sender, EventArgs e)
{
pro.Start();
}
private void button2_Click(object sender, EventArgs e)
{
pro.Kill();
}
private void button3_Click(object sender, EventArgs e)
{
IntPtr hWnd = pro.MainWindowHandle;
int data = Convert.ToInt32(this.textBox1.Text);
SendMessage(hWnd, 0x0100, data, 0);
SendMessage(hWnd, Message.WM_TEST, 300, 300);
}
4.界面显示:
点击button1:
左侧窗体为发送端,右侧窗体为接收端。
左侧窗体在文本框中输入数值,然后点击button2后,左右窗体显示如下:
4.关于消息驱动
现在VS上已经由事件驱动取代了消息驱动,不过像消息驱动还是在进程间通信常用的。此外本来试着通过控制台应用程序向窗体发送消息,没有发送成功(如果有高人解决了,麻烦告诉me一下下哈)。不过一般消息发送与处理都是窗口之间进行的。
5.附录:系统消息
C#下的SendMessage消息值.txt
http://blog.chinaunix.net/attachment/attach/26/79/05/512679055162e0ae58c2399f459bd9b30fd3494534.txt
6.链接资料网站
发送和接收消息:http://www.cnblogs.com/cdo/archive/2005/08/05/208128.html
SendMessage和PostMessage函数:http://blog.csdn.net/sbz0409/article/details/3772965
转载:http://blog.chinaunix.net/uid-26790551-id-3180355.html
相关文章推荐
- c# 进程间同步实现 进程之间通讯的几种方法
- C#机房重构——存储过程
- C#综合揭秘——细说进程、应用程序域与上下文之间的关系
- C#:通过串口读取扫描枪里的值
- C# 程序集 和 反射
- C#中的Dictionary字典类介绍
- C# 数据类型及转换
- C# 输入输出
- C#多线程编程
- 【笔记】《C#大学教程》- 第9章 面向对象编程:继承
- 解决c#,wpf程序带环境安装包体积太大问题
- c# chroma key 绿被抠像
- C# 中的委托和事件
- C#:String.Format数字格式化输出 {0:N2} {0:D2} {0:C2}...
- C# 发送邮件实例代码
- C#怎样用文件读写在文件的原有基础上追加一行数据
- [转]C# ListView 单击标题实现排序(在转载的基础上有所完善)
- [转]解决C# WinForm 中 VSHOST.EXE 程序不关闭的问题
- [转]C#使用Window图片查看器打开图片
- C#第九节课