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

进程之间通讯的几种方法:<br>在Windows程序中,各个进程之间常常需要交换数据,进行数据通讯。常用的方法有<br>使用内存映射文件<br>通过共享内存DLL共享内存<br>使用SendMessage向另一进程发送WM_COPYDATA消息<br>比起前两种的复杂实现来,WM_COPYDATA消息无疑是一种经济实惠的一中方法.(ZT)<br><p>WM_COPYDATA消息的主要目的是允许在进程间传递只读数据。Windows在通过WM_COPYDATA消息传递期间,不提供继承同步方式。SDK文档推荐用户使用SendMessage函数,接受方在数据拷贝完成前不返回,这样发送方就不可能删除和修改数据:<br>这个函数的原型及其要用到的结构如下:<br>SendMessage(hwnd,WM_COPYDATA,wParam,lParam); <br>其中,WM_COPYDATA对应的十六进制数为0x004A<br>wParam设置为包含数据的窗口的句柄。lParam指向一个COPYDATASTRUCT的结构:<br>typedef struct tagCOPYDATASTRUCT{<br>DWORD dwData;//用户定义数据<br>DWORD cbData;//数据大小<br>PVOID lpData;//指向数据的指针<br>}COPYDATASTRUCT;<br>该结构用来定义用户数据。<br>具体过程如下:</p>
<p>首先,在发送方,用FindWindow找到接受方的句柄,然后向接受方发送WM_COPYDATA消息.<br>接受方在DefWndProc事件中,来处理这条消息.由于中文编码是两个字节,所以传递中文时候字节长度要搞清楚.<br>体代码如下:<br>//---------------------------------------------------<br>//发送方:<br>using System;<br>using System.Drawing;<br>using System.Collections;<br>using System.ComponentModel;<br>using System.Windows.Forms;<br>using System.Data;<br>using System.Runtime.InteropServices;<br>namespace WinFormSendMsg<br>{<br>public class Form1 : System.Windows.Forms.Form<br>{<br>private System.Windows.Forms.TextBox textBox1;<br>private System.Windows.Forms.Button button1;<br>private System.ComponentModel.Container components = null;<br>const int WM_COPYDATA = 0x004A;<br>public Form1()<br>{<br>InitializeComponent();<br>}<br>protected override void Dispose( bool disposing )<br>{<br>if( disposing )<br>{<br>if (components != null) <br>{<br>components.Dispose();<br>}<br>}<br>base.Dispose( disposing );<br>}</p>
<p>private void InitializeComponent()<br>{<br>this.textBox1 = new System.Windows.Forms.TextBox();<br>this.button1 = new System.Windows.Forms.Button();<br>this.SuspendLayout();<br>// <br>// textBox1<br>// <br>this.textBox1.Location = new System.Drawing.Point(184, 24);<br>this.textBox1.Name = "textBox1";<br>this.textBox1.Size = new System.Drawing.Size(128, 21);<br>this.textBox1.TabIndex = 0;<br>this.textBox1.Text = "textBox1";<br>// <br>// button1<br>// <br>this.button1.Location = new System.Drawing.Point(344, 16);<br>this.button1.Name = "button1";<br>this.button1.Size = new System.Drawing.Size(112, 32);<br>this.button1.TabIndex = 1;<br>this.button1.Text = "button1";<br>this.button1.Click += new System.EventHandler(this.button1_Click);<br>// <br>// Form1<br>// <br>this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);<br>this.ClientSize = new System.Drawing.Size(536, 142);<br>this.Controls.AddRange(new System.Windows.Forms.Control[] {<br>this.button1,<br>this.textBox1});<br>this.Name = "Form1";<br>this.Text = "发送方窗体";<br>this.ResumeLayout(false);<br>}<br>static void Main() <br>{<br>Application.Run(new Form1());<br>}<br>[DllImport("User32.dll",EntryPoint="SendMessage")]<br>private static extern int SendMessage(<br>int hWnd, // handle to destination window<br>int Msg, // message<br>int wParam, // first message parameter<br>ref COPYDATASTRUCT lParam // second message parameter<br>);</p>
<p>[DllImport("User32.dll",EntryPoint="FindWindow")]<br>private static extern int FindWindow(string lpClassName,string<br>lpWindowName);</p>
<p>private void button1_Click(object sender, System.EventArgs e)<br>{<br>int WINDOW_HANDLER = FindWindow(null,@"接收方窗体");<br>if(WINDOW_HANDLER == 0)<br>{<br>}<br>else<br>{<br>byte[] sarr = System.Text.Encoding.Default.GetBytes(this.textBox1.Text);<br>int len = sarr.Length;<br>COPYDATASTRUCT cds;<br>cds.dwData = (IntPtr) 100;<br>cds.lpData = this.textBox1.Text;<br>cds.cbData = len + 1;<br>SendMessage(WINDOW_HANDLER, WM_COPYDATA, 0, ref cds);</p>
<p>}<br>}<br>}<br>public struct COPYDATASTRUCT<br>{<br>public IntPtr dwData;<br>public int cbData;<br>[MarshalAs(UnmanagedType.LPStr)] public string lpData;<br>}</p>
<p>}</p>
<p>//---------------------------------------------------<br>//接受方<br>//---------------------------------------------------<br>using System;<br>using System.Drawing;<br>using System.Collections;<br>using System.ComponentModel;<br>using System.Windows.Forms;<br>using System.Data;<br>using System.Runtime.InteropServices;<br>namespace WindowsFormGetMsg<br>{<br>public class Form1 : System.Windows.Forms.Form<br>{<br>private System.Windows.Forms.TextBox textBox1;<br>private System.ComponentModel.Container components = null;<br>const int WM_COPYDATA = 0x004A;<br>public Form1()<br>{<br>InitializeComponent();<br>}<br>protected override void Dispose( bool disposing )<br>{<br>if( disposing )<br>{<br>if (components != null) <br>{<br>components.Dispose();<br>}<br>}<br>base.Dispose( disposing );<br>}<br>private void InitializeComponent()<br>{<br>this.textBox1 = new System.Windows.Forms.TextBox();<br>this.SuspendLayout();<br>// <br>// textBox1<br>// <br>this.textBox1.Location = new System.Drawing.Point(176, 32);<br>this.textBox1.Name = "textBox1";<br>this.textBox1.Size = new System.Drawing.Size(160, 21);<br>this.textBox1.TabIndex = 0;<br>this.textBox1.Text = "textBox1";<br>// <br>// Form1<br>// <br>this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);<br>this.ClientSize = new System.Drawing.Size(432, 266);<br>this.Controls.AddRange(new System.Windows.Forms.Control[] {<br>this.textBox1});<br>this.Name = "Form1";<br>this.Text = "接收方窗体";<br>this.ResumeLayout(false);<br>}<br>static void Main() <br>{<br>Application.Run(new Form1());<br>}<br>protected override void DefWndProc(ref System.Windows.Forms.Message m)<br>{<br>switch(m.Msg)<br>{<br>case WM_COPYDATA:<br>COPYDATASTRUCT mystr = new COPYDATASTRUCT();<br>Type mytype = mystr.GetType();<br>mystr =(COPYDATASTRUCT)m.GetLParam(mytype);<br>this.textBox1.Text =mystr.lpData;<br>break;<br>default:<br>base.DefWndProc(ref m);<br>break;<br>}<br>}<br>}<br>public struct COPYDATASTRUCT<br>{<br>public IntPtr dwData;<br>public int cbData;<br>[MarshalAs(UnmanagedType.LPStr)] public string lpData;<br>}<br>}</p>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值