Add the MFC project to the console for debugging information output
In the MFC program, you can use the trace macro or outputdebugstring () function to output debugging information. The trace macro can output debugging information like the output window during debugging, and outputdebugstring () function output can be captured using debugview (debugview can also capture trace macro output. For details about how to use this function, refer to the instructions on the official website). You can also use afxmessagebox () or MessageBox () pop-up window output, but after all too much tedious, each pop-up window needs to be confirmed once. Introducing a logstore is also a good solution. You can also analyze the log file to understand the running status of the application. This is the ultimate killer and we need a more lightweight approach.
The console debugging information is output, that is, when the program is running, open a console window and output the debugging information compiled by yourself to the console to help you understand the running status of the current program and debug it, you only need a few lines of code to do it. You do not need to use a logstore.
This article takes a dialog box-based MFC program as an example to see how to add console output to the application.
1. The instance project name is demo. Add the header file to cdemodlg. cpp.# Include "conio. H";
2. In the cdemodlg: oninitdialog () {...} function, addAllocconsole ();
3. CallCprintf ()(_ Cprintf should be used in place after vs2005, as described below) function output information, usage is similar to printf () function;
4. To Disable Console output, callFreeconsole ();
In msdn (msdn library Visual Studio 2008 SP1), The allocconsole () function is prototype:
?
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, callGetlasterror.
Remarks:
A process can be associated with only one console, soAllocconsoleFunction fails if the calling process already has a console. A process can useFreeconsoleFunction to detach itself from its current console, then it can callAllocconsoleTo create a new console orAttachconsoleTo attach to another console.
If the calling process creates a child process, the child inherits the new console.
AllocconsoleInitializes 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, useGetstdhandleFunction.
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 callingCreateProcessFunction with the detached_process flag ).
The explanation of cprintf () in msdn is very short, with only one sentence
"This POSIX function is deprecated beginning in Visual C ++ 2005. Use the Iso c ++ conformant_ CprintfOr security-enhanced_ Cprintf_sInstead ."
Starting from vs2005, use the _ cprintf or _ cprintf_s function to replace cprintf and so then. Let's take a look at the explanation of _ cprintf, which is more brief: formats and prints to the console.
In my understanding, _ cprintf is formatted like the console output, C is the meaning of the console, and printf is the standard output, not necessarily the console window.