I would like to ask you how to set up possible access to my named pipe server from remote clients. Until now I thought that NamedPipes can be used just for inter-process communication on same computer, but based on http://msdn.microsoft.com/en-us/library/windows/desktop/aa365150%28v=vs.85%29.aspx it should be possible by setting PIPE_ACCEPT_REMOTE_CLIENTS / PIPE_REJECT_REMOTE_CLIENTS to allow/ not allow to access from remote computers. I did not find simple way how to setup this functionality in .NET. I suppose that PipeSecurity could be somehow used for it but I did not find a simple way.
My current solution allows to access all users to my named pipe on current machine. Can somebody improve my solution to allow access from another machine as well?
Thanks.
public NamedPipeServerStream CreateNamedPipeServer(string pipeName)
{
const int BufferSize = 65535;
var pipeSecurity = new PipeSecurity();
pipeSecurity.AddAccessRule(new PipeAccessRule("Users", PipeAccessRights.ReadWrite, AccessControlType.Allow));
pipeSecurity.AddAccessRule(new PipeAccessRule("Administrators", PipeAccessRights.ReadWrite, AccessControlType.Allow));
return new NamedPipeServerStream(pipeName, PipeDirection.InOut, countOfServerInstancesToCreate, PipeTransmissionMode.Message, PipeOptions.Asynchronous,
BufferSize,
BufferSize);
}
Yes, named pipes can be from/to remote computers. See http://msdn.microsoft.com/en-us/library/windows/desktop/aa365590(v=vs.85).aspx which details:
Named pipes can be used to provide communication between processes on the same computer or between processes on different computers across a network.
But, this option is not directly supported by .NET. The NamedPipeServerStream
constructors translates the PipeTransmisionMode
into the natives dwPipeMode
parameter so you don't have direct access to that.
Conceivably you could sneak PIPE_REJECT_REMOTE_CLIENTS
in there (PIPE_ACCEPT_REMOTE_CLIENTS
is zero, so you don't have to do anything to support that). You'd have to OR in a new value for the PipeTransmissionMode
parameter. For example:
var transmissionMode =
(PipeTransmissionMode)((int)PipeTransmissionMode.Byte | (0x8 >> 1));
Then create a NamedPipeServerStream
instance with transmissionMode
. But, don't expect any support for it :)