日期:2014-05-20  浏览次数:20833 次

请教几个关于"在C#中用WM_COPYDATA消息来实现进程间通信"的问题
代码如下:
//---------------------------------------------------
//发送方:
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,@ "接收方窗体 ");   //   1
if(WINDOW_HANDLER   ==   0)
{
}
else
{
byte[]   sarr   =   System.Text.Encoding.Default.GetBytes(this.textBox1.Text);