Skip to content

Instantly share code, notes, and snippets.

@thewh1teagle
Created August 27, 2024 18:25
Show Gist options
  • Select an option

  • Save thewh1teagle/0c15efbf33b6dc1c34670016eefb2d03 to your computer and use it in GitHub Desktop.

Select an option

Save thewh1teagle/0c15efbf33b6dc1c34670016eefb2d03 to your computer and use it in GitHub Desktop.
C++ gui mode attach to console and redirect standard output
#include <iostream>
#include <cstdio>
#include <windows.h>
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// Attach to the parent console
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
// Redirect stdout and stderr to the console
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
}
std::cout << "print to stdout" << std::endl;
std::cerr << "print to stderr" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment