当前位置: 首页 > news >正文

(转载)在C#用WM_COPYDATA消息来实现两个进程之间传递数据

原文地址:http://www.kehui.net/index.php?op=article&file=read&aid=16470


简介:

本文着重讲述了如果用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;
该结构用来定义用户数据。


具体过程如下:


接受方在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;
 }
}



//---------------------------------------------------
//发送方:
//---------------------------------------------------

首先,在发送方,用FindWindow找到接受方的句柄,然后向接受方发送WM_COPYDATA消息.

转载于:https://www.cnblogs.com/snowlove67/archive/2006/01/04/310668.html

相关文章:

  • 解读C#中的正则表达式
  • 《并发操作一致性问题》已全部完成
  • CRM投资回报(ROI)分析
  • ASP.NET应用程序的部署--兼谈aspnet_compiler.exe命令
  • How to break to loop? It frustrates me!
  • ASP.NET WEB服务器控件的使用
  • 体验TFS Build,要想实现每日Build,这个是不错的工具,
  • 一连几天加班,很辛苦.
  • 如何把javascript的值赋给.net的服务器控件
  • 新浪网:《2006中国Web2.0值得关注的百家企业名单(表)》
  • 减少滚动条滚动次数,迅速启动电脑
  • Automation服务器不能创建对象
  • 为我喜欢的德意志战车
  • 在工作流基础上的解决方案清单
  • Flash与后台数据交互方法总结
  • ES6指北【2】—— 箭头函数
  • 2019年如何成为全栈工程师?
  • Angular 4.x 动态创建组件
  • CoolViewPager:即刻刷新,自定义边缘效果颜色,双向自动循环,内置垂直切换效果,想要的都在这里...
  • HashMap剖析之内部结构
  • Js基础知识(一) - 变量
  • node-glob通配符
  • Spring核心 Bean的高级装配
  • uni-app项目数字滚动
  • 初识MongoDB分片
  • 动态规划入门(以爬楼梯为例)
  • 官方新出的 Kotlin 扩展库 KTX,到底帮你干了什么?
  • 后端_MYSQL
  • 前端技术周刊 2019-01-14:客户端存储
  • 思维导图—你不知道的JavaScript中卷
  • 携程小程序初体验
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • 回归生活:清理微信公众号
  • 完善智慧办公建设,小熊U租获京东数千万元A+轮融资 ...
  • ​LeetCode解法汇总2304. 网格中的最小路径代价
  • ​Linux·i2c驱动架构​
  • #### go map 底层结构 ####
  • (+4)2.2UML建模图
  • (14)Hive调优——合并小文件
  • (4)logging(日志模块)
  • (Matlab)使用竞争神经网络实现数据聚类
  • (二)fiber的基本认识
  • (含react-draggable库以及相关BUG如何解决)固定在左上方某盒子内(如按钮)添加可拖动功能,使用react hook语法实现
  • (论文阅读笔记)Network planning with deep reinforcement learning
  • (十六)Flask之蓝图
  • (数据结构)顺序表的定义
  • (源码版)2024美国大学生数学建模E题财产保险的可持续模型详解思路+具体代码季节性时序预测SARIMA天气预测建模
  • .NET CORE Aws S3 使用
  • .Net 高效开发之不可错过的实用工具
  • .net 使用$.ajax实现从前台调用后台方法(包含静态方法和非静态方法调用)
  • .net 验证控件和javaScript的冲突问题
  • .net6使用Sejil可视化日志
  • .NET使用存储过程实现对数据库的增删改查
  • @RequestBody的使用
  • [].shift.call( arguments ) 和 [].slice.call( arguments )