利用SendMessage实现C#进程间通信_第1页
利用SendMessage实现C#进程间通信_第2页
利用SendMessage实现C#进程间通信_第3页
利用SendMessage实现C#进程间通信_第4页
利用SendMessage实现C#进程间通信_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、进程之间通讯的几种方法:在Windows程序中,各个进程之间常常需要交换数据,进行数据通讯。常用的方法有使用内存映射文件通过共享内存DLL共享内存使用SendMessage向另一进程发送WM_COPYDATA消息比起前两种的复杂实现来,WM_COPYDATA消息无疑是一种经济实惠的一中方法.(ZT)WM_COPYDATA消息的主要目的是允许在进程间传递只读数据。Windows在通过WM_COPYDATA消息传递期间,不提供继承同步方式。SDK文档推荐用户使用SendMessage函数,接受方在数据拷贝完成前不返回,这样发送方就不可能删除和修改数据:这个函数的原型及其要用到的结构如下:SendM

2、essage(hwnd,WM_COPYDATA,wParam,lParam);其中,WM_COPYDATA对应的十六进制数为0X004AwParam设置为包含数据的窗口的句柄。IParam指向一个COPYDATASTRUCT的结构:typedefstructtagCOPYDATASTRUCTDWORDdwData;用户定义数据DWORDcbData;数据大小PVOIDIpData;/指向数据的指针COPYDATASTRUCT;该结构用来定义用户数据。具体过程如下:首先,在发送方,用FindWindow找到接受方的句柄,然后向接受方发送WM_COPYDATA消息.接受方在DefWndProc事件

3、中,来处理这条消息.由于中文编码是两个字节,所以传递中文时候字节长度要搞清楚.体代码如下:/发送方:usingSystem;usingSystem.Drawing;usingSystem.CoIIections;usingSystem.ComponentModel;usingSystem.Windows.Forms;usingSystem.Data;usingSystem.Runtime.InteropServices;namespaceWinFormSendMsgpublicclassForm1:System.Windows.Forms.FormprivateSystem.Windows.F

4、orms.TextBoxtextBox1;privateSystem.Windows.Forms.Buttonbutton1;privateSystem.ComponentModel.Containercomponents=null;constintWM_COPYDATA=0 x004A;publicForm1()InitializeComponent();protectedoverridevoidDispose(booldisposing)if(disposing)if(components!=null)components.Dispose();base.Dispose(disposing)

5、;privatevoidInitializeComponent()this.textBox1=newSystem.Windows.Forms.TextBox();this.button1=newSystem.Windows.Forms.Button();this.SuspendLayout();/textBox1/this.textBox1.Location=newSystem.Drawing.Point(184,24);this.textBox1.Name=textBox1;this.textBox1.Size=newSystem.Drawing.Size(128,21);this.text

6、Box1.TabIndex=0;this.textBox1.Text=textBox1;/button1/this.button1.Location=newSystem.Drawing.Point(344,16);this.button1.Name=button1;this.button1.Size=newSystem.Drawing.Size(112,32);this.button1.TabIndex=1;this.button1.Text=button1;this.button1.Click+=newSystem.EventHandler(this.button1_Click);/Form

7、1/this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);this.ClientSize=newSystem.Drawing.Size(536,142);this.Controls.AddRange(newSystem.Windows.Forms.Controlthis.button1,this.textBox1);this.Name=Form1;this.Text=发送方窗体;this.ResumeLayout(false);staticvoidMain()Application.Run(newForm1();DllImport(User32

8、.dll,EntryPoint=SendMessage)privatestaticexternintSendMessage(inthWnd,/handletodestinationwindowintMsg,/messageintwParam,/firstmessageparameterrefCOPYDATASTRUCTlParam/secondmessageparameter);DllImport(User32.dll,EntryPoint=FindWindow)privatestaticexternintFindWindow(stringlpClassName,stringlpWindowN

9、ame);privatevoidbutton1_Click(objectsender,System.EventArgse)intWINDOW_HANDLER=FindWindow(null,接收方窗体);if(WINDOW_HANDLER=0)elsebytesarr=System.Text.Encoding.Default.GetBytes(this.textBox1.Text);intlen=sarr.Length;COPYDATASTRUCTcds;cds.dwData=(IntPtr)100;cds.lpData=this.textBox1.Text;cds.cbData=len+1;

10、SendMessage(WINDOW_HANDLER,WM_COPYDATA,0,refcds);publicstructCOPYDATASTRUCTpublicIntPtrdwData;publicintcbData;MarshalAs(UnmanagedType.LPStr)publicstringlpData;/接受方/usingSystem;usingSystem.Drawing;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Windows.Forms;usingSystem.Data;usingSyste

11、m.Runtime.InteropServices;namespaceWindowsFormGetMsgpublicclassForm1:System.Windows.Forms.FormprivateSystem.Windows.Forms.TextBoxtextBox1;privateSystem.ComponentModel.Containercomponents=null;constintWM_COPYDATA=0 x004A;publicForm1()InitializeComponent();protectedoverridevoidDispose(booldisposing)if

12、(disposing)if(components!=null)components.Dispose();base.Dispose(disposing);privatevoidInitializeComponent()this.textBox1=newSystem.Windows.Forms.TextBox();this.SuspendLayout();/textBox1/this.textBox1.Location=newSystem.Drawing.Point(176,32);this.textBox1.Name=textBox1;this.textBox1.Size=newSystem.D

13、rawing.Size(160,21);this.textBox1.TabIndex=0;this.textBox1.Text=textBox1;/Form1/this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);this.ClientSize=newSystem.Drawing.Size(432,266);this.Controls.AddRange(newSystem.Windows.Forms.Controlthis.textBox1);this.Name=Form1;this.Text=接收方窗体;this.ResumeLayout(false);staticvoidMain()Application.Run(newForm1();protectedoverridevoidDefWndProc(refSystem.Windows.Forms.Messagem)switch(m.Msg)caseWM_COPYDATA:COPYDATASTRUCTmystr=newCOPYDATASTRUCT();Typemytype=mystr.GetType();mystr=(COP

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论