我是在c#代码中调用的winAPI来实现的命名管道
服务器端:
static void Main(string[] args)
{
string pipename="\\\\.\\pipe\\mypipe";
uint BufferSize=1024;
IntPtr h_pipe=IntPtr.Zero;
Process p1 = null;
try
{
h_pipe = CreateNamedPipe(pipename, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1, BufferSize, BufferSize, 0, IntPtr.Zero);
if ((IntPtr)INVALID_HANDLE_OPERATION == h_pipe)
{
Console.WriteLine("管道打开失败!");
return;
}
Process process = new Process();
ProcessStartInfo ps = new ProcessStartInfo();
ps.FileName = "E:\\MyProject\\PipeClient\\PipeClient\\bin\\Debug\\PipeClient.exe";
ps.Arguments = pipename+" "+"来自服务端";
ps.WindowStyle = ProcessWindowStyle.Normal;
p1= Process.Start(ps); byte[] readbyte = new byte[1024];
int readsize = 0;
unsafe
{
fixed (byte* preadbyte = readbyte)
{
System.Threading.Thread.Sleep(1000);
if (ReadFile(h_pipe, (void*)preadbyte, BufferSize, &readsize, 0))
{
Console.WriteLine(System.Text.Encoding.Default.GetString(readbyte));
}
else
{
Console.WriteLine("读取文件失败");
}
}
}
}
finally
{
if(p1!=null)
p1.Close();
if (h_pipe != null)
CloseHandle(h_pipe);
}
}
}
客户端:
static void Main(string[] args)
{
IntPtr h_pipe = IntPtr.Zero;
try
{
if (args != null && args.Length > 0)
{
h_pipe = CreateFile(args[0], GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, System.IntPtr.Zero);
if (h_pipe == (IntPtr)INVALID_HANDLE_OPERATION)
{
Console.WriteLine("打开管道失败!");
return;
}
else
{
byte[] writebyte = new byte[1024];
writebyte=System.Text.Encoding.Default.GetBytes(args[1]+"(客户端返回)");
uint buffersize=1024;
uint outputbuffer=0;
unsafe
{
fixed (byte* writebytes = writebyte)
{
if (WriteFile(h_pipe, (void*)writebytes, buffersize, out outputbuffer, IntPtr.Zero))
{
Console.WriteLine("客户端通过管道写入成功!");
}
else
{
Console.WriteLine("客户端通过管道写入失败!");
}
}
}
}
}
else
{
Console.WriteLine("参数为空");
}
}
finally
{
if (h_pipe != null)
CloseHandle(h_pipe);
}
} 运行服务器端,客户端会提示“打开管道失败” ,请问我的createfile为什么会失败呢?
服务器端:
static void Main(string[] args)
{
string pipename="\\\\.\\pipe\\mypipe";
uint BufferSize=1024;
IntPtr h_pipe=IntPtr.Zero;
Process p1 = null;
try
{
h_pipe = CreateNamedPipe(pipename, PIPE_ACCESS_INBOUND, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1, BufferSize, BufferSize, 0, IntPtr.Zero);
if ((IntPtr)INVALID_HANDLE_OPERATION == h_pipe)
{
Console.WriteLine("管道打开失败!");
return;
}
Process process = new Process();
ProcessStartInfo ps = new ProcessStartInfo();
ps.FileName = "E:\\MyProject\\PipeClient\\PipeClient\\bin\\Debug\\PipeClient.exe";
ps.Arguments = pipename+" "+"来自服务端";
ps.WindowStyle = ProcessWindowStyle.Normal;
p1= Process.Start(ps); byte[] readbyte = new byte[1024];
int readsize = 0;
unsafe
{
fixed (byte* preadbyte = readbyte)
{
System.Threading.Thread.Sleep(1000);
if (ReadFile(h_pipe, (void*)preadbyte, BufferSize, &readsize, 0))
{
Console.WriteLine(System.Text.Encoding.Default.GetString(readbyte));
}
else
{
Console.WriteLine("读取文件失败");
}
}
}
}
finally
{
if(p1!=null)
p1.Close();
if (h_pipe != null)
CloseHandle(h_pipe);
}
}
}
客户端:
static void Main(string[] args)
{
IntPtr h_pipe = IntPtr.Zero;
try
{
if (args != null && args.Length > 0)
{
h_pipe = CreateFile(args[0], GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, System.IntPtr.Zero);
if (h_pipe == (IntPtr)INVALID_HANDLE_OPERATION)
{
Console.WriteLine("打开管道失败!");
return;
}
else
{
byte[] writebyte = new byte[1024];
writebyte=System.Text.Encoding.Default.GetBytes(args[1]+"(客户端返回)");
uint buffersize=1024;
uint outputbuffer=0;
unsafe
{
fixed (byte* writebytes = writebyte)
{
if (WriteFile(h_pipe, (void*)writebytes, buffersize, out outputbuffer, IntPtr.Zero))
{
Console.WriteLine("客户端通过管道写入成功!");
}
else
{
Console.WriteLine("客户端通过管道写入失败!");
}
}
}
}
}
else
{
Console.WriteLine("参数为空");
}
}
finally
{
if (h_pipe != null)
CloseHandle(h_pipe);
}
} 运行服务器端,客户端会提示“打开管道失败” ,请问我的createfile为什么会失败呢?
在c#里面调用WinAPI的时候,接口的参数有些找不到它所代表的实际数值,那我在c#中怎么定义这些参数呢?
比如:HANDLE CreateFile(
LPCTSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile
);参数 dwShareMode 要求为 FILE_SHARE_DELETE 或 FILE_SHARE_READ 或 FILE_SHARE_WRITE
msdn中没有给出这几个参数的实际值,那我在c#中调用方法的时候怎么传递这些参数呢?有些参数msdn中是给了实际值了,如 PIPE_ACCESS_DUPLEX=0x00000003 (CreateNamedPipe方法的参数)
很不幸的告诉这位仁兄啊,我兜里的分是 它“o” ,这个40分是发帖的时候那一栏里默认的我也没有改,不知道我结贴发饷的时候csdn会怎么处理呢?