DebugConsole.cs 821 B

1234567891011121314151617181920212223242526272829
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. namespace XUnity.AutoTranslator.Plugin.Core.Debugging
  7. {
  8. public static class DebugConsole
  9. {
  10. private static IntPtr _consoleOut;
  11. public static void Enable()
  12. {
  13. var oldConsoleOut = Kernel32.GetStdHandle( -11 );
  14. if( !Kernel32.AllocConsole() ) return;
  15. _consoleOut = Kernel32.CreateFile( "CONOUT$", 0x40000000, 2, IntPtr.Zero, 3, 0, IntPtr.Zero );
  16. if( !Kernel32.SetStdHandle( -11, _consoleOut ) ) return;
  17. Stream stream = Console.OpenStandardOutput();
  18. StreamWriter writer = new StreamWriter( stream, Encoding.Default );
  19. writer.AutoFlush = true;
  20. Console.SetOut( writer );
  21. Console.SetError( writer );
  22. }
  23. }
  24. }