国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

如何在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;
 }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南宁市| 扶风县| 招远市| 怀来县| 盐城市| 青川县| 政和县| 苏尼特右旗| 新昌县| 丰都县| 凤冈县| 都江堰市| 冀州市| 库尔勒市| 昌图县| 互助| 邵阳市| 邵东县| 乌拉特后旗| 剑河县| 巴楚县| 饶平县| 和林格尔县| 肥乡县| 精河县| 墨玉县| 正蓝旗| 新乡县| 余庆县| 襄樊市| 蒙阴县| 马鞍山市| 项城市| 黔江区| 三明市| 钟山县| 山西省| 玛沁县| 贵港市| 河曲县| 慈利县|