Created
July 13, 2021 18:48
-
-
Save GuilhermeRossato/08ff598e6c97f01f7cd7f38ead2780a8 to your computer and use it in GitHub Desktop.
Creating a Console Windows for a Win32 program in windows subsystem so that you can printf again
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // adapted from c++ on stackoverflow: https://stackoverflow.com/a/57210516/5974331 | |
| // opens a new console windows for the current process | |
| // returns 1 on success | |
| int CreateConsole() { | |
| if (!AllocConsole()) { | |
| return 0; | |
| } | |
| FILE *fDummy; | |
| freopen_s(&fDummy, "CONIN$", "r", stdin); | |
| freopen_s(&fDummy, "CONOUT$", "w", stderr); | |
| freopen_s(&fDummy, "CONOUT$", "w", stdout); | |
| fflush(stdin); | |
| fflush(stderr); | |
| fflush(stdout); | |
| return 1; | |
| } | |
| // usage example: | |
| int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) | |
| { | |
| if (!CreateConsole()) { | |
| return 1; | |
| } | |
| printf("I will show up on the new console\n"); | |
| // rest of implementation of windows program | |
| // createwindows / GetMessage / etc | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment