The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Email Subscription

    Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 716 other subscribers

pipe – Windows how to redirect file parameter to stdout? (Windows equivalent of `/dev/stdout`) – Super User

Posted by jpluimers on 2022/02/02

TL;DR:

  • Windows has CON: which is an equivalent for /dev/tty
  • Windows has no equivalent for /dev/stdout (the standard output stream)
  • There is a C# PipeServer.cs proof-of-concept that allows to simulate /dev/stdout through a temporary named pipe
  • Windows pipe names start with \\.\pipe\ for names on the local machine
  • The above for /dev/stdout on Windows also holds for /dev/stdin (the standard input stream)

All via [Wayback] pipe – Windows how to redirect file parameter to stdout? (Windows equivalent of /dev/stdout) – Super User.

Windows CON is like /dev/tty

Because of extensive backward compatibility to MS-DOS, Windows keeps these filenames as reserved and for most, including CON has special functionality. The original DOS documentation of this has been taken off-line, but luckily there are archival sites: MS-DOS Device Driver Names Cannot be Used as File Names. That is the link to [Wayback] KB74496 before it was taken off-line. Originally, it was known as [Wayback] Q74496:

Microsoft MS-DOS reserves certain names for system device drivers. If you try to name a file using one of these names, you will receive the following error message:

Write Fault Error Writing Device <FILENAME.EXT>Abort, Retry, Ignore, Fail?

The solution is to change the file name.

…

Below is a list of default device driver names.

   Name    Function
   ----    --------
   CON     Keyboard and display
   PRN     System list device, usually a parallel port
   AUX     Auxiliary device, usually a serial port
   CLOCK$  System real-time clock
   NUL     Bit-bucket device
   A:-Z:   Drive letters
   COM1    First serial communications port
   LPT1    First parallel printer port
   LPT2    Second parallel printer port
   LPT3    Third parallel printer port
   COM2    Second serial communications port
   COM3    Third serial communications port
   COM4    Fourth serial communications port

Basically MS-DOS CON is like the Unix/Linux /dev/tty Device file – Wikipedia, and Windows CON is like the /dev/tty* Virtual console – Wikipedia (since it is bound to the conhost.exe Windows Console – Wikipedia)

Windows has no equivalent for /dev/stdout

On Linux, processes can read input from the special files /dev/stdin or write output to /dev/stdout, and you can specify those as file parameters. This is very useful when a tool by default does not read from /dev/stdin or write to /dev/stdout.

I wish Windows had an equivalent for /dev/stdout (and /dev/stdin). Since I could not find it, I asked [Wayback] pipe – Windows how to redirect file parameter to stdout?(Windows equivalent of /dev/stdout) – Super User, which confirmed by suspicion (thanks [Wayback] grawity!):

Windows has no direct equivalent to /dev/stdout.

You can simulate /dev/stdout and /dev/stdin on Windows through a temporary named pipe provided through an intermediate program

[Wayback] grawity also wrote a proof-of-concept C# program that can provide access to standard input via a named pipe from a secondary console:

Run:

PipeServer foo in | programtwo

in window one, and:

programone \\.\pipe\foo

in window two.

I’ve copied the source to a gist having an initial PipeServer.cs [Wayback].

Later, [Wayback] ntninja improved the proof-of-concept so a secondary console was not needed any more, which I copied to the second PipeServer.cs [Wayback].

General usage:

PipeServer [in|out] [process name] [argument 1] [argument 2] [...]

The string "{pipe}" is then replaced by the redirection path.

Real-World example:

PipeServer.exe in "C:\Keil\UV4\Uv4.exe" -b "C:\Project\Project.uvproj" -j0 -o "{pipe}"

What happens underneath is that "{pipe}" becomes a value like "\\.\pipe\pipeserver_1" fully automatically (the tool tries from 1 up till 65535).

Windows named pipes and named pipe names

Since named pipes are a relatively unknown feature for many people, a few important links (note they are not limited to Win32, it’s just that I could not find the generic documentation on them yet):

–jeroen


Source code:

https://gist.github.com/jpluimers/c38271933bdea49b1902fa88d568ab10

Leave a Comment

 

Discover more from The Wiert Corner - irregular stream of stuff

Subscribe now to keep reading and get access to the full archive.

Continue reading