I create a named pipe server with my .NET app, and its name is "TSPipe". I then open Notepad and try to save the file to "\.\pipe\TSPipe". I then want to be able to read what notepad wrote to that pipe.
I'm still working out the general logic for the thread that handles the NamedPipeServerStream, but here's my code for the named pipe server:
public void PipeThread() {
var sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
var rule = new PipeAccessRule(sid, PipeAccessRights.ReadWrite, AccessControlType.Allow);
var sec = new PipeSecurity();
sec.AddAccessRule(rule);
while (continuePipeThread) {
pipeStream = new NamedPipeServerStream("TSPipe", PipeDirection.In, 100, PipeTransmissionMode.Byte, PipeOptions.None, 0, 0, sec);
Program.Output.PrintLine("Waiting for connection...");
pipeStream.WaitForConnection();
Program.Output.PrintLine("Connected, reading...");
byte[] data = new byte[1024];
int lenRead = 0;
lenRead = pipeStream.Read(data, 0, data.Length);
string line = System.Text.Encoding.ASCII.GetString(data, 0, lenRead);
Program.Output.PrintLine(line);
pipeStream.Close();
pipeStream.Dispose();
}
}
Thanks in advance for any help, but I will let you know if any of the suggestions help!
You cannot write to a named pipe from notepad. Using your code, when I try to write to \\\\.\pipe\TSPipe
notepad auto-corrects it to the UNC path \\\\pipe\TSPipe
. I tried a couple of other applications and they did not work either. It appears that they run some logic other than just passing the name typed in into CreateFile
.
However running something like echo Hello, Word! > \\\\.\pipe\TSPipe
will send the text Hello, World!
to your pipe server.
So I guess the answer to the question "On Windows, Can I use Named Pipes As Files?" is sometimes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With