我的程序可以调通,按钮1创建邮件槽;在textbox1输入信息,按下按钮2发送;按下按钮3,接收,将信息显示在textbox2。前两步都正常,但是按下按钮3接收信息时出错了,请大家帮我看看哪里错了,下面是我的代码,大家帮帮忙,谢谢了using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Runtime.InteropServices;namespace MailSlot
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private static FileStream messageStream = null; private static IntPtr ptr; private static string mailSlotName = @"\\.\mailslot\PasswordManagerSlot"; private static bool receiving = false; [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern IntPtr CreateFile( string filename, [MarshalAs(UnmanagedType.U4)] FileAccess fileaccess, [MarshalAs(UnmanagedType.U4)] FileShare fileshare, int securityattributes, [MarshalAs(UnmanagedType.U4)] FileMode creationdisposition, int flags, IntPtr template);
public const uint MAILSLOT_WAIT_FOREVER = 0xFFFFFFFF; public const int INVALID_HANDLE_VALUE = -1;
[DllImport("kernel32.dll")] static extern IntPtr CreateMailslot(string lpName, uint nMaxMessageSize, uint lReadTimeout, IntPtr lpSecurityAttributes); [DllImport("kernel32.dll")] static extern bool GetMailslotInfo(IntPtr hMailslot, IntPtr lpMaxMessageSize, ref int lpNextSize, ref int lpMessageCount, IntPtr lpReadTimeout);  [DllImport("kernel32.dll")] static extern bool ReadFile(IntPtr hFile, byte[] lpBuffer, uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, IntPtr lpOverlapped); [DllImport("kernel32.dll", SetLastError=true)] static extern bool CloseHandle(IntPtr hObject); private static int nextMessageSize = 0; private static byte[] buffer = new byte[4096]; /// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(176, 40);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 24);
this.button1.TabIndex = 0;
this.button1.Text = "创建";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// button2
// 
this.button2.Location = new System.Drawing.Point(176, 96);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(72, 23);
this.button2.TabIndex = 1;
this.button2.Text = "发送 ";
this.button2.Click += new System.EventHandler(this.button2_Click);
// 
// button3
// 
this.button3.Location = new System.Drawing.Point(176, 152);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(72, 23);
this.button3.TabIndex = 2;
this.button3.Text = "接收";
this.button3.Click += new System.EventHandler(this.button3_Click);
// 
// textBox1
// 
this.textBox1.Location = new System.Drawing.Point(16, 96);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(136, 21);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "textBox1";
// 
// textBox2
// 
this.textBox2.Location = new System.Drawing.Point(16, 152);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(136, 21);
this.textBox2.TabIndex = 4;
this.textBox2.Text = "textBox2";
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(280, 214);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
// MailSlot
ptr = CreateMailslot(mailSlotName, (uint)1000, MAILSLOT_WAIT_FOREVER, (System.IntPtr)0); if (ptr.ToInt32() == INVALID_HANDLE_VALUE) { /* ask the framework to marshall the win32 error code to an exception */ Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error()); } receiving = true;

} private void button2_Click(object sender, System.EventArgs e)
{
// SendMessage ptr = CreateFile(mailSlotName, FileAccess.ReadWrite, FileShare.ReadWrite, 0, FileMode.Open, 0, IntPtr.Zero); /* Is bad handle? INVALID_HANDLE_VALUE */ if (ptr.ToInt32() == -1) { /* ask the framework to marshall the win32 error code to an exception */ Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error()); } else {
string message = this.textBox1.Text.ToString();//... FileStream messageStream = new FileStream(ptr,FileAccess.Write); byte[] buffer = System.Text.Encoding.UTF8.GetBytes(message); messageStream.Write(buffer, 0, buffer.Length); messageStream.Close(); }

} private void button3_Click(object sender, System.EventArgs e)
{
// ReceiveMessage int l = messageStream.Read(buffer, 0, buffer.Length);//未将对象引用设置到对象的实例???? if (l > 0) { string message = System.Text.Encoding.UTF8.GetString(buffer, 0, l);
this.textBox2.Text = message.ToString(); } // if

}
}
}