如何阅读控制台应用程序的输出?
问题描述:
我有第三方控制台应用程序,打印几行和立即退出(或等待按键被关闭 - 取决于使用的参数)。我想从我自己的控制台程序运行此应用程序,并将其输出到我的缓冲区。我试过这种方法,但它不起作用:如何阅读控制台应用程序的输出?
....
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上,什么也不做。任何想法可能是错误的?管道不是不幸的我的一杯茶,我只是把在互联网上找到的一些代码放在一起。
非常感谢。只有当你发现不工作,做一些更复杂的解决您遇到的特定问题(S)
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;
:
在命令提示符处输入'appname> output.txt 2>&1'并查看输出是否被重定向到文本文件。如果是这样,你的代码有问题。否则,你需要一种不同的方法。 – arx