首页 > 开发 > 综合 > 正文

如何在C#用WM_COPYDATA消息来实现两个进程之间传递数据

2024-07-21 02:26:18
字体:
来源:转载
供稿:网友
中国最大的web开发资源网站及技术社区,

简介:

本文着重讲述了如果用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;
 }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表