文件名称:
Redirecting Standard Output to a CEdit Control
开发工具:
文件大小: 23kb
下载次数: 0
上传时间: 2010-06-24
详细说明: 来源: 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(); } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.