MFC 利用控制台输出调试信息(转)

方法一:
WINDOWS为你提供了一系列的API来完成这个功能,例如:ReadConsole,WriteConsole等,具体参见MSDN。 

1。首先调用AllocConsole函数来为你进程创建一个Console,该API是将当前进程Attache到一个新创建的Console上。你还可以通过调用SetConsoleTitle(tstrName);来设置Console的Title.

2。使用WriteConsoleOutput来将信息输出到Console上;在输出之前,先要得到Console的HANDLE,这通过 GetStdHandle(STD_OUTPUT_HANDLE)来得到,然后将信息组织成Console的格式,然后输出。

3。关闭CONSOLE。当不需要这个CONSOLE的时候,调用FreeConsole来将当前进程从Console中Detach中。 

4。通过创建一个进程来为监视你的CONSOLE输入和输出;你可以创建一个线程然后来,在线程中取得标准输入和输出CONSOLE的HANDLE,然后循环监视其事件,再对不同的事件进行处理。

第二种方案: 
在 Project | Setting...中,选项 Post-builder step 里新建command, 输入: 
editbin /SUBSYSTEM:CONSOLE $(OUTDIR)\filename.exe 
(其中 filename 为可执行文件名) 
则可以使用 printf 或者 std::cout 在控制台中输出了。例如你的可执行文件名为 HelloWorld.exe,则你新建的 command 就为 

editbin   /SUBSYSTEM:CONSOLE   $(OUTDIR)\HelloWorld.exe 
editbin /SUBSYSTEM:CONSOLE "D:\lizhicai\GUI\S1400\1stStation\Debug\S1400GUI.exe"
第三种方案,就用TRACE,然后编译debug的工程,直接运行你的可执行文件,可以用Dbgview来获得输出。

MFC工程加入控制台调试信息输出

  在MFC程序中,可以使用TRACE宏或者OutPutDebugString()函数输出调试信息,TRACE宏可以在调试时像Output窗口输出调试信息,OutPutDebugString()函数的输出则可以用DebugView捕获(DebugView也可以捕获TRACE宏的输出,其官网在 这里 ,具体使用请参考官网的说明),另外也可以通过AfxMessageBox()或者MessageBox()弹窗输出,但毕竟太多繁琐,每弹出一个窗口便要确认一次。引入日志库也是个好办法,同时也可以通过分析日志文件了解应用程序的运行状况,这是终极大杀器,我们还需要更轻量级的方法。

  控制台调试信息输出,即是在程序运行时,打开一个Console窗口,将自己编写的调试信息输出到Console中,便于了解当前程序的运行状况,帮助调试,仅需简单几行代码即可搞定,不需要动用日志库这样重量级的东东。

  本文以一个基于对话框的MFC程序为例,看看如何给应用程序加上控制台输出。

  1、实例工程名为Demo,在CDemoDlg.cpp中加入头文件 #include "conio.h" 

  2、在CDemoDlg::OnInitDialog() {...}函数中加入AllocConsole();

  3、在需要输出调试信息的地方,调用 cprintf() (在VS2005后应该用 _cprintf 代替,下文有说明)函数输出信息,用法同 printf() 函数类似;

  4、若需要关闭控制台输出,调用 FreeConsole();

  在MSDN(MSDN Library Visual Studio 2008 SP1)中,AllocConsole()函数的原型为:

?
1
BOOL  WINAPI AllocConsole( void );

  Return Value:

  If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.

  Remarks:

  A process can be associated with only one console, so the AllocConsole function fails if the calling process already has a console. A process can use the FreeConsole function to detach itself from its current console, then it can call AllocConsole to create a new console or AttachConsole to attach to another console.

  If the calling process creates a child process, the child inherits the new console.

  AllocConsole initializes standard input, standard output, and standard error handles for the new console. The standard input handle is a handle to the console's input buffer, and the standard output and standard error handles are handles to the console's screen buffer. To retrieve these handles, use the GetStdHandle function.

  This function is primarily used by graphical user interface (GUI) application to create a console window. GUI applications are initialized without a console. Console applications are initialized with a console, unless they are created as detached processes (by calling the CreateProcess function with the DETACHED_PROCESS flag).

  MSDN中对于cprintf()的解释,很简短,只有一句话

  "This POSIX function is deprecated beginning in Visual C++ 2005. Use the ISO C++ conformant _cprintf or security-enhanced _cprintf_s instead."

  从VS2005开始,用 _cprintf 或 _cprintf_s 函数代替 cprintf ,So then,再来看下 _cprintf 的解释,更加简短了:Formats and prints to the console.

按照我的理解,就是 _cprintf 是像控制台输出格式化信息,c 即是console的意思,而 printf 是标准输出,不一定是控制台窗口了。


http://blog.csdn.net/liuxizhen2009/article/details/8557888

http://www.cnblogs.com/zhaomzs/archive/2013/01/23/2873764.html


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值