Chinaunix首页 | 论坛 | 博客
  • 博客访问: 608640
  • 博文数量: 263
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 2555
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-26 11:20
文章分类

全部博文(263)

文章存档

2011年(10)

2010年(19)

2009年(170)

2008年(64)

我的朋友

分类: WINDOWS

2009-10-14 23:01:35

MFC程序利用控制台输出调试信息
 
        近日研究师兄的一个MFC程序,见其会生成一个控制台窗口输出信息,就如同ANSYS的Output窗口,觉得这个功能实在有用。
        于是研究了一下他的代码,不过因为其为DLL工程,又可怜我的VC6打不开他的VS2005的Test工程,原先的工程没有办法编通过。
        于是想把实现此功能的类拆出来,仔细研究之后,做了个Test工程,居然不好用,不知道哪里出了问题。
        于是google之,有几个结果可供参考:
(1)Creating a console for your MFC app's debug output[]
(2)MFC 利用控制台输出调试信息[http://writeblog.csdn.net/PostEdit.aspx?entryId=1842880]
(3) MFC/DLL 编程时用独立的控制台窗口显示用户自定义调试信息[这篇文章因为被转载多次,找不到出处,google标题即可找到]
(4) GUI程序也能使用控制台窗口[]
 
        综合一下,其实现的方法基本一致。
                1)调用AllocConsole()函数,创建一个Console;
                2)调用_cprintf()函数,输出字符串;
                        或者,重定向输出流(详见下文);
                3)调用FreeConsole()函数,释放Console。
 
        为了使用方便,参考了师兄使用的“单身模式”,写了下面的class。
  1. //.h  
  2. class CConsolePrinter    
  3. {  
  4. public:  
  5.     void print(const char* str);  
  6.     static void Destroy();  
  7.     static CConsolePrinter* Instance();  
  8.       
  9.       
  10.     virtual ~CConsolePrinter();  
  11.   
  12. protected:  
  13.     CConsolePrinter();  
  14.   
  15. private:  
  16.     static CConsolePrinter* _instance;  
  17.     FILE *file;  
  18.   
  19. };  
  1. class="csharp" name="code">//.cpp  
  2. CConsolePrinter* CConsolePrinter::_instance = 0;  
  3.   
  4. CConsolePrinter::CConsolePrinter()  
  5. {  
  6.     // create a new console to the process  
  7.     AllocConsole();  
  8.       
  9.     int hCrun;      
  10.     hCrun = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);  
  11.     file  = _fdopen(hCrun, "w");  
  12.       
  13.     // use default stream buffer  
  14.     setvbuf(file, NULL, _IONBF, 0);  
  15.     *stdout = *file;  
  16.   
  17.     std::cout << "Ready!\n";  
  18. }  
  19.   
  20. CConsolePrinter::~CConsolePrinter()  
  21. {  
  22.     FreeConsole();  
  23.     fclose(file);  
  24.   
  25. }  
  26.   
  27. CConsolePrinter* CConsolePrinter::Instance()  
  28. {  
  29.     if (_instance == 0)  
  30.     {  
  31.         _instance = new CConsolePrinter;  
  32.     }  
  33.     return _instance;  
  34. }  
  35.   
  36. void CConsolePrinter::Destroy()  
  37. {  
  38.     if (_instance)  
  39.     {  
  40.         delete _instance;  
  41.     }  
  42.     _instance = 0;  
  43. }  
  44.   
  45. void CConsolePrinter::print(const char *str)  
  46. {  
  47.     std::cout << str << std::endl;  
  48. }  
  49.   
  50. class="csharp" name="code">  
        使 用方法也很简单。
        首先在应用程序类的InitInstance()函数中添加 CConsolePrinter::Instance();,例如:
    class="csharp" name="code">BOOL CMfcWithConsoleApp::InitInstance()  
  51. {  
  52.     // details omitted  
  53.   
  54.     // allocate a console  
  55. #ifdef _DEBUG  
  56.     CConsolePrinter::Instance();  
  57. #endif  
  58.   
  59.                      // details omitted  
  60.   
  61.     // The one and only window has been initialized, so show and update it.  
  62.     m_pMainWnd->ShowWindow(SW_SHOW);  
  63.     m_pMainWnd->UpdateWindow();  
  64.   
  65.     return TRUE;  
  66. }  
  67.   
  68. class="csharp" name="code">  
        第二步在应用程序类的ExitInstance()函数中添加CConsolePrinter::Destroy();,例如:
    class="csharp" name="code">int CMfcWithConsoleApp::ExitInstance()   
  69. {  
  70.     // TODO: Add your specialized code here and/or call the base class  
  71.  
  72. #ifdef _DEBUG  
  73.     CConsolePrinter::Destroy();  
  74. #endif  
  75.       
  76.     return CWinApp::ExitInstance();  
  77. }  
  78.   
  79.   
        然后就是在需要输出字符串信息的地方调用CConsolePrinter::Instance()->print();函数。
    class="csharp" name="code">void CMfcWithConsoleDoc::OnEditCopy()   
  80. {  
  81.     // TODO: Add your command handler code here  
  82. #ifdef _DEBUG  
  83.     CConsolePrinter::Instance()->print("CMfcWithConsoleDoc::OnEditCopy() ");  
  84. #endif  
  85.       
  86. }  
  87.   
  88.     需要注意的是,如果关闭控制台窗口会导致主程序退出。  
  
  •   
  •   
  •   
  •   
  •  
  • 阅读(1171) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~