Created
August 27, 2024 18:25
-
-
Save thewh1teagle/0c15efbf33b6dc1c34670016eefb2d03 to your computer and use it in GitHub Desktop.
C++ gui mode attach to console and redirect standard output
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
| #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