从儿童控制台管道读取在某些条件下垂悬

时间:2016-08-12 15:33:17

标签: c++ winapi pipe console-application

在我的Win32程序中,我实现了执行控制台应用程序并读取其std / err输出。基本上它与MSDN中给出的代码相同:Creating a Child Process with Redirected Input and Output

到目前为止,这么好。它就像一个魅力,用我的所有控制台应用程序读取std和err流。但显然(由于全局HANDLE变量)代码被设计为逐个运行控制台应用程序,从不在一起。所以我稍微改了一下:

  • 全局HANDLE变量替换为本地变量。它们被传递给辅助函数。
  • 添加了名为bWait的参数。如果它是false,启动后没有从控制台管道读取,也没有等待进程句柄(异步的味道)。
  • 相反,读取句柄返回给调用者(通过给定的指针)。它们可以用于稍后从管道中读取。

为什么我需要这个?我想用bWait = false启动 tshark Wireshark 的控制台版本,一个流量嗅探器),然后用bWait = true启动我自己的实用程序并等待直到我的公用事业停止工作然后我想检查我的实用程序是否ping服务器。 (由于我们有很多实用程序,这将是我们自动测试程序的重要功能)。因此,我想在此之后阅读 tshark 控制台管道并解析其日志。

以下是我的修改:

// Create a child process that uses the previously created pipes
//  for STDERR and STDOUT.
PROCESS_INFORMATION CreateChildProcess(HANDLE hChildStd_OUT_Wr, HANDLE     hChildStd_ERR_Wr,
const std::wstring& cmd, bool& bSuccess, DWORD& exitCode, DWORD& lastError, bool bWait = true)
{
// Set the text I want to run
//char szCmdline[]="test --log_level=all --report_level=detailed";


    bSuccess = false;

    wchar_t wrBuffer[BUFSIZE];
    ::wcscpy_s(wrBuffer, cmd.c_str());

    PROCESS_INFORMATION piProcInfo;
    STARTUPINFO siStartInfo;

    // Set up members of the PROCESS_INFORMATION structure. 
    ZeroMemory(&piProcInfo, sizeof(PROCESS_INFORMATION));

    // Set up members of the STARTUPINFO structure. 
    // This structure specifies the STDERR and STDOUT handles for redirection.
    ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
    siStartInfo.cb = sizeof(STARTUPINFO);
    siStartInfo.hStdError = hChildStd_ERR_Wr;
    siStartInfo.hStdOutput = hChildStd_OUT_Wr;
    siStartInfo.dwFlags |= STARTF_USESTDHANDLES;

    // Create the child process. 
    bSuccess = CreateProcess(NULL,
        wrBuffer,      // command line 
        NULL,          // process security attributes 
        NULL,          // primary thread security attributes 
        TRUE,          // handles are inherited 
        0,             // creation flags 
        NULL,          // use parent's environment 
        NULL,          // use parent's current directory 
        &siStartInfo,  // STARTUPINFO pointer 
        &piProcInfo) != 0;  // receives PROCESS_INFORMATION

    if (!bSuccess)
    {
        lastError = ::GetLastError();
    }
    else
    {
        lastError = 0;
    }

    if (bWait && bSuccess && ::WaitForSingleObject(piProcInfo.hProcess, INFINITE) == WAIT_FAILED)
    {
        bSuccess = false;
    }

    if (bWait && FALSE == ::GetExitCodeProcess(piProcInfo.hProcess, &exitCode))
    {
        bSuccess = false;
    }

    if (bWait)
    {
        ::CloseHandle(hChildStd_ERR_Wr);
        ::CloseHandle(hChildStd_OUT_Wr);
    }

    return piProcInfo;
}

// Read output from the child process's pipe for STDOUT
// and write to the parent process's pipe for STDOUT. 
// Stop when there is no more data. 
void ReadFromPipe(HANDLE hChildStd_OUT_Rd, HANDLE hChildStd_ERR_Rd, std::wstring& stdS, std::wstring& errS)
{
    DWORD dwRead;
    CHAR chBuf[BUFSIZE];
    bool bSuccess = FALSE;
    std::string out = "", err = "";
    for (;;)
    {
        bSuccess = ReadFile(hChildStd_OUT_Rd, chBuf, BUFSIZE, &dwRead, NULL) != 0;
        if (!bSuccess || dwRead == 0) break;

        std::string s(chBuf, dwRead);
        out += s;
    }
    dwRead = 0;
    for (;;)
    {
        bSuccess = ReadFile(hChildStd_ERR_Rd, chBuf, BUFSIZE, &dwRead, NULL) != 0;
        if (!bSuccess || dwRead == 0) break;

        std::string s(chBuf, dwRead);
        err += s;
    }

    wchar_t utf[10000] = { 0 };

    ::MultiByteToWideChar(866, 0, (LPCCH) out.c_str(), -1, utf, sizeof(utf));
    stdS = utf;
    StringReplace(stdS, std::wstring(L"\n"), std::wstring(L"\r\n"));

    ::MultiByteToWideChar(866, 0, (LPCCH) err.c_str(), -1, utf, sizeof(utf));
    errS = utf;
    StringReplace(errS, std::wstring(L"\n"), std::wstring(L"\r\n"));
}

