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

Reading from child console pipe hanging under some conditions

本文关键字:管道 控制台 条件下      更新时间:2023-10-16

在我的Win32程序中,我实现了执行控制台应用程序并读取其std/err输出。基本上,它与MSDN中给出的代码相同:使用重定向的输入和输出创建子进程

到目前为止,一切都很好。它就像一个魅力,用我所有的控制台应用程序读取std和err流。但很明显(由于全局HANDLE变量(,代码被设计为一个接一个地运行控制台应用程序,而不是一起运行。所以我改变了一点:

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

为什么我需要这个?我想用bWait = false启动tshart(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"rn"));
    ::MultiByteToWideChar(866, 0, (LPCCH) err.c_str(), -1, utf, sizeof(utf));
    errS = utf;
    StringReplace(errS, std::wstring(L"n"), std::wstring(L"rn"));
}
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:rnrn";
        msg += tsharkErr;
        ::MessageBox(NULL, msg.c_str(), L"uhelper", MB_ICONERROR | MB_OK);
    }
    else if (traffic.length() > 0)
    {
        newOutput += L"rnTraffic to ";
        newOutput += g_Products[i].PingbackDomain;
        newOutput += L":rn";
        newOutput += traffic;
        if (newOutput[newOutput.length() - 1] != L'n')
            newOutput += L"rn";
    }
}

我的修改是否破坏了MSDN代码?不幸的是,我找不到如何(以及在哪里(。

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

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后,读数停止挂起。