简体   繁体   English

标准输入/标准输出重定向,输入不起作用

[英]Stdin/Stdout redirection , input not working

I am trying to create a pipe between a command line shell and my application.我正在尝试在命令行 shell 和我的应用程序之间创建一个管道。

This is the code I have so far: http://pastebin.com/uupd4aXi这是我到目前为止的代码: http : //pastebin.com/uupd4aXi

What I am trying to do is write "whoami" to stdin and get a return equivalent to that command.我想要做的是将“whoami”写入标准输入并获得与该命令等效的返回。 If I comment out the writeinput function, readoutput successfully prints the standard cmd printout.如果我注释掉 writeinput 函数,则 readoutput 会成功打印标准的 cmd 打印输出。 However if I don't, writeinput gets stuck at an infinite loop at:但是,如果我不这样做, writeinput 就会陷入无限循环:

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".如果我删除 if 语句并手动导致循环中断,我仍然只收到 cmd 打印输出消息,而不是对我的命令“whoami”的响应。

What am I doing wrong?我究竟做错了什么?

You are running into the trap of redirecting both stdin and stout but processing them serially .您遇到了重定向 stdin 和 stout 但串行处理它们的陷阱。

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.如果您只想运行whoami程序并捕获输出,那么您不需要cmd.exe并尝试将whoami.exe作为其输入。 Just run whoami.exe directly and capture its output.只需直接运行whoami.exe并捕获其输出。

EDIT: updated link to the article:https://devblogs.microsoft.com/oldnewthing/20110707-00/?p=10223编辑:更新文章链接:https ://devblogs.microsoft.com/oldnewthing/20110707-00/?p=10223

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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