1

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

....    
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 上并且什么都不做。有什么想法可能是错的吗?不幸的是,管道不是我的一杯茶,我只是整理了一些在 Internet 上找到的代码。

非常感谢。

4

1 回答 1

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;

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

于 2012-05-29T16:25:56.240 回答