简体   繁体   中英

Stdin/Stdout redirection , input not working

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 whoami program and capture the output, then you don't need cmd.exe and try to pump whoami.exe as its input. Just run whoami.exe directly and capture its output.

EDIT: updated link to the article:https://devblogs.microsoft.com/oldnewthing/20110707-00/?p=10223

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM