Did you find any answers? I'd like to create a process and redirect its standard error stream so that I can read it and show the percentage completion, based on key words. I'm not sure how to do this.
The process is quite simple and goes something like:
Use CreateFile to obtain a handle to a file with write access. Use the lpSecurityAttributes parameter to specify a SECURITY_ATTRIBUTES structure. Set the bInheritHandle member of this structure to TRUE.
Set the fInheritHandles parameter of CreateProcess to TRUE.
Use the lpStartupInfo parameter of CreateProcess to specify a STARTUPINFO structure. Set the dwFlags member of this structure to STARTF_USESTDHANDLES. Set the hStdOutput member to the handle obtained previously.
Could somebody help me write this portion of code. I'm not to familiar with handles, security attributes, etc. I'm pretty good with c++ but visual is new to me. If somebody could write a portion of code that executes a program and reads its output as a stream in real-time, I would love you forever. Otherwise if you could walk me through it I would appreciate that also.
I wrote this function a while back which you may find useful:
CString SpawnProcessReturnStdOut( char *command_line )
{
CString result = "";
// Setup default pipe security with the bInheritHandle flag set to
// allow pipe handles to be inherited by the child process.
SECURITY_ATTRIBUTES saAttr;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
// Create the anonymous pipe and store read/write handles
HANDLE hPipeInputRead, hPipeOutputWrite;
CreatePipe( &hPipeInputRead,
&hPipeOutputWrite,
&saAttr,
0 );
// Start up the child process
BOOL bRet = FALSE;
STARTUPINFO si = {0};
PROCESS_INFORMATION pi = {0};
// Make child process use the anonymous pipe for stdin and stdout
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
si.hStdInput = hPipeInputRead;
si.hStdOutput = hPipeOutputWrite;
bRet = ::CreateProcess( NULL,
command_line,
NULL,
NULL,
TRUE,
0,
NULL,
NULL,
&si,
&pi );
if (bRet)
{
// Read the contents of the pipe
DWORD dwRead, dwAvail;
CHAR chBuf[256];
BOOL pipe_data = true;
BOOL process_finished;
do
{
process_finished = (WaitForSingleObject (pi.hProcess, 0) == WAIT_OBJECT_0);
// Peek to see if there is any more data in the pipe
PeekNamedPipe( hPipeInputRead,chBuf,255,&dwRead,&dwAvail,NULL);
// Clear buffer to zero
memset(chBuf,0,sizeof(chBuf));
// Close the process and pipe handles
CloseHandle (pi.hProcess);
CloseHandle (pi.hThread);
CloseHandle( hPipeInputRead );
CloseHandle( hPipeOutputWrite );
}
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.