Stdin / Stdout重定向,输入无效

问题描述 投票:0回答:1

我正在尝试在命令行外壳和应用程序之间创建管道。

这是我到目前为止的代码:http://pastebin.com/uupd4aXi

[我想做的是在stdin中写“ whoami”,并获得与该命令等效的返回值。如果我注释掉writeinput函数,则readoutput成功打印出标准的cmd打印输出。但是,如果我不这样做,writeinput会陷入无限循环:

for (;;) 
    { 

        bSuccess = WriteFile(hSTD_IN_WRITE, chBuf, sizeof(chBuf), &dwWritten, NULL);
        if ( ! bSuccess ) break; 
    } 

如果我删除if语句并手动导致循环中断,我仍然只会收到cmd打印输出消息,而没有响应我的命令“ whoami”。

我在做什么错?

c cmd stdout pipe stdin
1个回答
1
投票

您正在进入redirecting both stdin and stout but processing them serially的陷阱。

如果您只想运行whoami程序并捕获输出,那么就不需要cmd.exe并尝试泵送whoami.exe作为其输入。只需直接运行whoami.exe并捕获其输出即可。

编辑:文章的更新链接:https://devblogs.microsoft.com/oldnewthing/20110707-00/?p=10223

© www.soinside.com 2019 - 2024. All rights reserved.