转:http://www.cppblog.com/mydriverc/articles/28732.html
进程之间通讯的几种方法:
在Windows程序中,各个进程之间常常需要交换数据,进行数据通讯。常用的方法有
使用内存映射文件
通过共享内存DLL共享内存
使用SendMessage向另一进程发送WM_COPYDATA消息
比起前两种的复杂实现来,WM_COPYDATA消息无疑是一种经济实惠的一中方法.(ZT)

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事件中,来处理这条消息.由于中文编码是两个字节,所以传递中文时候字节长度要搞清楚.
代码如下:

 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(18424);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(12821);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(34416);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(11232);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(614);
this.ClientSize = new System.Drawing.Size(536142);
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.dll",EntryPoint="SendMessage")]
private static extern int SendMessage(
int hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
ref COPYDATASTRUCT lParam // second message parameter
);

[DllImport(
"User32.dll",EntryPoint="FindWindow")]
private static extern int FindWindow(string lpClassName,string
lpWindowName);

private void button1_Click(object sender, System.EventArgs e)
{
int WINDOW_HANDLER = FindWindow(null,@"接收方窗体");
if(WINDOW_HANDLER == 0)
{
}
else
{
byte[] sarr = System.Text.Encoding.Default.GetBytes(this.textBox1.Text);
int len = sarr.Length;
COPYDATASTRUCT cds;
cds.dwData 
= (IntPtr) 100;
cds.lpData 
= this.textBox1.Text;
cds.cbData 
= len + 1;
SendMessage(WINDOW_HANDLER, WM_COPYDATA, 
0ref cds);

}
}
}
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)] 
public string lpData;
}

}

 

 WindowsFormGetMsg
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
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.SuspendLayout();
// 
// textBox1
// 
this.textBox1.Location = new System.Drawing.Point(17632);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(16021);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(614);
this.ClientSize = new System.Drawing.Size(432266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1});
this.Name = "Form1";
this.Text = "接收方窗体";
this.ResumeLayout(false);
}
static void Main() 
{
Application.Run(
new Form1());
}
protected override void DefWndProc(ref System.Windows.Forms.Message m)
{
switch(m.Msg)
{
case WM_COPYDATA:
COPYDATASTRUCT mystr 
= new COPYDATASTRUCT();
Type mytype 
= mystr.GetType();
mystr 
=(COPYDATASTRUCT)m.GetLParam(mytype);
this.textBox1.Text =mystr.lpData;
break;
default:
base.DefWndProc(ref m);
break;
}
}
}
public struct COPYDATASTRUCT
{
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)] 
public string lpData;
}
}

若需要传递二进制数据,可使用Marshal.AllocHGlobal分配非托管内存,Marshal.Copy拷贝数据发送.代码如下:
byte[] data=在C#中用WM_COPYDATA消息来实现进程间通信;
COPYDATASTRUCT cds;

cds.cbData 
= data.Length;
cds.lpData 
= Marshal.AllocHGlobal(data.Length);
Marshal.Copy(data,
0,cds.lpData,data.Length);

接收消息代码如下:

byte[] bt = new byte[cds.cbData]; 
Marshal.Copy(cds.lpData,bt,
0,bt.Length);

详情参阅:http://www.cnblogs.com/jiangyh-is-me/archive/2006/06/05/417381.html

 

相关文章:

  • 2022-12-23
  • 2021-09-27
  • 2022-12-23
  • 2022-12-23
  • 2021-09-07
  • 2021-11-05
  • 2021-10-11
  • 2021-07-28
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
  • 2021-09-19
  • 2022-02-24
  • 2021-06-11
  • 2022-03-08
相关资源
相似解决方案