Redirecting Standard Output to a CEdit Control
文件大小: 23k
源码售价: 10 个金币 积分规则     积分充值
资源说明:来源: http://www.codeguru.com/Cpp/misc/misc/article.php/c321/ 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. Post a comment Email Article Print Article Share Articles Digg del.icio.us Newsvine Facebook Google LinkedIn MySpace Reddit Slashdot StumbleUpon Technorati Twitter Windows Live YahooBuzz FriendFeed 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(); } }
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。