如何阅读控制台应用程序的输出?

时间:2012-05-29 16:18:08

标签: c++ winapi

我有一个第三方控制台应用程序打印几行并立即退出(或等待按下按键关闭 - 取决于使用的参数)。我想从我自己的控制台程序运行这个应用程序,并将其输出到我的缓冲区。我尝试过这种方法,但它不起作用:

....    
HANDLE stdRead, stdWrite;
SECURITY_ATTRIBUTES PipeSecurity;
ZeroMemory (&PipeSecurity, sizeof (SECURITY_ATTRIBUTES));
PipeSecurity.nLength = sizeof (SECURITY_ATTRIBUTES);
PipeSecurity.bInheritHandle = true;
PipeSecurity.lpSecurityDescriptor = NULL;

CreatePipe (&stdRead, &stdWrite, &PipeSecurity, NULL)

STARTUPINFO sinfo;
ZeroMemory (&sinfo, sizeof (STARTUPINFO));
sinfo.cb = sizeof (STARTUPINFO);
sinfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
sinfo.hStdInput = stdWrite;
sinfo.hStdOutput = stdRead;
sinfo.hStdError = stdRead;
sinfo.wShowWindow = SW_SHOW;
CreateProcess (NULL, CommandLine, &PipeSecurity, &PipeSecurity, TRUE, NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE | CREATE_UNICODE_ENVIRONMENT, NULL, NULL, &sinfo, &pi))

DWORD dwRetFromWait= WAIT_TIMEOUT;
while (dwRetFromWait != WAIT_OBJECT_0)
{
    dwRetFromWait = WaitForSingleObject (pi.hProcess, 10);
    if (dwRetFromWait == WAIT_ABANDONED)
        break;

    //--- else (WAIT_OBJECT_0 or WAIT_TIMEOUT) process the pipe data
    while (ReadFromPipeNoWait (stdRead, Buffer, STD_BUFFER_MAX) > 0)
    {
        int iLen= 0; //just for a breakpoint, it never breaks here
    }
}
....


int ReadFromPipeNoWait (HANDLE hPipe, WCHAR *pDest, int nMax)
{
DWORD nBytesRead = 0;
DWORD nAvailBytes;
WCHAR cTmp [10];

ZeroMemory (pDest, nMax * sizeof (WCHAR));
// -- check for something in the pipe
PeekNamedPipe (hPipe, &cTmp, 20, NULL, &nAvailBytes, NULL);
if (nAvailBytes == 0)
    return (nBytesRead); //always ends here + cTmp contains crap

// OK, something there... read it
ReadFile (hPipe, pDest, nMax-1, &nBytesRead, NULL); 

return nBytesRead;
}

如果我删除PeekNamedPipe,它只会挂起在ReadFile上并且什么都不做。任何想法可能是错的?管道不幸是我的一杯茶,我只是将一些代码放在网上。

非常感谢。

1 个答案:

答案 0 :(得分:2)

我从一个简单的方法开始:

char tmp[1024];
std::string buffer;

FILE *child = _popen("child prog.exe", "r");

if (NULL == child)
    throw std::runtime_error("Unable to spawn child program");

while (fgets(tmp, sizeof(tmp), child))
   buffer += tmp;

只有当你发现它不起作用时,做一些更复杂的事情来解决你遇到的具体问题。