CreateProcess和重定向输出

时间:2017-09-22 09:32:48

标签: c++ windows winapi cmd

有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: Creating a Child Process with Redirected Input and Output实施了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

(请忽略我不清除缓冲区)

1 个答案:

答案 0 :(得分:3)

致电out_write后关闭in_readCreateProcess句柄。否则ReadFile上的out_read将在管道为空时阻止,因为即使在孩子退出后仍有潜在的作者 - 当前流程中的out_write句柄。

另外,正如Harry Johnston在评论中指出的那样,在从管道读取之前等待进程退出可能会导致死锁。如果管道填满,孩子将在WriteFile上阻止。