Open the console in the mfc Program

Source: Internet
Author: User

Author blog: http://87year.info

The majority of c/c ++ learning is started from the console, and it is very convenient to directly output log information in the console.

Today, I have Baidu a variety of methods for outputting logs to the console in the mfc program. Finally, I have compared a convenient and simple method that can be turned off at any time.

/* Required header files
# Include <stdio. h>
# Include <io. h>
# Include <fcntl. h>
*/
Int hCrt = 0;
// Create a console <! -- More -->
Void OnBnClickedButton1 ()
{
FreeConsole ();
If (AllocConsole ())
{
HCrt = _ open_osfhandle (long) GetStdHandle (STD_OUTPUT_HANDLE), _ O_TEXT );
* Stdout = * (: _ fdopen (hCrt, "w "));
: Setvbuf (stdout, NULL, _ IONBF, 0 );
* Stderr = * (: _ fdopen (hCrt, "w "));
: Setvbuf (stderr, NULL, _ IONBF, 0 );
}
}
// Release the Console
Void OnBnClickedButton2 ()
{
FreeConsole ();
}
Output Information
Void OnEnUpdateEdit1 ()
{
Printf ("hello MFConsole ");
}

Before exiting, FreeConsole () is required. Otherwise, an error will occur.

Related Article

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.