




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 网络借贷中的担保机制研究考核试卷
- 2025演艺场所租赁合同模板
- 2025电力建设合同范本
- 2025标准代理商合同
- 二零二五版独家代理协议书独家授权代理协议
- 租赁期间房屋买卖合同书二零二五年
- 二零二五版混凝土工劳务承包合同
- 石场承包开采合同二零二五年
- 二零二五版食堂炊事员聘用合同范例
- 本商铺租赁合同书范例
- 情绪心理学与情绪管理 课件
- 《民俗旅游学》教案-第九章 岁时节日民俗与旅游
- 软件质量证明书
- 高考标准化考场建设方案详细
- 人民医院肿瘤科临床技术操作规范2023版
- 高压-引风机电机检修文件包
- 2023届物理高考二模考前指导
- GB/T 39486-2020化学试剂电感耦合等离子体质谱分析方法通则
- GB/T 11085-1989散装液态石油产品损耗
- GXH-3011A1便携式红外线CO分析仪
- 2022年四川省阿坝州中考数学试卷及解析
评论
0/150
提交评论