Eclipse开发平台 Eclipse集成开发环境 Java技术参考 J2EE企业应用项目 J2EE开发框架整合应用
Visual Studio 2008 技术 Visual Studio 2008集成环境 LINQ SQL Server 2008数据库 Silverlight WCF WPF WWF
平台综合 开发平台技术动态 跨平台开发 软件信息技术与商业 国外媒体技术资讯
程讯网下载 精品代码 — J2EE应用下载 精品代码 — ASP.NET应用 常用J2EE开发框架 流行ASP.NET开发框架

搜索
Google
 
最新文章
对“职业生涯及规划”的一些想法与建议
 
Visual Studio 10将会怎样?
 
三十怎么了? 心态最重要!
 
访问Microsoft格式文件的Jakarta-POI API库的.NET版本
 
dashCommerce 3.X如何安装Microsoft SQL Server 2005 Express Edition的高级服务功能
 
.net 2.0运行时提示以下错误:authentication mode=Windows 解决方法如下
 
在浏览器中发贴子时实现自动添加贴子签名档的BHO对象设计
 
C#下用Browser Helper Object对象实现拦截IE浏览器的各项消息的IE插件
 
Build a Managed BHO and Plug into the Browser
 
在C#中用WM_COPYDATA消息来实现进程间通信的详细编码
 
BHO 浏览器辅助对象关联原理、编写流程
 
Oracle8i的卸载
 
Oracle9i图形工具OEM的简介
 
详细介绍整个Oracle9i软件的安装过程
 
C# 面向对象设计模式纵横谈 - 18、Iterator迭代器(行为型模式)
 
 
在C#中用WM_COPYDATA消息来实现进程间通信的详细编码 - .NET Framework编程 .NET开发
 
内容摘要 -


全文 -

在C#中用WM_COPYDATA消息来实现进程间通信的详细编码

 

进程之间通讯的几种方法:

在Windows程序中,各个进程之间常常需要交换数据,进行数据通讯。常用的方法有

使用内存映射文件

通过共享内存DLL共享内存

使用SendMessage向另一进程发送WM_COPYDATA消息

 

比起前两种的复杂实现来,WM_COPYDATA消息灵活简便


WM_COPYDATA消息的主要目的是允许在进程间传递只读数据。Windows在通过WM_COPYDATA消息传递期间,不提供继承同步方式。SDK文档推荐用户使用SendMessage函数,接受方在数据拷贝完成前不返回,这样发送方就不可能删除和修改数据:
这个函数的原型及其要用到的结构如下:
SendMessage(hwnd,WM_COPYDATA,wParam,lParam);
其中,WM_COPYDATA对应的十六进制数为0x004A
wParam设置为包含数据的窗口的句柄。lParam指向一个COPYDATASTRUCT的结构:
typedef struct tagCOPYDATASTRUCT{
DWORD dwData;//用户定义数据
DWORD cbData;//数据大小
PVOID lpData;//指向数据的指针
}COPYDATASTRUCT;
该结构用来定义用户数据。
具体过程如下:

首先,在发送方,用FindWindow找到接受方的句柄,然后向接受方发送WM_COPYDATA消息.
接受方在DefWndProc事件中,来处理这条消息.由于中文编码是两个字节,所以传递中文时候字节长度要搞清楚.
体代码如下:
//---------------------------------------------------
//发送方:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace WinFormSendMsg
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;
const int WM_COPYDATA = 0x004A;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(184, 24);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(128, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(344, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 32);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(536, 142);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.textBox1});
this.Name = "Form1";
this.Text = "发送方窗体";
this.ResumeLayout(false);
}
static void Main()
{
Application.Run(new Form1());
}
[DllImport("User32

9 7 3 1 2 4 8 :

 
相关内容
查找更多 ◇ .NET Framework编程 .NET开发