使用C#,如何连接到单向的C ++命名管道?

时间:2016-11-03 19:42:59

标签: c# c++ windows named-pipes

我在C ++程序Server中创建了一个命名管道,我希望在C#应用程序Client中连接它。我用C ++编写了一些测试代码,我可以写入管道就好了。我遇到的问题是让C#代码写入命名管道。现在它抛出System.UnauthorizedAccessException' occurred in System.Core.dll异常。这两个程序都在管理员的Visual Studio中运行,我的设置是否有错?

C ++服务器

pipe_handle = CreateNamedPipe("\\\\.\\pipe\\StackPipe",                     // Name of Pipe
                              PIPE_ACCESS_INBOUND,          // Direction of Pipe
                              PIPE_TYPE_MESSAGE |PIPE_READMODE_MESSAGE | PIPE_WAIT, // Pipe Mode
                              1,                                // Maximum instances
                              0,                                // Output Buffer Size
                              (BUFFER_MAX*sizeof(_TCHAR)),  // Input Buffer Size
                              NMPWAIT_USE_DEFAULT_WAIT,     // Default Time Out
                              NULL);                            // Security Attributes - Admin and System 

C ++ Test Client

WaitNamedPipe(L"\\\\.\\pipe\\StackPipe", NMPWAIT_WAIT_FOREVER);
HANDLE hpipe = CreateFile(  L"\\\\.\\pipe\\StackPipe", //Formatting is different
                            GENERIC_WRITE,
                            0,
                            NULL, 
                            OPEN_EXISTING,
                            FILE_ATTRIBUTE_NORMAL,
                            NULL);

C#客户端

NamedPipeClientStream pipeClient = 
        new NamedPipeClientStream(  ".",
                                    "StackPipe",
                                    PipeDirection.In,
                                    PipeOptions.Asynchronous,                                           TokenImpersonationLevel.Identification);
pipeClient.Connect(); //Fails Here

1 个答案:

答案 0 :(得分:0)

感谢@Harry Johnston,我误解了方向:

NamedPipeClientStream pipeClient = 
            new NamedPipeClientStream(  ".",
                                        "StackPipe",
                                        PipeDirection.Out, // Change Direction
                                        PipeOptions.Asynchronous);