I am trying to create a pipe between a command line shell and my application.
This is the code I have so far:
http://pastebin.com/uupd4aXi
What I am trying to do is write “whoami” to stdin and get a return equivalent to that command.
If I comment out the writeinput function, readoutput successfully prints the standard cmd printout. However if I don’t, writeinput gets stuck at an infinite loop at:
for (;;)
{
bSuccess = WriteFile(hSTD_IN_WRITE, chBuf, sizeof(chBuf), &dwWritten, NULL);
if ( ! bSuccess ) break;
}
If i remove the if statement and manually cause a break on the loop, I am still only getting the cmd printout message but not the respone to my command “whoami”.
What am I doing wrong?
You are running into the trap of redirecting both stdin and stout but processing them serially.
If all you want to do is run the
whoamiprogram and capture the output, then you don’t needcmd.exeand try to pumpwhoami.exeas its input. Just runwhoami.exedirectly and capture its output.EDIT: updated link to the article:
https://devblogs.microsoft.com/oldnewthing/20110707-00/?p=10223