win32api包装
using System;
using System.Runtime.InteropServices;
namespace WindowsApplication2
...{
/**//// <summary>
/// Win32 的摘要说明。
/// </summary>
internal class Win32
...{
public static readonly IntPtr MAILSLOT_WAIT_FOREVER = new IntPtr(-1);
public static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
[DllImport("kernel32.dll")]
public static extern IntPtr CreateMailslot(string lpName, uint nMaxMessageSize,
IntPtr lReadTimeout, IntPtr lpSecurityAttributes);
[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public static extern IntPtr CreateFile(
string lpFileName,
EFileAccess dwDesiredAccess,
EFileShare dwShareMode,
IntPtr lpSecurityAttributes,
ECreationDisposition dwCreationDisposition,
EFileAttributes dwFlagsAndAttributes,
IntPtr hTemplateFile);
[DllImport("kernel32.dll")]
public static extern bool ReadFile(IntPtr hFile, byte[] lpBuffer,
uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, IntPtr lpOverlapped);
[DllImport("kernel32.dll")]
static extern bool WriteFile(IntPtr hFile, byte [] lpBuffer,
uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten,
[In] ref System.Threading.NativeOverlapped lpOverlapped);
[Flags]
public enum EFileAccess : uint
...{
GenericRead = 0x80000000,
GenericWrite = 0x40000000,
GenericExecute = 0x20000000,
GenericAll = 0x10000000
}
[Flags]
public enum EFileShare : uint
...{
None = 0x00000000,
Read = 0x00000001,
Write = 0x00000002,
Delete = 0x00000004
}
public enum ECreationDisposition : uint
...{
New = 1,
CreateAlways = 2,
OpenExisting = 3,
OpenAlways = 4,
TruncateExisting = 5
}
[Flags]
public enum EFileAttributes : uint
...{
Readonly = 0x00000001,
Hidden = 0x00000002,
System = 0x00000004,
Directory = 0x00000010,
Archive = 0x00000020,
Device = 0x00000040,
Normal = 0x00000080,
Temporary = 0x00000100,
SparseFile = 0x00000200,
ReparsePoint = 0x00000400,
Compressed = 0x00000800,
Offline = 0x00001000,
NotContentIndexed= 0x00002000,
Encrypted = 0x00004000,
Write_Through = 0x80000000,
Overlapped = 0x40000000,
NoBuffering = 0x20000000,
RandomAccess = 0x10000000,
SequentialScan = 0x08000000,
DeleteOnClose = 0x04000000,
BackupSemantics = 0x02000000,
PosixSemantics = 0x01000000,
OpenReparsePoint = 0x00200000,
OpenNoRecall = 0x00100000,
FirstPipeInstance= 0x00080000
}
}
}
服务器设计界面

服务器端代码
private IntPtr mailslot;
private Thread t_ReadingProc = null;
private void button1_Click_1(object sender, System.EventArgs e)
...{
this.mailslot = Win32.CreateMailslot("//./Mailslot/Myslot",0,Win32.MAILSLOT_WAIT_FOREVER,new IntPtr(0));
if(this.mailslot == Win32.INVALID_HANDLE_VALUE)
...{
this.textBox1.Text = "创建邮槽失败";
}
else
...{
this.textBox1.Text += System.DateTime.Now.ToLongTimeString()+"成功创建邮槽//./Mailslot/Myslot ";
t_ReadingProc= new Thread(new ThreadStart(Reading));
t_ReadingProc.Start();
this.textBox1.Text += System.DateTime.Now.ToLongTimeString()+"启动读邮槽线程 ";
}
}
private void Reading()
...{
byte[] buffer = new byte[256];
uint bn = 0;
while(Win32.ReadFile(this.mailslot,buffer,256,out bn,new IntPtr(0)))
...{
this.textBox1.Text += System.DateTime.Now.ToLongTimeString()+"邮槽接收到如下数据:"+System.Text.Encoding.GetEncoding("gb2312").GetString(buffer,0,Convert.ToInt32(bn))+" ";
}
}客户端设计界面

客户端代码
private IntPtr mailslot;
private void button2_Click(object sender, System.EventArgs e)
...{
this.mailslot = Win32.CreateFile(
"//./Mailslot/Myslot",
Win32.EFileAccess.GenericWrite,
Win32.EFileShare.Read,
new IntPtr(0),
Win32.ECreationDisposition.OpenExisting,
Win32.EFileAttributes.Normal,
new IntPtr(0));
if(this.mailslot==Win32.INVALID_HANDLE_VALUE)
...{
MessageBox.Show("连接邮槽失败");
}
else
...{
this.button2.Text = "已连接";
this.button2.Enabled = false;
this.button1.Enabled = true;
}
}
private void button1_Click(object sender, System.EventArgs e)
...{
byte[] b = System.Text.Encoding.GetEncoding("gb2312").GetBytes(this.textBox1.Text);
uint bl;
System.Threading.NativeOverlapped lp = new System.Threading.NativeOverlapped();
if(Win32.WriteFile(
this.mailslot,b,Convert.ToUInt16(b.Length),out bl,ref lp))
...{
//MessageBox.Show("发送成功");
}
else
...{
MessageBox.Show("发送失败");
}
}测试结果截图

本文介绍了一个基于Win32API实现的邮件槽(Mailslot)通信示例,包括服务器端与客户端的设计与实现代码。通过使用CreateMailslot及CreateFile等函数,实现了进程间的数据传递。

2万+

被折叠的 条评论
为什么被折叠?



