标准输入/标准输出重定向,输入不起作用
我正在尝试在命令行 shell 和我的应用程序之间创建一个管道。
这是我到目前为止的代码: http://pastebin.com/uupd4aXi
我想做的是将“whoami”写入标准输入并获得返回相当于该命令。 如果我注释掉 writeinput 函数,则 readoutput 会成功打印标准 cmd 打印输出。但是,如果我不这样做,writeinput 就会陷入无限循环:
for (;;)
{
bSuccess = WriteFile(hSTD_IN_WRITE, chBuf, sizeof(chBuf), &dwWritten, NULL);
if ( ! bSuccess ) break;
}
如果我删除 if 语句并手动导致循环中断,我仍然只能收到 cmd 打印输出消息,但不会收到对我的命令“whoami”的响应。
我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在陷入 重定向 stdin 和 stout 的陷阱但按顺序处理它们。
如果您只想运行
whoami
程序并捕获输出,那么您不需要cmd.exe
并尝试泵whoami.exe< /code> 作为其输入。只需直接运行
whoami.exe
并捕获其输出即可。编辑:更新了文章的链接:
https://devblogs.microsoft.com/oldnewthing/20110707-00/? p=10223
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 needcmd.exe
and try to pumpwhoami.exe
as its input. Just runwhoami.exe
directly and capture its output.EDIT: updated link to the article:
https://devblogs.microsoft.com/oldnewthing/20110707-00/?p=10223