bool ExecuteCmd(std::wstring cmd, std::wstring& std, std::wstring& err, std::wstring& code, DWORD& lastError,
                bool bWait = true, HANDLE* phChildStd_OUT_Rd = nullptr, HANDLE* phChildStd_ERR_Rd = nullptr)
{
    HANDLE hChildStd_OUT_Rd = NULL;
    HANDLE hChildStd_OUT_Wr = NULL;
    HANDLE hChildStd_ERR_Rd = NULL;
    HANDLE hChildStd_ERR_Wr = NULL;

    SECURITY_ATTRIBUTES sa;

    // Set the bInheritHandle flag so pipe handles are inherited. 
    sa.nLength = sizeof(SECURITY_ATTRIBUTES);
    sa.bInheritHandle = TRUE;
    sa.lpSecurityDescriptor = NULL;

    // Create a pipe for the child process's STDERR. 
    if (!CreatePipe(&hChildStd_ERR_Rd, &hChildStd_ERR_Wr, &sa, 0))
    {
        return false;
    }

    // Ensure the read handle to the pipe for STDERR is not inherited.
    if (!SetHandleInformation(hChildStd_ERR_Rd, HANDLE_FLAG_INHERIT, 0))
    {
        return false;
    }

    // Create a pipe for the child process's STDOUT.
    if (!CreatePipe(&hChildStd_OUT_Rd, &hChildStd_OUT_Wr, &sa, 0))
    {
        return false;
    }

    // Ensure the read handle to the pipe for STDOUT is not inherited
    if (!SetHandleInformation(hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0))
    {
        return false;
    }

    // Create the child process.
    bool bSuccess = false;
    DWORD dwExitCode = 9999;
    PROCESS_INFORMATION piProcInfo = CreateChildProcess(hChildStd_OUT_Wr, hChildStd_ERR_Wr, cmd, bSuccess, dwExitCode, lastError, bWait);

    if (phChildStd_OUT_Rd)
        *phChildStd_OUT_Rd = hChildStd_OUT_Rd;
    if (phChildStd_ERR_Rd)
        *phChildStd_ERR_Rd = hChildStd_ERR_Rd;

    if (!bWait)
        return true;

    wchar_t buffer[10] = { 0 };
    code = ::_itow((int) dwExitCode, buffer, 10);

    if (!bSuccess)
    {
        return false;
    }

    // Read from pipe that is the standard output for child process.
    ReadFromPipe(hChildStd_OUT_Rd, hChildStd_ERR_Rd, std, err);
    ::CloseHandle(hChildStd_OUT_Rd);
    ::CloseHandle(hChildStd_ERR_Rd);

    return true;
}

现在,问题。当我在无等待模式下尝试启动 tshark 时,从管道读取的内容被挂起。即,ReadFile

if (g_iConnection != -1 && g_Products[i].PingbackDomain.size() > 0)
    {
        wchar_t buf[5] = { 0 };
        std::wstring list, err, code;
        DWORD dwErr = 0;
        std::wstring cmd = L"C:\\Program Files\\Wireshark\\tshark -a duration:5 -l -i ";
        cmd += ::_itow(g_iConnection + 1, buf, 10);
        cmd += L" -f \"host ";
        cmd += g_Products[i].PingbackDomain;
        cmd += L"\"";
        ExecuteCmd(cmd, list, err, code, dwErr, false, &hChildStd_OUT_Rd, &hChildStd_ERR_Rd);
        ::Sleep(500);
    }
...
// Starting my utility (if this section is commented out, ReadFile still hangs).
...
if (hChildStd_OUT_Rd && hChildStd_ERR_Rd)
{
    std::wstring traffic, tsharkErr;
    ReadFromPipe(hChildStd_OUT_Rd, hChildStd_ERR_Rd, traffic, tsharkErr);
    ::CloseHandle(hChildStd_OUT_Rd);
    ::CloseHandle(hChildStd_ERR_Rd);

    if (tsharkErr.size() > 0)
    {
        std::wstring msg = L"There has been an issue, while logging with Wireshark:\r\n\r\n";
        msg += tsharkErr;
        ::MessageBox(NULL, msg.c_str(), L"uhelper", MB_ICONERROR | MB_OK);
    }
    else if (traffic.length() > 0)
    {
        newOutput += L"\r\nTraffic to ";
        newOutput += g_Products[i].PingbackDomain;
        newOutput += L":\r\n";
        newOutput += traffic;

        if (newOutput[newOutput.length() - 1] != L'\n')
            newOutput += L"\r\n";
    }
}

我是否通过修改打破了MSDN代码?不幸的是,我无法找到(以及在哪里)。

1 个答案:

答案 0 :(得分:0)

这解决了问题(在创建进程之前!):

if (!bWait)
    {
        DWORD mode = PIPE_READMODE_BYTE | PIPE_NOWAIT;
        ::SetNamedPipeHandleState(hChildStd_OUT_Rd, &mode, NULL, NULL);
        ::SetNamedPipeHandleState(hChildStd_ERR_Rd, &mode, NULL, NULL);
    }

应用PIPE_NOWAIT阅读停止挂起。