tags:

views:

482

answers:

2

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?

+1  A: 

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.

Stephen Martin
For testing purposes I've tried running both code paths in the same process, to the same effect. I'll look into PipeSecurity anyway.
Kevin Montrose
A: 

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

Shattenjagger