進程|資料
簡介:
本文著重講述了如果用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;
}
}