Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(AllocConsole()) {
- freopen("CONOUT$", "wt", stdout);
- SetConsoleTitle("Debug Console");
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
- }
- class outbuf : public std::streambuf {
- public:
- outbuf() {
- setp(0, 0);
- }
- virtual int_type overflow(int_type c = traits_type::eof()) {
- return fputc(c, stdout) == EOF ? traits_type::eof() : c;
- }
- };
- int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {
- // create the console
- if(AllocConsole()) {
- freopen("CONOUT$", "w", stdout);
- SetConsoleTitle("Debug Console");
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
- }
- // set std::cout to use my custom streambuf
- outbuf ob;
- std::streambuf *sb = std::cout.rdbuf(&ob);
- // do some work here
- // make sure to restore the original so we don't get a crash on close!
- std::cout.rdbuf(sb);
- return 0;
- }
- void BindStdHandlesToConsole()
- {
- // Get STDOUT handle
- HANDLE ConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
- int SystemOutput = _open_osfhandle(intptr_t(ConsoleOutput), _O_TEXT);
- FILE *COutputHandle = _fdopen(SystemOutput, "w");
- // Get STDERR handle
- HANDLE ConsoleError = GetStdHandle(STD_ERROR_HANDLE);
- int SystemError = _open_osfhandle(intptr_t(ConsoleError), _O_TEXT);
- FILE *CErrorHandle = _fdopen(SystemError, "w");
- // Get STDIN handle
- HANDLE ConsoleInput = GetStdHandle(STD_INPUT_HANDLE);
- int SystemInput = _open_osfhandle(intptr_t(ConsoleInput), _O_TEXT);
- FILE *CInputHandle = _fdopen(SystemInput, "r");
- // Redirect the CRT standard input, output, and error handles to the console
- freopen_s(&CInputHandle, "CONIN$", "r", stdin);
- freopen_s(&COutputHandle, "CONOUT$", "w", stdout);
- freopen_s(&CErrorHandle, "CONOUT$", "w", stderr);
- //Clear the error state for each of the C++ standard stream objects. We need to do this, as
- //attempts to access the standard streams before they refer to a valid target will cause the
- //iostream objects to enter an error state. In versions of Visual Studio after 2005, this seems
- //to always occur during startup regardless of whether anything has been read from or written to
- //the console or not.
- std::wcout.clear();
- std::cout.clear();
- std::wcerr.clear();
- std::cerr.clear();
- std::wcin.clear();
- std::cin.clear();
- }
- void BindStdHandlesToConsole()
- {
- //Redirect unbuffered STDIN to the console
- HANDLE stdInHandle = GetStdHandle(STD_INPUT_HANDLE);
- if(stdInHandle != INVALID_HANDLE_VALUE)
- {
- int fileDescriptor = _open_osfhandle((intptr_t)stdInHandle, _O_TEXT);
- if(fileDescriptor != -1)
- {
- FILE* file = _fdopen(fileDescriptor, "r");
- if(file != NULL)
- {
- *stdin = *file;
- setvbuf(stdin, NULL, _IONBF, 0);
- }
- }
- }
- //Redirect unbuffered STDOUT to the console
- HANDLE stdOutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
- if(stdOutHandle != INVALID_HANDLE_VALUE)
- {
- int fileDescriptor = _open_osfhandle((intptr_t)stdOutHandle, _O_TEXT);
- if(fileDescriptor != -1)
- {
- FILE* file = _fdopen(fileDescriptor, "w");
- if(file != NULL)
- {
- *stdout = *file;
- setvbuf(stdout, NULL, _IONBF, 0);
- }
- }
- }
- //Redirect unbuffered STDERR to the console
- HANDLE stdErrHandle = GetStdHandle(STD_ERROR_HANDLE);
- if(stdErrHandle != INVALID_HANDLE_VALUE)
- {
- int fileDescriptor = _open_osfhandle((intptr_t)stdErrHandle, _O_TEXT);
- if(fileDescriptor != -1)
- {
- FILE* file = _fdopen(fileDescriptor, "w");
- if(file != NULL)
- {
- *stderr = *file;
- setvbuf(stderr, NULL, _IONBF, 0);
- }
- }
- }
- //Clear the error state for each of the C++ standard stream objects. We need to do this, as
- //attempts to access the standard streams before they refer to a valid target will cause the
- //iostream objects to enter an error state. In versions of Visual Studio after 2005, this seems
- //to always occur during startup regardless of whether anything has been read from or written to
- //the console or not.
- std::wcout.clear();
- std::cout.clear();
- std::wcerr.clear();
- std::cerr.clear();
- std::wcin.clear();
- std::cin.clear();
- }
- class outbuf : public std::streambuf {
- public:
- outbuf() {
- setp(0, 0);
- }
- virtual int_type overflow(int_type c = traits_type::eof()) {
- return fputc(c, stdout) == EOF ? traits_type::eof() : c;
- }
- };
- int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {
- // create the console
- if(AllocConsole()) {
- freopen("CONOUT$", "w", stdout);
- SetConsoleTitle("Debug Console");
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
- }
- // set std::cout to use my custom streambuf
- outbuf ob;
- std::streambuf *sb = std::cout.rdbuf(&ob);
- // do some work here
- // make sure to restore the original so we don't get a crash on close!
- std::cout.rdbuf(sb);
- return 0;
- }
- if(AllocConsole())
- {
- freopen("CONOUT$", "wt", stdout);
- freopen("CONIN$", "rt", stdin);
- SetConsoleTitle(L"Debug Console");
- std::ios::sync_with_stdio(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment