Redirecting Standard Output to a CEdit Control

Environment:
The demo was built with Microsoft Developer Studio 97 (Visual C++ 5.0)
and has been tested with Windows 95, Windows 98 and Windows NT 4.0.

Applications sometimes
need to create a child process whose output may, by default, be written
to a console window. Running a batch file is a good example of this. In
many cases the console window goes away as soon as the child process exits.
At times it would be useful if there was a way to redirect the output of
the child process to a CEdit control.

A good example of the desired behavior is the output generated by tools
executed from the Tools menu of DevStudio. A tool’s output is written to
the Output window, and the user can copy and clear text from this window.
Moreover, the output from the tool is displayed as it is being written,
instead of all at once when the tool exits.

It would be nice if Microsoft had provided an example of how to do this,
but such is not the case. The only example that I could find in the Online
Documentation is the one titled “Creating a Child Process with Redirected
Input and Output”. This example shows how to create a child process and
redirect its standard output to an anonymous pipe. The parent process writes
and then reads to the pipe, while the child process reads and then writes
to the pipe, and that’s it. This example falls short of demonstrating how
to use a loop to read data from a pipe whenever data is available, and
to exit the loop when the child process has exited.

I spent a good amount of time searching newsgroups and web sites for
examples but never found any that worked to my liking. Then I came across
a post to the microsoft.public.vc.mfc
newsgroup, submitted by Dima Shamroni,
that provides the essential code. I have used this code as a basis for
the following class, CRedirect.

The class CRedirect creates a child process and redirects its standard
output and standard error to a CEdit control. Both the command line for
the process and the CEdit control are specified by the caller.

Using the class is simple, as shown in the following:


CRedirect Redirect(“C:\Temp\sample.bat”, m_Edit);
Redirect.Run();

A fancier way of using
this class is to dynamically allocate an object of CRedirect and provide
a Stop button that allows you to terminate the child process, as shown
in the following (an abbreviated version of what’s used in the demo):


CRedirectDemoDlg::OnButtonRun()
{
m_pRedirect = new CRedirect(“C:\Temp\sample.bat”, m_Edit);
m_pRedirect->Run();
delete m_pRedirect;
m_pRedirect = 0;
}

CRedirectDemoDlg::OnButtonStop()
{
if ( m_pRedirect )
{
m_pRedirect->Stop();
}

The CRedirect::Run()
method blocks the caller. However, windows still receive messages because
within the CRedirect::Run() method there is a loop that both reads
data from a pipe and calls the method listed below to process window messages.


CRedirect::PeekAndPump()
{
MSG Msg;
while ( ::PeekMessage(&Msg, NULL, 0, 0, PM_NOREMOVE) )
{
(void)AfxGetApp()->PumpMessage();
}
}

Download demo project – 23 KB

Download source – 4 KB

Date Last Updated: March 4, 1999

More by Author

Must Read