簡介:
本文著重講述了如果用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 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 );
}
#region windows form designer generated code
private void initializecomponent()
{
this.textbox1 = new system.windows.forms.textbox();
this.suspendlayout();
//
// textbox1
//
this.textbox1.location = new system.drawing.point
(176, 32);
this.textbox1.name = "textbox1";
this.textbox1.size = new system.drawing.size(160,
21);
this.textbox1.tabindex = 0;
this.textbox1.text = "textbox1";
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6,
14);
this.clientsize = new system.drawing.size(432, 266);
this.controls.addrange(new
system.windows.forms.control[] {
this.textbox1});
this.name = "form1";
this.text = "接收方";
this.resumelayout(false);
}
#endregion
[stathread]
static void main()
{
application.run(new form1());
}
protected override void defwndproc(ref
system.windows.forms.message m)
{
switch(m.msg)
{
//接收自定義消息 user,并顯示其參數
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;
}
}
}
[structlayout(layoutkind.sequential)]
public struct copydatastruct
{
public intptr dwdata;
public int cbdata;
[marshalas(unmanagedtype.lpstr)] public string lpdata;
}
}
//---------------------------------------------------
//接受方
//---------------------------------------------------
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.runtime.interopservices;
namespace 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 );
}
#region windows form designer generated code
private void initializecomponent()
{
this.textbox1 = new system.windows.forms.textbox();
this.suspendlayout();
//
// textbox1
//
this.textbox1.location = new system.drawing.point
(176, 32);
this.textbox1.name = "textbox1";
this.textbox1.size = new system.drawing.size(160,
21);
this.textbox1.tabindex = 0;
this.textbox1.text = "textbox1";
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6,
14);
this.clientsize = new system.drawing.size(432, 266);
this.controls.addrange(new
system.windows.forms.control[] {
this.textbox1});
this.name = "form1";
this.text = "接收方";
this.resumelayout(false);
}
#endregion
[stathread]
static void main()
{
application.run(new form1());
}
protected override void defwndproc(ref
system.windows.forms.message m)
{
switch(m.msg)
{
//接收自定義消息 user,并顯示其參數
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;
}
}
}
[structlayout(layoutkind.sequential)]
public struct copydatastruct
{
public intptr dwdata;
public int cbdata;
[marshalas(unmanagedtype.lpstr)] public string lpdata;
}
}
新聞熱點
疑難解答