关于命名管道通信的问题
从网上下了段代码,修改了一下,用于本机上C和C#之间的命名管道通信。可以正常的收发信息
while (true) { Console.WriteLine("请输入信息"); string msg = Console.ReadLine(); if (msg == "EXIT") break; byte[] msgBuffer = Encoding.UTF8.GetBytes(msg); IntPtr fileHandle; while (true) { // 创建命名管道 fileHandle = NamedPipeNative.CreateFile(pipeName, NamedPipeNative.GENERIC_READ | NamedPipeNative.GENERIC_WRITE, 0, null, NamedPipeNative.OPEN_EXISTING, 0, 0); // 创建失败时,跳出循环 if (fileHandle.ToInt32() != NamedPipeNative.INVALID_HANDLE_VALUE) break; // 无法打开管道时,不再执行下面的操作 if (NamedPipeNative.GetLastError() != NamedPipeNative.ERROR_PIPE_BUSY) { Console.WriteLine("不能打开管道"); return; } if (!NamedPipeNative.WaitNamedPipe(pipeName, 20000)) { Console.WriteLine("不能打开管道"); return; } }