Enable console output in a Windows program-2016.01.04

Source: Internet
Author: User
Tags win32 window

At some point, we may need to open the console window in the Win32 window application, print some messages, or act as another human-computer interface for the current program, or to help debug the program. To achieve this effect, you need to understand the function allocconsole and the Freopen function of C-runtime.
The AllocConsole function applies a console window for the current window program, and the following MSDN describes AllocConsole:

The allocconsole function allocates a new console for the calling process.

BOOL allocconsole (void);

The function call succeeds, returns a value other than 0, and returns 0 if the call fails.

After applying to the console for the current window program, we need to call C-runtime's Freopen function to relocate the standard output (stdout) to the new application's console.
The prototype of Freopen is as follows:

File *freopen (constcharconstchar *mode, file *stream);

where "conout$" is a special string that refers to the current console.

Example code:

if (AllocConsole ()) {                   "conout$""w+t", stdout );      " conout$ " " w+t " , stderr); }

Enable console output in a Windows program-2016.01.04

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.