When you create a basic main()-based application, you get the console window implicitly; but a typical WinMain()-based application does not open a console window – you have to allocate the console and to connect the I/O streams yourself.
Yet, this is not a thing you find described in manuals. Fortunately, it’s simple to do, and the code presented here can be used as-is in all your projects.
#include <stdio.h> #include <io.h> #include <fcntl.h> #include <windows.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; // use the console just like a normal one - printf(), getchar(), ... }
Thanks dude , it woz really helpfull
You’re welcome, glad to be helpful.
Um, crashes. Not working. VC6.0.
Debug and report, or present your code.
Thanks so much, this was exactly what I was looking for!
Glad I could help! Happy coding! 😀
This is really cool. Although it seems like the focus switches when you switch windows.
Oh I know this is a bit of a noob question. Is there anything I need to get rid of or delete from that code that may cause memory leaks?
Excellent! Saved me a couple of hours of hopeless searching 🙂
Excelente post, compiló sin problemas sobre mi proyecto Win32, gracias!
Thanks! 🙂
Thanks! It’s really helpful!
Thanks, man! This saved my life!
Thanks! It’s very helpful!
Thanks loads, have used this repeatedly in my projects. Very helpful. Always credit you for it, too!
Thanks, it is great.
But when I run the program in a console, I get an error. Can I avoid that ?
cheers
Great! That really helped me. I was searching for a way to make a Qt Application close the console on exit.
Hello, I wonder how can I close that console after a getchar() is pressed?
Let me know.
Thanks
thanks, exacly what i was looking for
Use FreeConsole() to detach the console.
Thx, was helpful
Thank you – this is an invaluable tool!
Good stuff! Thanks!
Thanks. Great for some debug output.
but how create more than 1 console on same application?
It shows a declaration syntax error in the first line of your code pls reply fast.
you might want to include windows.h header 🙂 i’ll update the code.
Doesn’t do anything besides opening the console – no IO operations work.
I want to show some thanks to you just for rescuing me from this crisis. Just after checking throughout the world-wide-web and seeing basics that were not productive, I was thinking my entire life was well over. Living without the presence of answers to the problems you have resolved by way of your good site is a critical case, as well as those that might have in a negative way damaged my career if I hadn’t discovered your web page. Your own competence and kindness in playing with the whole thing was tremendous. I am not sure what I would’ve done if I had not come upon such a solution like this. It’s possible to at this moment look forward to my future. Thanks a lot very much for this high quality and amazing guide. I won’t be reluctant to suggest your web page to anyone who would like guide on this situation.
Great post. I am facing a couple of these problems.
But how to remove the window?
Great post! Though, I see one problem: no code to free resources you aquired. File handles and console must be explicitly freed. Still, I’m googling how to do this properly)
Thank you!
Thank You!
Hi, thanks for this. Would it be possible to cite some of the evidence based research you quoted as this would be really helpful and powerful 3 week diet.
It doesn’t work, I’m using codeblocks
I pretty sure the code was right but…
Thanks for this!
In a 64-bit environment, I have to use
int hCrt = _open_osfhandle((unsigned long long)handle_out, _O_TEXT);
to avoid pointer truncation warnings related to assigning to a 32-bit pointer, as per
https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-1-c4311?f1url=%3FappId%3DDev16IDEF1%26l%3DEN-US%26k%3Dk(C4311)%26rd%3Dtrue&view=msvc-170