MFCDLL programming When using a separate console window to display user-defined debugging information

xiaoxiao2021-03-06  56

I package this feature into two files, download this text to the local, saved to the correct file name. Implementing the source code without changing the source code, the debug version displays the debug information window, and does not realize the window in the Release version and does not increase the contractual burden. ############### DBWindow.h ################# / ************* *********************************************************** ***** creation date: 2004/08/30 file name: DBWindow.h author: Liu Lei (vietor) version: 2.0 E-mail: liuleilover@163.com create Objective: to use VC MFC / DLL programming usually requires Debug version The test information is output through the console, while the code to which the output debug information needs to be annotated when compiling into the Release version, and the courses are often troublesome. The role of this program is that the debug version can be debugged in a separate console window when the MFC / DLL software development is performed, and does not have to manually comment on the Release version, which is automatically carry out. Note: Only one console window is present within a process, for multiple DLLs that may simultaneously debug, use color identification. Copyright Notice: You can copy and use the copy of this program at will, but please ensure that all files are complete and not modified, if you have a modified opinion, please contact the author. *********************************************************** **************************** / #1fndef _dbwindow_ # define _dbwindow_ #include // Text Color #define WDS_T_RED Foreground_red # define WDS_T_Green Foreground_green WDS_T_Green Foreground_green WDS_T_Green Foreground_Green WDS_T_Green Foreground_green #define WDS_T_BLUE FOREGROUND_BLUE // text background color console output #define WDS_BG_RED BACKGROUND_RED # define WDS_BG_GREEN BACKGROUND_GREEN # define WDS_BG_BLUE BACKGROUND_BLUE #ifdef _DEBUG // set the output console window title BOOL DBWindowTile (LPCTSTR tile); // formatted text output Bool DBWindowWrite (LPCTSTSTR FMT, ...); // With color formatted text Output Bool DBWindowWrite (Word Attrs, LPCTSTR fmt, ...); #else #define dbwindowtile # define dbwindowwrite #ENDIF #ENDIF ##### ### dbWindow.cpp ############### / ********************* ********************************************** Creation Date : 2004/08/30 file name: DBWindow.cpp author: Liu Lei (vietor) version: 2.0 E-mail: liuleilover@163.com created to: Debug version usually require test information via the console output when VC MFC / DLL programming, When compiled into a Release version, you need to comment on the code of these output debug information, and these code is more troublesome.

The role of this program is that the debug version can be debugged in a separate console window when the MFC / DLL software development is performed, and does not have to manually comment on the Release version, which is automatically carry out. Note: Only one console window is present within a process, for multiple DLLs that may simultaneously debug, use color identification. Copyright Notice: You can copy and use the copy of this program at will, but please ensure that all files are complete and not modified, if you have a modified opinion, please contact the author.

*********************************************************** ***************************** / #include "stdafx.h" #include "dbwindow.h" #ifdef _debug #include #include #include #define CONSOLE_TILE _T ( "DBWindow Ver 2.0 by LiuLei") class ConsoleWindow {public: ConsoleWindow (); virtual ~ ConsoleWindow (); BOOL SetTile (LPCTSTR lpTile); BOOL WriteString (LPCTSTR lpString); BOOL WriteString (WORD Attrs, LPCTSTR lpString); private: HANDLE m_hConsole; BOOL m_bCreate; BOOL m_bAttrs; WORD m_OldColorAttrs;}; ConsoleWindow :: ConsoleWindow () {m_hConsole = NULL; m_bCreate = FALSE; if (AllocConsole ()) {m_hConsole = GetStdHandle (STD_OUTPUT_HANDLE); SetConsoleTitle (CONSOLE_TILE); SetConsoleMode (m_hConsole, ENABLE_PROCESSED_OUTPUT); m_bCreate = TRUE;} else {m_hConsole = GetStdHandle (STD_OUTPUT_HANDLE); if (m_hConsole == INVALID_HANDLE_VALUE) m_hConsole = NULL;} if (m_hConsole) {CONSOLE_SCREEN_BUFFER_INFO csbiInfo ; IF (getConsolescreenBufferinfo (M_HCONSOLE, & CSBIINFO) {m_battrs = true; m_OldColorAttrs = csbiInfo.wAttributes;} else {m_bAttrs = FALSE; m_OldColorAttrs = 0;}}} ConsoleWindow :: ~ ConsoleWindow () {if (m_bCreate) FreeConsole ();} BOOL ConsoleWindow :: SetTile (LPCTSTR lpTile) {return SetConsoleTitle ( lpTile);} BOOL ConsoleWindow :: WriteString (LPCTSTR lpString) {BOOL ret = FALSE; if (m_hConsole) {ret = WriteConsole (m_hConsole, lpString, _tcslen (lpString), NULL, NULL);} return ret;} BOOL ConsoleWindow: : WriteString (WORD Attrs, LPCTSTR lpString) {BOOL ret = FALSE; if (m_hConsole) {if (m_bAttrs) SetConsoleTextAttribute (m_hConsole, Attrs); ret = WriteConsole (m_hConsole, lpString, _tcslen (lpString), NULL, NULL);

if (m_bAttrs) SetConsoleTextAttribute (m_hConsole, m_OldColorAttrs);} return ret;} ConsoleWindow ConWindow; #define MAX_BUF_LEN 4096 BOOL DBWindowTile (LPCTSTR tile) {return ConWindow.SetTile (tile);} BOOL DBWindowWrite (LPCTSTR fmt, ...) { TCHAR message [MAX_BUF_LEN]; va_list cur_arg; va_start (cur_arg, fmt); _vsntprintf (message, MAX_BUF_LEN, fmt, cur_arg); va_end (cur_arg); return ConWindow.WriteString (message);} BOOL DBWindowWrite (WORD Attrs, LPCTSTR fmt, ...) {TCHAR message [MAX_BUF_LEN]; va_list cur_arg; va_start (cur_arg, fmt); _vsntprintf (message, MAX_BUF_LEN, fmt, cur_arg); va_end (cur_arg); return ConWindow.WriteString (Attrs, message);} #endif Author Blog:

http://blog.9cbs.net/liuleilover/

转载请注明原文地址:https://www.9cbs.com/read-118296.html

New Post(0)