简体   繁体   中英

Using C#, how do you connect to a C++ Named Pipe that is mono-directional?

I have a named pipe being created in a C++ program Server and I want to connect to it in a C# application Client . I wrote some test code in C++ and I can write to the pipe just fine. The issue I am having is getting the C# code to write to the named pipe. Right now it throws a System.UnauthorizedAccessException' occurred in System.Core.dll exception. Both programs are running in Visual Studio as admin, do I have the settings wrong?

C++ Server

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# Client

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

Thanks to @Harry Johnston, I misread the direction:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM