David Crow
2009-10-29 15:50:59 UTC
I've created a console window with redirected stdin and stdout. I send that
window a command (e.g., dir), and then read the results from that window.
This works intermittently. The command I send governs the "timing" of my
code. For example:
CreateProcess("cmd.exe", ...);
// write the command
DWORD dw;
WriteFile(write_stdin, cmd, cmd.GetLength(), &dw, NULL);
// wait for something to show up
do
{
PeekNamedPipe(read_stdout, NULL, 0, NULL, &dw, NULL);
} while (dw == 0);
// read until no more
do
{
ReadFile(read_stdout, buf, 31, &read, NULL);
PeekNamedPipe(read_stdout, NULL, 0, NULL, &dw, NULL);
} while (dw != 0);
If I change 31 to 32, it stops working when "dir" is the command. If the
command is "chkdsk /?" instead, I use 5.
I understand that ReadFile() will block until that number of bytes have been
read. Ultimately I'd just wait until the command was finished (and use
PeekNamedPipe() to know how much to read), but I don't know how to check
that, and the console-window buffer is only so big.
Any suggestions?
Thanks.
- DC
window a command (e.g., dir), and then read the results from that window.
This works intermittently. The command I send governs the "timing" of my
code. For example:
CreateProcess("cmd.exe", ...);
// write the command
DWORD dw;
WriteFile(write_stdin, cmd, cmd.GetLength(), &dw, NULL);
// wait for something to show up
do
{
PeekNamedPipe(read_stdout, NULL, 0, NULL, &dw, NULL);
} while (dw == 0);
// read until no more
do
{
ReadFile(read_stdout, buf, 31, &read, NULL);
PeekNamedPipe(read_stdout, NULL, 0, NULL, &dw, NULL);
} while (dw != 0);
If I change 31 to 32, it stops working when "dir" is the command. If the
command is "chkdsk /?" instead, I use 5.
I understand that ReadFile() will block until that number of bytes have been
read. Ultimately I'd just wait until the command was finished (and use
PeekNamedPipe() to know how much to read), but I don't know how to check
that, and the console-window buffer is only so big.
Any suggestions?
Thanks.
- DC