Windows commands for interfacing named pipes with standard I/O streams.
In the following discussion, produce
is a program that outputs data on
stdout
, while consume
is a program that reads data on stdin
. We'll
demonstrate how to achieve the same result as
produce | consume > result.txt
using named pipes.
Here are examples I've used for testing purposes. Substitute to taste.
@ECHO OFF
REM produce.bat
REM Runs seq (as installed with MSYS).
REM Prints a numeric sequence (here, 1 through 20) to stdout.
C:\MinGW\msys\1.0\bin\seq.exe 20
@ECHO OFF
REM consume.bat
REM Runs wc (as installed with MSYS).
REM Prints newline/word/byte counts of stdin.
C:\MinGW\msys\1.0\bin\wc.exe
createAndReadPipe
: Get stdin
from a new inbound named pipeOn one console, do:
createAndReadPipe MyPipeName | consume > result.txt
Then, on another console, do:
produce > \\.\pipe\MyPipeName
createAndWritePipe
: Put stdout
to a new outbound named pipeOn one console, do:
produce | createAndWritePipe MyPipeName
Then, on another console, do:
consume < \\.\pipe\MyPipeName > result.txt
createAndReadPipe
and createAndWritePipe
are pipe servers. The
program that subsequently opens the pipe for write or read,
respectively, is a client.This has been successfully built on MinGW and MSYS with GCC 4.8.1. The resulting binary does not depend on DLLs not already distributed with Windows.
Copyright © 2014 Peter S. May
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.