C#进程间通信--API传递参数(SendMessage)

时间:2022-08-28 20:49:00
注:本文为个人学习摘录,原文地址: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,代码如下:
 

点击(此处)折叠或打开

  1. class Message
  2.     {
  3.         public const int USER = 0x0400;
  4.         public const int WM_TEST = USER + 101;
  5.         public const int WM_MSG = USER + 102;
  6.     }

3.接收端和发送端
接收端Form1:
 

点击(此处)折叠或打开

  1. public partial class Form1 : Form
  2.     {
  3.         public Form1()
  4.         {
  5.             InitializeComponent();
  6.         }
  7.         [DllImport("User32.dll", EntryPoint = "SendMessage")]
  8.         private static extern int SendMessage(IntPtr hWnd, int msg, uint wParam, uint lParam);
  9.         //[DllImport("MessageDLL.dll", EntryPoint = "StartSendMessage")]//MessageDLL找不到
  10.         //private static extern int StartSendMessage(IntPtr hWnd);
  11.         //自己触发自定义消息
  12.         private void button1_Click(object sender, EventArgs e)
  13.         {
  14.             SendMessage(this.Handle, Message.WM_TEST, 100, 200);
  15.         }
  16.         private void button2_Click(object sender, EventArgs e)
  17.         {
  18.             //StartSendMessage(this.Handle);
  19.         }
  20.         //响应和处理自定义消息
  21.         protected override void DefWndProc(ref System.Windows.Forms.Message m)
  22.         {
  23.             string message;
  24.             switch (m.Msg)
  25.             {
  26.                 case Message.WM_TEST://处理消息
  27.                     message = string.Format("收到从应用程序发出的消息!参数为:{0}, {1}", m.WParam, m.LParam);
  28.                     MessageBox.Show(message);
  29.                     break;
  30.                 //case Message.WM_MSG:
  31.                 // message = string.Format("收到从DLL发出的消息!参数为:{0}, {1}", m.WParam, m.LParam);
  32.                 // MessageBox.Show(message);
  33.                 // break;
  34.                 default:
  35.                     base.DefWndProc(ref m);
  36.                     break;
  37.             }
  38.         }
  39.         private void Form1_KeyDown(object sender, KeyEventArgs e)
  40.         {
  41.             this.label1.Text = e.KeyValue.ToString();
  42.         }
  43.     }

发送端Form2:button1启动接收端,button2关闭接收端,button3传递系统消息和自定义的消息。
 

点击(此处)折叠或打开

  1. public partial class Form1 : Form
  2.     {
  3.         [DllImport("User32.dll", EntryPoint = "SendMessage")]
  4.         private static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
  5.         //[DllImport("MessageDLL.dll", EntryPoint = "StartSendMessage")]//MessageDLL找不到
  6.         //private static extern int StartSendMessage(IntPtr hWnd);
  7.         //internal string local = "this is a local internal";
  8.         public Form1()
  9.         {
  10.             InitializeComponent();
  11.         }
  12.         //ClassLibrary1.Class1 cla = new ClassLibrary1.Class1();
  13.         ProcessStartInfo startInfo = new ProcessStartInfo();
  14.         Process pro = new Process();
  15.         private void Form1_Load(object sender, EventArgs e)
  16.         {
  17.             startInfo.FileName = @"F:\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe";
  18.             pro.StartInfo = startInfo;
  19.         }
  20.         private void button1_Click(object sender, EventArgs e)
  21.         {
  22.             pro.Start();
  23.         }
  24.         private void button2_Click(object sender, EventArgs e)
  25.         {
  26.             pro.Kill();
  27.         }
  28.         private void button3_Click(object sender, EventArgs e)
  29.         {
  30.             IntPtr hWnd = pro.MainWindowHandle;
  31.             int data = Convert.ToInt32(this.textBox1.Text);
  32.             SendMessage(hWnd, 0x0100, data, 0);
  33.             SendMessage(hWnd, Message.WM_TEST, 300, 300);
  34. }

4.界面显示:
点击button1:
C#进程间通信--API传递参数(SendMessage) C#进程间通信--API传递参数(SendMessage)
 
左侧窗体为发送端,右侧窗体为接收端。
左侧窗体在文本框中输入数值,然后点击button2后,左右窗体显示如下:
 
C#进程间通信--API传递参数(SendMessage)C#进程间通信--API传递参数(SendMessage)
 
4.关于消息驱动
现在VS上已经由事件驱动取代了消息驱动,不过像消息驱动还是在进程间通信常用的。此外 本来试着通过控制台应用程序向窗体发送消息,没有发送成功(如果有高人解决了,麻烦告诉me一下下哈)。不过一般消息发送与处理都是窗口之间进行的。
5.附录:系统消息
 
6.链接资料网站
SendMessage和PostMessage函数:http://blog.csdn.net/sbz0409/article/details/3772965