I tried this code to output to the console:
#include <Windows.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
AllocConsole();
HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
int hCrt = _open_osfhandle((long) handle_out, _O_TEXT);
FILE* hf_out = _fdopen(hCrt, "w");
setvbuf(hf_out, NULL, _IONBF, 1);
*stdout = *hf_out;
HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
hCrt = _open_osfhandle((long) handle_in, _O_TEXT);
FILE* hf_in = _fdopen(hCrt, "r");
setvbuf(hf_in, NULL, _IONBF, 128);
*stdin = *hf_in;
printf("Hello!");
}
the console opens but nothing is outputted to it. What's wrong with that code?
I tried all these suggestions:
https://justcheckingonall.wordpress.com/2008/08/29/console-window-win32-app/
http://dslweb.nwnexus.com/~ast/dload/guicon.htm
How do I print to the debug output window in a Win32 app?
but I couldn't get any output to the console created with AllocConsole() on Windows 10 in WinMain.
Note: I actually didn't create any real Window.
Was something changed in Window 10 that prevents the above solutions from working or is there something that I might be missing (compiler flags or something)?
What do you think?
Best Answer
Based on the accepted answer from ProXicT link with a few modifications. The following code works for std::cout. The other methods won't work on 64bit with Visual Studio 2015:
EDIT:
The same stuff as above but with colors for completeness:
All that remains now is to make it into a complete logging class.