无法读取在cmd.exe管道下启动的进程的输出

Cannot read output of processes launched under cmd.exe pipe

本文关键字:启动 进程 输出 管道 exe 读取 cmd      更新时间:2023-10-16

我希望您的编程进展顺利。

由于我缺乏知识,我有一个问题希望能得到一个简单的答案。

我使用了这个问题中的代码-CreateProcess cmd.exe读/写管道死锁

一切都很顺利。

问题是,当我从cmd.exe shell运行其他需要交互的命令时,例如python或powershell,我会得到初始输出,然后不会向管道中写入任何内容。

这就是我的输入/输出:

static PCSTR commands[] = { "powershellrn", "dirrn", "helprn"};
ULONG n = RTL_NUMBER_OF(commands);
PCSTR* psz = commands;
do 
{
if (MessageBoxW(0,0, L"force close ?", MB_YESNO) == IDYES)
{
DisconnectNamedPipe(hFile);
break;
}
if (p = new U_IRP(&obj))
{
PCSTR command = *psz++;
p->Write(command, (ULONG)strlen(command) * sizeof(CHAR));
p->Release();
}
} while (--n)

当代码运行时,我会得到初始的powershell.exe提示,就像一样

PS C:Users>

但在那之后,没有任何内容被写入管道。

代码正在使用CreateProcess(…"cmd.exe"…),我已尝试将其从"cmd.exe"更改为"cmd.exe/c"answers"cmd.exe/k",两者都不起作用。

也许你知道我需要从CreateProcess()引发的管道读取/写入解释输出(如python或powershell)吗?谢谢你的帮助!

执行cmd.exe并通过管道将命令发送到execpowershell。然后全部依赖于powershell实现

在窗口7:

powershell使用ReadConsoleW获取输入。因此,它不使用您命名的管道,而不是从中读取。您可以注意到,在执行powershell后,控制台窗口将变得交互式。因此powershell不接受您向管道写入的内容(它根本不从中读取),而是从屏幕读取用户输入。但是,在手动向控制台输入一些命令并按enter键后,您可以获得管道输出-powershell使用(混合)-WriteFileWriteConsoleW进行输出。一些信息通过WriteFile输出,一些通过WriteConsoleW输出

在windows10:上

powershell使用ReadFile获取输入。以及CCD_ 7用于输出。所以它从管道中读取命令并将结果写入其中。一切都很完美。您还可以注意到,在这种情况下,控制台窗口是不活动的-您不能在其中输入任何文本(与win7不同)

所以代码都是完全可以的。问题只在于第三方程序如何读取和写入数据。如果它没有从你的管道中读取-你在这里什么都做不了