몇 줄을 인쇄하고 즉시 종료되는 타사 콘솔 앱이 있습니다 (또는 사용 된 인수에 따라 키가 닫힐 때까지 대기). 내 자신의 콘솔 프로그램 에서이 응용 프로그램을 실행하고 내 버퍼에 출력을하고 싶습니다. 나는이 방법을 시도했지만 작동하지 않습니다 : 나는 PeekNamedPipe을 제거하면콘솔 앱의 출력을 읽는 방법은 무엇입니까?
....
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;
}
, 그냥에서 ReadFile에 달려 아무것도하지 않습니다. 어떤 아이디어가 잘못되었을 수 있습니까? 파이프는 불행히도 차 한 잔, 나는 인터넷에서 발견 된 코드 중 일부를 조합했습니다.
고마워요.
명령 프롬프트에서'appname> output.txt 2> & 1'을 입력하고 출력이 텍스트 파일로 리디렉션되는지 확인하십시오. 그렇다면 코드에 문제가 있습니다. 그렇지 않으면 다른 접근 방식이 필요합니다. – arx