天天看點

MFC 利用控制台輸出調試資訊(轉) 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

繼續閱讀