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