A lot of IDE's capture the output from some command line utility, displaying it in a window on the screen. Any hints on how to go about 'capturing' the output?
Printable View
A lot of IDE's capture the output from some command line utility, displaying it in a window on the screen. Any hints on how to go about 'capturing' the output?
BOOL PipeResult( const CString& CommandLine, CString& strOutFile )
{
STARTUPINFO si;
memset( &si, 0, sizeof( si ) );
si.cb = sizeof( si );
// Hide DOS window
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
// Prepare pipe handles for standard output redirection
SECURITY_ATTRIBUTES saAttr;
// Set the bInheritHandle flag so pipe handles are inherited.
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
HANDLE hReadPipe, hWritePipe;
BOOL res = CreatePipe( &hReadPipe, &hWritePipe, &saAttr, 0 );
if ( !res )
{
AfxMessageBox(_T("Problem pipe creation"));
}
else
{
si.dwFlags |= STARTF_USESTDHANDLES;
si.hStdOutput = hWritePipe;
PROCESS_INFORMATION pi;
res = CreateProcess( NULL, (LPTSTR)(LPCTSTR)CommandLine, NULL, NULL, TRUE,
0, NULL, NULL, &si, &pi );
if (!res)
{
// process error of rsh
AfxMessageBox(_T("Problem optimizer running"));
}
else
{
// Close write pipe
CloseHandle( hWritePipe );
DWORD NumberOfBytesRead;
TCHAR Buffer[ 257 ];
while(::ReadFile( hReadPipe, Buffer, sizeof(Buffer)/sizeof(Buffer[ 0 ])-1,
&NumberOfBytesRead, NULL ) )
{
if (NumberOfBytesRead)
{
Buffer[ NumberOfBytesRead ] = TCHAR( 0 );
strOutFile += Buffer;
}
else
break;
};
// Close read pipe and wait for the finish
CloseHandle( hReadPipe );
WaitForSingleObject( pi.hProcess, INFINITE );
res = TRUE;
}
}
return res;
}
Rating isn't important...But gurus respect it and keep high
Thanks for the code.
I tried it and everything works fine.
I dont know but I am reaching the
section only when the child process ( it is a console app ) is done with and it has outputted everything in the stdout. Therefore, I can show output only after the whole process is over.Code:if (NumberOfBytesRead)
{
Buffer[ NumberOfBytesRead ] = TCHAR( 0 );
strOutFile += Buffer;
}
else
break;
What I require is that I get a call whenever the exe writes something in the console, so that i can get the text and add it to my edit box.
Something like VC++, which gives information about compiling of each and every .cpp as it is compiled? I mean...one line after another?
Any help?
Now MSDN offers better code under the title
"Creating a Child Process with Redirected Input and Output"
Usage PeekNamedPipe could help as well
I have tried that. Same problem. It is outputting after the child process gets over.
I tried google groups..and none of the sample codes behave in an interactive manner where they show the output as they get it.
I will try PeekNamedPipe.....havnt used Named Pipes till now? Any code sample to try out...
Thanks for your response.
1. PeekNamedPipe is applicable to anonymous pipe as well
2. Probably the problem is your child process
The child process is doing around 10 same type of jobs. Each job is computationally complex and it takes around 2min to finish one job. After that it outputs the result of each job on stdout using *printf* function.
After the process is over say after 20 minutes..all the result comes in a hurry...?
Any guesses?
I assume that you output job results upon finishing job.
Try to use fflush(NULL) for child process at the end of job
THANK YOU THANK YOU THANK YOU.
fflush ( NULL ) didi the trick?
Well i am not a consol guy? Can you tell me the reason?
Because console output( as well as other file output ) is buffered