Guest User

Untitled

a guest
Sep 5th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. if(AllocConsole()) {
  2. freopen("CONOUT$", "wt", stdout);
  3. SetConsoleTitle("Debug Console");
  4. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
  5. }
  6.  
  7. class outbuf : public std::streambuf {
  8. public:
  9. outbuf() {
  10. setp(0, 0);
  11. }
  12.  
  13. virtual int_type overflow(int_type c = traits_type::eof()) {
  14. return fputc(c, stdout) == EOF ? traits_type::eof() : c;
  15. }
  16. };
  17.  
  18. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {
  19. // create the console
  20. if(AllocConsole()) {
  21. freopen("CONOUT$", "w", stdout);
  22. SetConsoleTitle("Debug Console");
  23. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
  24. }
  25.  
  26. // set std::cout to use my custom streambuf
  27. outbuf ob;
  28. std::streambuf *sb = std::cout.rdbuf(&ob);
  29.  
  30. // do some work here
  31.  
  32. // make sure to restore the original so we don't get a crash on close!
  33. std::cout.rdbuf(sb);
  34. return 0;
  35. }
  36.  
  37. void BindStdHandlesToConsole()
  38. {
  39.  
  40. // Get STDOUT handle
  41. HANDLE ConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  42. int SystemOutput = _open_osfhandle(intptr_t(ConsoleOutput), _O_TEXT);
  43. FILE *COutputHandle = _fdopen(SystemOutput, "w");
  44.  
  45. // Get STDERR handle
  46. HANDLE ConsoleError = GetStdHandle(STD_ERROR_HANDLE);
  47. int SystemError = _open_osfhandle(intptr_t(ConsoleError), _O_TEXT);
  48. FILE *CErrorHandle = _fdopen(SystemError, "w");
  49.  
  50. // Get STDIN handle
  51. HANDLE ConsoleInput = GetStdHandle(STD_INPUT_HANDLE);
  52. int SystemInput = _open_osfhandle(intptr_t(ConsoleInput), _O_TEXT);
  53. FILE *CInputHandle = _fdopen(SystemInput, "r");
  54.  
  55. // Redirect the CRT standard input, output, and error handles to the console
  56. freopen_s(&CInputHandle, "CONIN$", "r", stdin);
  57. freopen_s(&COutputHandle, "CONOUT$", "w", stdout);
  58. freopen_s(&CErrorHandle, "CONOUT$", "w", stderr);
  59.  
  60. //Clear the error state for each of the C++ standard stream objects. We need to do this, as
  61. //attempts to access the standard streams before they refer to a valid target will cause the
  62. //iostream objects to enter an error state. In versions of Visual Studio after 2005, this seems
  63. //to always occur during startup regardless of whether anything has been read from or written to
  64. //the console or not.
  65. std::wcout.clear();
  66. std::cout.clear();
  67. std::wcerr.clear();
  68. std::cerr.clear();
  69. std::wcin.clear();
  70. std::cin.clear();
  71. }
  72.  
  73. void BindStdHandlesToConsole()
  74. {
  75. //Redirect unbuffered STDIN to the console
  76. HANDLE stdInHandle = GetStdHandle(STD_INPUT_HANDLE);
  77. if(stdInHandle != INVALID_HANDLE_VALUE)
  78. {
  79. int fileDescriptor = _open_osfhandle((intptr_t)stdInHandle, _O_TEXT);
  80. if(fileDescriptor != -1)
  81. {
  82. FILE* file = _fdopen(fileDescriptor, "r");
  83. if(file != NULL)
  84. {
  85. *stdin = *file;
  86. setvbuf(stdin, NULL, _IONBF, 0);
  87. }
  88. }
  89. }
  90.  
  91. //Redirect unbuffered STDOUT to the console
  92. HANDLE stdOutHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  93. if(stdOutHandle != INVALID_HANDLE_VALUE)
  94. {
  95. int fileDescriptor = _open_osfhandle((intptr_t)stdOutHandle, _O_TEXT);
  96. if(fileDescriptor != -1)
  97. {
  98. FILE* file = _fdopen(fileDescriptor, "w");
  99. if(file != NULL)
  100. {
  101. *stdout = *file;
  102. setvbuf(stdout, NULL, _IONBF, 0);
  103. }
  104. }
  105. }
  106.  
  107. //Redirect unbuffered STDERR to the console
  108. HANDLE stdErrHandle = GetStdHandle(STD_ERROR_HANDLE);
  109. if(stdErrHandle != INVALID_HANDLE_VALUE)
  110. {
  111. int fileDescriptor = _open_osfhandle((intptr_t)stdErrHandle, _O_TEXT);
  112. if(fileDescriptor != -1)
  113. {
  114. FILE* file = _fdopen(fileDescriptor, "w");
  115. if(file != NULL)
  116. {
  117. *stderr = *file;
  118. setvbuf(stderr, NULL, _IONBF, 0);
  119. }
  120. }
  121. }
  122.  
  123. //Clear the error state for each of the C++ standard stream objects. We need to do this, as
  124. //attempts to access the standard streams before they refer to a valid target will cause the
  125. //iostream objects to enter an error state. In versions of Visual Studio after 2005, this seems
  126. //to always occur during startup regardless of whether anything has been read from or written to
  127. //the console or not.
  128. std::wcout.clear();
  129. std::cout.clear();
  130. std::wcerr.clear();
  131. std::cerr.clear();
  132. std::wcin.clear();
  133. std::cin.clear();
  134. }
  135.  
  136. class outbuf : public std::streambuf {
  137. public:
  138. outbuf() {
  139. setp(0, 0);
  140. }
  141.  
  142. virtual int_type overflow(int_type c = traits_type::eof()) {
  143. return fputc(c, stdout) == EOF ? traits_type::eof() : c;
  144. }
  145. };
  146.  
  147. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {
  148. // create the console
  149. if(AllocConsole()) {
  150. freopen("CONOUT$", "w", stdout);
  151. SetConsoleTitle("Debug Console");
  152. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
  153. }
  154.  
  155. // set std::cout to use my custom streambuf
  156. outbuf ob;
  157. std::streambuf *sb = std::cout.rdbuf(&ob);
  158.  
  159. // do some work here
  160.  
  161. // make sure to restore the original so we don't get a crash on close!
  162. std::cout.rdbuf(sb);
  163. return 0;
  164. }
  165.  
  166. if(AllocConsole())
  167. {
  168. freopen("CONOUT$", "wt", stdout);
  169. freopen("CONIN$", "rt", stdin);
  170. SetConsoleTitle(L"Debug Console");
  171. std::ios::sync_with_stdio(1);
  172. }
Advertisement
Add Comment
Please, Sign In to add comment