Skip to content

Instantly share code, notes, and snippets.

@GuilhermeRossato
Created July 13, 2021 18:48
Show Gist options
  • Select an option

  • Save GuilhermeRossato/08ff598e6c97f01f7cd7f38ead2780a8 to your computer and use it in GitHub Desktop.

Select an option

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
// 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