Adding consoles to MFC programs

Source: Internet
Author: User

Adding consoles to MFC programs

MFC program, if you want to output debugging information, we are generally trace or use a log file, is not very convenient, the first one needs us in the debug state, the second is also to configure the trouble and not intuitive. Using the console to display debug information should be a better choice. Here are a few of the MFC programs that use the console to output debugging information.

1. We can create a command-line window ourselves, calling the AllocConsole () function when the main program is initialized. The corresponding exit call Freeconsole ()

1 BOOL cmfctestapp::initinstance ()2 {3    ...4 #ifdef _DEBUG5 allocconsole ();6  #endif7    ...8 cwinapp::initinstance ();9 }

In this way, the output function needs to use _cprintf or Writeconsole (GetStdHandle (std_output_handle) ...), and the others, such as cout,printf, cannot be exported here. Of course, you can encapsulate a set of debugging functions yourself.

2. Modify the project configuration information, the General control program type is through the two/subsystem:windows to configure, this can be seen in the project properties/linker/system/subsystem here, We can make the program display the command line by modifying the program as the console type, but modifying this property directly will prompt for link errors because different types of programs have different entry points. Here is a simple way to add a sentence directly to the stdafx.h

1 #ifdef _DEBUG 2 #pragma COMMENT (linker, "/subsystem:console/entry:wwinmaincrtstartup") //I use VS2005 3 #endif

We have redefined the system type and the entry function, which can be arbitrarily combined according to your own needs. You can refer to the current property information of your project, project properties/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")

How PS changes the display color of Win32 Console program

Code

HANDLE Hcon = GetStdHandle (std_output_handle); /* */setconsoletextattribute (hcon,forecolor| backgroundcolor); /* its text and background colors can be foreground_blue, Foreground_green, foreground_red, foreground_intensity, Background_blue, Background_green, background_red, and background_intensity* *

Adding consoles to MFC programs

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.