2016-08-12 74 views
0

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

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

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

为什么我需要这个?我想开始tshark(控制台版本Wireshark,流量嗅探器)与bWait = false,然后启动我自己的实用程序与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代码吗?不幸的是,我无法找到(以及在哪里)。

回答

0

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

应用PIPE_NOWAIT看完后停止挂。