I'm currently trying to create a simple program that writes and reads from/to the pipes of a process (cmd.exe
), by calling CreateProcess()
.
I have 3 pipes:
- Input
- Output
- Error
But reading from both output and error pipe is problematic.
I'm currently doing it like this:
while (readOutput) {
DWORD bytesAvailable;
PeekNamedPipe(OutputReadPipe, NULL, 0, NULL, &bytesAvailable, NULL);
while (bytesAvailable > 0) {
// Read the pipe and print the output
}
PeekNamedPipe(ErrorReadPipe, NULL, 0, NULL, &bytesAvailable, NULL);
while (bytesAvailable > 0) {
// Read the pipe and print the output
}
Sleep(20)
}
But sometimes cmd.exe
writes to both pipes (error and output) at the same time and the reading order is incorrect, because I first read from the output pipe and then from the error pipe.
Is there a way I can read from both pipes in the correct order?