Skip to content

Instantly share code, notes, and snippets.

@cor3ntin
Created April 27, 2017 08:50
Show Gist options
  • Save cor3ntin/282f9ddadb0f1609e62aae7f37bfb859 to your computer and use it in GitHub Desktop.
Save cor3ntin/282f9ddadb0f1609e62aae7f37bfb859 to your computer and use it in GitHub Desktop.
#if defined(AK_OS_WINDOWS)
//Attempt to attach to parent
if(!AttachConsole(ATTACH_PARENT_PROCESS)) {
if(!force)
return false;
//Try to create a new window, if allowed
FreeConsole();
if(!AllocConsole())
return false;
SetConsoleTitle(L"Debug Console");
}
//The std stream are reopen using the console handle
freopen("CONOUT$", "w", stdout);
freopen("CONIN$", "r", stdin);
freopen("CONERR$", "w", stderr);
//make sure the console handles are returned by GetStdHandle
HANDLE newOut = CreateFileW(L"CONOUT$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
SetStdHandle(STD_OUTPUT_HANDLE, newOut);
HANDLE newErr = CreateFileW(L"CONERR$", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
SetStdHandle(STD_ERROR_HANDLE, newErr);
//clear the std stream state to make sure they are not in an error state
std::wcout.clear();
std::cout.clear();
std::wcerr.clear();
std::cerr.clear();
std::wcin.clear();
std::cin.clear();
//pprint should recalculate the capabilities of the newly attached console
gWInConsoleInitialized = false;
return true;
#endif
return isTerminal();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment