Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a child process with stdout to a file

Status
Not open for further replies.

Mukiwa

Programmer
Feb 13, 2001
3
IL
Is it possible to create a child process (using CreateProcess) so that it's standard output is changed to a specific file ?

Thanks
Dave.
 
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));

if( dwAvail > 0 )
{
pipe_data = ReadFile( hPipeInputRead, chBuf, 255, &dwRead, NULL);
result += chBuf;
}
}
while( dwAvail > 0 || !process_finished );

// Close the process and pipe handles
CloseHandle (pi.hProcess);
CloseHandle (pi.hThread);
CloseHandle( hPipeInputRead );
CloseHandle( hPipeOutputWrite );
}

return result;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top