ᴘᴀɴᴀʏɪᴏᴛɪs
ᴘᴀɴᴀʏɪᴏᴛɪs

Reputation: 7509

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?

Upvotes: 0

Views: 775

Answers (1)

Raymond Chen
Raymond Chen

Reputation: 45173

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

Upvotes: 1

Related Questions