CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

看了C#Programming(O'Rreilly)后很疑惑,难道不同进程间通信也要用tcp/http么?

楼主elusion(落)2004-05-02 22:20:18 在 .NET技术 / C# 提问

可能是我理解有限   但觉得他就这意思  
   
  另外也请问已运行的俩appdomain间怎么通信?? 问题点数:20、回复次数:8Top

1 楼turnmissile(会翻跟头的导弹)回复于 2004-05-03 10:34:09 得分 1

有一个类不错。  
  MessageQueue,用法在msdn里面有Top

2 楼Ninputer(装配脑袋)回复于 2004-05-03 10:49:29 得分 1

用TCP是很好的方法啊。SQLServer不就是用这种方式通讯的吗?Top

3 楼lvenlee(大头.NET)回复于 2004-05-03 12:25:14 得分 1

看你应用到什么程度了,看情况选择合适的通信方式.Top

4 楼Elusive(落)回复于 2004-05-03 12:37:03 得分 1

to   turnmissile(会翻跟头的导弹)    
  我查查看  
  to   Ninputer(装配脑袋)   (   )    
  不会吧   我是指同一台pc哩Top

5 楼tiger999(不吃肉的老虎)回复于 2004-05-03 14:50:28 得分 1

为何不可以?只不过数据到网卡兜了一圈,如过都是本地进程,效率就很差了。Top

6 楼elusion(落)回复于 2004-05-03 14:52:39 得分 0

那本地进程该怎么办哩  
  Top

7 楼stdotleo(大雨仔|M$ MVP)回复于 2004-05-04 09:50:26 得分 15

进程之间通讯的几种方法:  
   
  在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   WinFormSendMsg  
  {  
  public   class   Form1   :   System.Windows.Forms.Form  
  {  
  private   System.Windows.Forms.TextBox   textBox1;  
  private   System.Windows.Forms.Button   button1;  
  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   );  
  }  
   
   
  private   void   InitializeComponent()  
  {  
  this.textBox1   =   new   System.Windows.Forms.TextBox();  
  this.button1   =   new   System.Windows.Forms.Button();  
  this.SuspendLayout();  
  //    
  //   textBox1  
  //    
  this.textBox1.Location   =   new   System.Drawing.Point(184,   24);  
  this.textBox1.Name   =   "textBox1";  
  this.textBox1.Size   =   new   System.Drawing.Size(128,   21);  
  this.textBox1.TabIndex   =   0;  
  this.textBox1.Text   =   "textBox1";  
  //    
  //   button1  
  //    
  this.button1.Location   =   new   System.Drawing.Point(344,   16);  
  this.button1.Name   =   "button1";  
  this.button1.Size   =   new   System.Drawing.Size(112,   32);  
  this.button1.TabIndex   =   1;  
  this.button1.Text   =   "button1";  
  this.button1.Click   +=   new   System.EventHandler(this.button1_Click);  
  //    
  //   Form1  
  //    
  this.AutoScaleBaseSize   =   new   System.Drawing.Size(6,   14);  
  this.ClientSize   =   new   System.Drawing.Size(536,   142);  
  this.Controls.AddRange(new   System.Windows.Forms.Control[]   {  
  this.button1,  
  this.textBox1});  
  this.Name   =   "Form1";  
  this.Text   =   "发送方窗体";  
  this.ResumeLayout(false);  
   
  }  
   
  static   void   Main()    
  {  
  Application.Run(new   Form1());  
  }  
   
  [DllImport("User32.dll",EntryPoint="SendMessage")]  
  private   static   extern   int   SendMessage(  
  int   hWnd,   //   handle   to   destination   window  
  int   Msg,   //   message  
  int   wParam,   //   first   message   parameter  
  ref   COPYDATASTRUCT   lParam   //   second   message   parameter  
  );  
   
   
  [DllImport("User32.dll",EntryPoint="FindWindow")]  
  private   static   extern   int   FindWindow(string   lpClassName,string  
  lpWindowName);  
   
   
  private   void   button1_Click(object   sender,   System.EventArgs   e)  
  {  
  int   WINDOW_HANDLER   =   FindWindow(null,@"接收方窗体");  
  if(WINDOW_HANDLER   ==   0)  
  {  
   
  }  
  else  
  {  
  byte[]   sarr   =   System.Text.Encoding.Default.GetBytes(this.textBox1.Text);  
  int   len   =   sarr.Length;  
   
  COPYDATASTRUCT   cds;  
  cds.dwData   =   (IntPtr)   100;  
  cds.lpData   =   this.textBox1.Text;  
  cds.cbData   =   len   +   1;  
  SendMessage(WINDOW_HANDLER,   WM_COPYDATA,   0,   ref   cds);  
   
   
  }  
   
  }  
  }  
  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   );  
  }  
  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);  
   
  }  
  static   void   Main()    
  {  
  Application.Run(new   Form1());  
  }  
   
  protected   override   void   DefWndProc(ref   System.Windows.Forms.Message   m)  
  {  
  switch(m.Msg)  
  {  
  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;  
   
  }  
   
  }  
   
  }  
   
  public   struct   COPYDATASTRUCT  
  {  
  public   IntPtr   dwData;  
  public   int   cbData;  
  [MarshalAs(UnmanagedType.LPStr)]   public   string   lpData;  
  }  
  }  
  Top

8 楼elusion(落)回复于 2004-05-04 12:43:26 得分 0

我查了一下,有一个c#的Clipboard可用Top

相关问题

  • C++ I/O 问题
  • 请教c++的I\O输入问题
  • vb.net中c#中的 o Is LinkButton 怎么写
  • 关于C中的I/O的问题
  • 100分求解TCP/IP C/S的例程!!
  • C++builder中TCP通信问题
  • 怎样才能截获本地机进出的TCP/IP包?(C/C++)
  • c#?!?!?!
  • :)C#
  • C#!!!!!

关键词

  • c#
  • 数据
  • 通信
  • 消息
  • 内存
  • 结构
  • 用户
  • copydata
  • 进程
  • 接受方

得分解答快速导航

  • 帖主:elusion
  • turnmissile
  • Ninputer
  • lvenlee
  • Elusive
  • tiger999
  • stdotleo

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo