来源: 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(); } }








































- 1


- 粉丝: 1
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 洛阳网站建设方案(1).docx
- 互联网+时代高职院校人力资源管理创新路径探索(1).docx
- C++课程设计报告(学生学籍管理系统)(1).doc
- 电脑打开网页flash不停闪问题完美解决方案(1).doc
- 吉林大学C语言习题(1).doc
- 基于光谱技术结合计算机信息处理技术鉴别机油品种的研究的论文-计算机应用论文(1).docx
- 【推荐下载】一个自动化系统集商成功的故事(1).pdf
- 信息化时代农商银行合规管理机制建设研究(1).docx
- 刍议计算机病毒的防范措施(1).docx
- 农业机械自动化发展中存在的问题及对策(1).docx
- 2023年操作系统软件行业市场需求分析报告及未来五至十年行业预测报告(1).docx
- 2020年重庆专升本计算机练习题三套(1).pdf
- 电气自动化专业实习报告(3000字)(1).docx
- 轻轨转向架的结构设计与分析毕业设计--机械制造与自动化论文(1)(1).doc
- 浅析搭建信息运维管理系统项目推动信息化管理(1).docx
- 智能识别系统软件项目节能评估报告模板(1).docx


