创建进程和重定向输出
CreateProcess and redirecting output
有 2 个应用程序。
AppCMD
是一个命令行应用程序,AppMAIN
使用一些命令行参数开始AppCMD
。 不幸的是,AppMAIN
似乎不能很好地处理输出AppCMD
并且出了点问题。
我想记录对AppCMD
及其输出的调用,以查看发生了什么。
为此,我想将AppCMD
替换为另一个二进制AppWRAP
,该将调用转发到重命名的AppCMD
并记录其输出。AppWRAP
应该表现得像一个透明的中间人。
出于测试目的,我编写了一个简单的AppCMD
,只输出它的命令行参数:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
cout << "#### Hello, I'm the test binary that wants to be wrapped." << endl;
if (argc < 2) {
cout << "#### There where no command line arguments." << endl;
}
else {
cout << "#### These are my command line arguments:";
for (int i = 1; i < argc; ++i) cout << " " << argv[i];
cout << endl;
}
cout << "#### That's pretty much everything I do ... yet ;)" << endl;
return 0;
}
我遵循 MSDN:使用重定向输入和输出创建子进程来实现AppWrap
但我卡住了,因为它没有返回,我无法弄清楚为什么:
#include <iostream>
#include <sstream>
#include <Windows.h>
using namespace std;
const string TARGET_BINARY("TestBinary.exe");
const size_t BUFFSIZE = 4096;
HANDLE in_read = 0;
HANDLE in_write = 0;
HANDLE out_read = 0;
HANDLE out_write = 0;
int main(int argc, char *argv[])
{
stringstream call;
cout << "Hello, I'm BinTheMiddle." << endl;
//-------------------------- CREATE COMMAND LINE CALL --------------------------
call << TARGET_BINARY;
for (int i = 1; i < argc; ++i) {
call << " " << argv[i];
}
cout << "Attempting to call '" << call.str() << "'" << endl;
//------------------------------ ARRANGE IO PIPES ------------------------------
SECURITY_ATTRIBUTES security;
security.nLength = sizeof(SECURITY_ATTRIBUTES);
security.bInheritHandle = NULL;
security.bInheritHandle = TRUE;
security.lpSecurityDescriptor = NULL;
if (!CreatePipe(&out_read, &out_write, &security, 0)) {
cout << "Error: StdoutRd CreatePipe" << endl;
return -1;
}
if (!SetHandleInformation(out_read, HANDLE_FLAG_INHERIT, 0)) {
cout << "Stdout SetHandleInformation" << endl;
return -2;
}
if (!CreatePipe(&in_read, &in_write, &security, 0)) {
cout << "Stdin CreatePipe" << endl;
return -3;
}
if (!SetHandleInformation(in_write, HANDLE_FLAG_INHERIT, 0)) {
cout << "Stdin SetHandleInformation" << endl;
return -4;
}
//------------------------------ START TARGET APP ------------------------------
STARTUPINFO start;
PROCESS_INFORMATION proc;
ZeroMemory(&start, sizeof(start));
start.cb = sizeof(start);
start.hStdError = out_write;
start.hStdOutput = out_write;
start.hStdInput = in_read;
start.dwFlags |= STARTF_USESTDHANDLES;
ZeroMemory(&proc, sizeof(proc));
// Start the child process.
if (!CreateProcess(NULL, (LPSTR) call.str().c_str(), NULL, NULL, TRUE,
0, NULL, NULL, &start, &proc))
{
cout << "CreateProcess failed (" << GetLastError() << ")" << endl;
return -1;
}
// Wait until child process exits.
WaitForSingleObject(proc.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(proc.hProcess);
CloseHandle(proc.hThread);
//----------------------------------- OUTPUT -----------------------------------
DWORD dwRead;
CHAR chBuf[127];
while (ReadFile(out_read, chBuf, 127, &dwRead, NULL)) {
cout << "Wrapped: " << chBuf << endl;
}
return 0;
}
好像在等ReadFile
回来。谁能发现我做错了什么?
我这样称呼二进制文件:
> shell_cmd_wrapper.exe param1 param2
这是控制台输出,但二进制文件不返回。
Hello, I'm BinTheMiddle.
Attempting to call 'TestBinary.exe param1 param2'
Wrapped:#### Hello, I'm the test binary that wants to be wrapped.
#### These are my command line arguments: param1 param2
#### That'sD
Wrapped: pretty much everything I do ... yet ;)
s to be wrapped.
#### These are my command line arguments: param1 param2
#### That'sD
(请忽略我不清除缓冲区)
调用CreateProcess
后关闭out_write
和in_read
句柄。否则,当管道为空时,out_read
上的ReadFile
将阻塞,因为即使在子通道退出后仍然存在潜在的编写器 - 当前进程中的out_write
句柄。
此外,正如 Harry Johnston 在评论中指出的那样,在从管道读取之前等待进程退出可能会导致死锁。如果管道填满,孩子将在WriteFile
上堵塞。
相关文章:
- 将标准输出重定向到ostream
- 在程序执行期间从标准输出重定向到自定义流
- 将 Tcl 中的命令输出重定向到文件时,文件以 DOS 格式生成
- 将 Linux system() 调用命令的输出重定向到仅可变的
- 将 ffmpeg 控制台输出重定向到C++中的字符串或文件
- Win32 GUI C(++) 应用将标准输出和标准输出重定向到磁盘上的同一文件
- 将线程之间的标准输入/输出重定向
- GDB Cygwin 不会将输出重定向到文件
- 如何将 printf 流输出重定向到.csv文件
- 将PowerShell.exe的输入和输出重定向到C 中的管道
- Windows Batch - 如何在将标准输出重定向到文件时添加时间戳
- 将一个进程标准输出重定向到管道将产生不同的运行结果
- 如何将我的应用程序生成的标准 Mac OS X 日志输出重定向到文件
- 正在将控制台输出重定向到同一解决方案中的MFC屏幕输出
- 函数 system() 中的输出重定向似乎不起作用
- 使用 ofstream 将控制台输出重定向到文件C++末尾
- 将 printf C++控制台输出重定向到 C#
- 将打印到控制台的函数输出重定向到字符串
- 为外壳输出重定向创建文件
- 将调试输出重定向到空流而不是 std::cerr