无法读取在cmd.exe管道下启动的进程的输出
Cannot read output of processes launched under cmd.exe pipe
我希望您的编程进展顺利。
由于我缺乏知识,我有一个问题希望能得到一个简单的答案。
我使用了这个问题中的代码-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使用(混合)-WriteFile
和WriteConsoleW
进行输出。一些信息通过WriteFile
输出,一些通过WriteConsoleW
输出
在windows10:上
powershell使用ReadFile
获取输入。以及CCD_ 7用于输出。所以它从管道中读取命令并将结果写入其中。一切都很完美。您还可以注意到,在这种情况下,控制台窗口是不活动的-您不能在其中输入任何文本(与win7不同)
所以代码都是完全可以的。问题只在于第三方程序如何读取和写入数据。如果它没有从你的管道中读取-你在这里什么都做不了
- 在挂钩启动新线程时解除挂钩进程
- 如何制作启动 Python 脚本的 linux 后台进程(在 c 中)
- 由 JOB 中的进程启动的子进程是否可以将 JOB 属性设置为脱离作业?
- 通过服务启动进程后,从SHGetKnownFolderPath 0x80070005错误
- 如何在启动时将 gdbserver 附加到进程?
- 在C++中启动进程时隐藏命令提示符
- 如何正确启动进程并转发 stdin/stdout/stderr
- 以不同用户身份启动进程
- 使用 posix_spawn 启动进程
- 当java作为服务运行时,从java启动进程
- 当用std::system启动进程时,我可以指定一个工作目录吗
- 在窗口错误中使用QProcess启动进程:"Timers can only be used with threads started with QThread"
- 创建进程 我无法启动进程
- Windows 7或Windows 2008如何在本地系统帐户或系统上下文中启动进程(从桌面应用程序)
- 在派生的线程中启动进程
- Windows服务启动进程结束通知(20032008)
- 无法使用参数启动进程
- 提示重新启动进程以获得管理权限
- 如何在 C、C++ 的 Linux 操作系统上启动进程
- 在linux上用c++启动进程,使用环境变量c++