Is there a way to redirect the standard output of a process in Win32 console to a named pipe?
Named pipes are built in to Windows and while they would be an useful concept, I've never seen them used from command line.
Ie. like example.exe >\\.\mypipe
. (This syntax may not be correct but you get the point.)
I would like to be able to redirect stdout and stderr to different pipes at the same time.
I would like to avoid using physical files as a substitute, to escape dealing with IO slowness, IO buffers, file locks, access rights, available hard disk space, decision to overwrite, unmeant persistence, etc.
Another reason is because the traditional Windows set of tools are not designed around a (text) file based philosophy as much as in Unix. Also, named pipes couldn't easily be mounted in Windows, if at all.
Finally, there is the curiosity if a good concept could be put into good use.
Best Answer
I'm not sure why you don't want to redirect to a file. There are two methods I will provide here. One method is to redirect to and read from a file, the other is a set of programs.
Named pipes
What I did was write two programs for .NET 4. One sends output to a named pipe, the other reads from this pipe and displays to the console. Usage is quite simple:
In another console window:
Unfortunately, this can only redirect
stdout
(orstdin
, or combined), notstderr
by itself, due to limitations in the pipe operator (|
) in the Windows Command Prompt. If you figure out how to sendstderr
through that pipe operator, it should work. Alternatively, the server could be modified to launch your program and specifically redirectstderr
. If that is necessary, let me know in a comment (or do it yourself); it's not too difficult if you have some C# and .NET "Process" library knowledge.You can download the server and the client.
If you close the server after connection, the client will close immediately. If you close the client after connection, the server will close as soon as you try to send something through it. It's not possible to reconnect a broken pipe, mostly because I can't be bothered doing something so complicated right now. It's also limited to one client per server.
Source code
These are written in C#. There's not much point trying to explain it. They use the .NET NamedPipeServerStream and NamedPipeClientStream.
The server:
The client:
Redirecting to a file
stderr
output to that fileThis provides the desired effect of one console window to watch
stdout
(and providestdin
), and another to watchstderr
.Anything that mimics
tail
would work. The PowerShell method works natively in Windows, but may be a bit slow (i.e. there's some latency between the writing to the file and the displaying to the screen). See this StackOverflow question for othertail
alternatives.The only problem is the temporary file may grow quite large. A possible workaround is to run a loop that only prints if the file has content and clear the file immediately afterwards, but that would cause a race condition.