Reputation: 22581
I'm trying to do some IPC between a managed and unmanaged process. I've settled on named pipes.
I'm spinning up a thread in managed code, using NamedPipeServerStream:
using (NamedPipeServerStream stream = new NamedPipeServerStream("MyPipe", PipeDirection.In))
{
while (true)
{
stream.WaitForConnection();
stream.Read(buffer, 0, size);
//Handle buffer values
}
}
On the unmanaged side I'm using CallNamedPipe:
CallNamedPipe(TEXT("\\\\.\\pipe\\MyPipe"), NULL, 0, pData, dataSize, NULL, NMPWAIT_WAIT_FOREVER);
However, CallNamedPipe fails with a GetLastError of 5 (Access Denied). Any idea why?
Upvotes: 1
Views: 4486
Reputation: 1030
Sometimes an access denied may be received when there is a lot of opening and closing of named pipes, similar to the errors described here: Named Pipe Remoting Exceptions. The solution seems to be to retry making the named pipe after a short delay.
Upvotes: 0
Reputation: 31
Here is winning line of code:
NamedPipeServerStream pipeServer = new NamedPipeServerStream("MyPipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.None)
It should be both-sided, even you're using it only for outgoing data
Upvotes: 3
Reputation: 9645
My guess would be that you are running the processes under two different accounts. Since you are using the NamedPipeStream constructor that uses default security the other user does not have access. This can be solved by using the constructor that takes a PipeSecurity instance. Then just give the other account access explicitly.
EDIT: I just noticed that you are creating the Pipe as a one-way pipe with the direction in. But I believe that CallNamedPipe attempts to open the pipe for both reading and writing and will fail when connecting to a one-way pipe.
EDIT 2: Also that constructor creates a byte type pipe and CallNamedPipe can only connect to message type pipes. So you'll have to use another constructor.
Upvotes: 1