Add the console to the MFC Program

Source: Internet
Author: User

MFCProgramTo output debugging information, we generally use trace or log files, which is not very convenient. The first one needs to be in the debugging status, the second one is also difficult to configure and is not intuitive. Using the console to display debugging information should be a better choice. The following describes how to use the console to output debugging information in the MFC program.

1. You can create a command line window and call the allocconsole () function during initialization of the main program. Call freeconsole () when exiting ()

 

  1   Bool cmfctestapp: initinstance ()
2 {
3 ...
4 # Ifdef _ debug
5 Allocconsole ();
6   # Endif
7 ...
8 Cwinapp: initinstance ();
9 }

In this way, the output function must use _ cprintf or writeconsole (getstdhandle (std_output_handle)...). Other types such as cout and printf cannot be output here. Of course, you can encapsulate a set of debugging functions by yourself.

 

2. modify the configuration information of the Project. Generally, the program type is configured through these two/subsystem: Windows. This can be seen in project properties/linker/system/subsystem, we can display the command line by modifying the program to the console type, but directly modifying this attribute will prompt a link error because different types of programs have different program portals. Here we use a simple method to add a sentence directly in stdafx. h.

 

 
  1 # Ifdef _ debug
2 # PragmaComment (linker, "/subsystem: console/entry: wwinmaincrtstartup ")// I use vs2005
3 # Endif

We have redefined the system types and entry functions, which can be combined as needed. You can refer to the current property information of your project, project property/linker/command line, the following reference

 

# Pragma comment (linker, "/subsystem: Windows/entry: winmaincrtstartup ")
# Pragma comment (linker, "/subsystem: Windows/entry: maincrtstartup ")

# Pragma comment (linker, "/subsystem: console/entry: maincrtstartup ")
# Pragma comment (linker, "/subsystem: console/entry: winmaincrtstartup ")

 

PS How to change the display color of the Win32 console Program

 Code 
   Handle hcon  =  Getstdhandle (std_output_handle );
/*
Std_input_handle standard input handle std_output_handle standard output handle std_error_handle standard error handle */
Setconsoletextattribute (hcon, forecolor | Backgroundcolor );
/* The text and background colors can be foreground_blue, foreground_green, foreground_red, foreground_intensity, background_blue, background_green, background_red, and background_intensity. */



 

End: happy National Day ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.