You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
5.5 KiB

6 years ago
  1. using System;
  2. using System.ComponentModel;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using Microsoft.Win32.SafeHandles;
  6. namespace IPA.Logging
  7. {
  8. // https://stackoverflow.com/a/48864902/3117125
  9. internal static class WinConsole
  10. {
  11. internal static TextWriter ConOut;
  12. internal static TextReader ConIn;
  13. private static SafeFileHandle outHandle;
  14. private static SafeFileHandle inHandle;
  15. public static bool UseVTEscapes { get; private set; } = true;
  16. internal static IntPtr OutHandle => outHandle.DangerousGetHandle();
  17. internal static IntPtr InHandle => inHandle.DangerousGetHandle();
  18. internal static bool IsInitialized;
  19. public static void Initialize(bool alwaysCreateNewConsole = false)
  20. {
  21. bool consoleAttached = true;
  22. if (alwaysCreateNewConsole
  23. || (AttachConsole(AttachParent)
  24. && Marshal.GetLastWin32Error() != ErrorAccessDenied))
  25. {
  26. consoleAttached = AllocConsole();
  27. }
  28. if (consoleAttached)
  29. {
  30. InitializeStreams();
  31. IsInitialized = true;
  32. }
  33. }
  34. private static void InitializeStreams()
  35. {
  36. InitializeOutStream();
  37. InitializeInStream();
  38. }
  39. private static void InitializeOutStream()
  40. {
  41. var fs = CreateFileStream("CONOUT$", GenericWrite, FileShareWrite, FileAccess.Write, out outHandle);
  42. if (fs != null)
  43. {
  44. var writer = new StreamWriter(fs) { AutoFlush = true };
  45. ConOut = writer;
  46. Console.SetOut(writer);
  47. Console.SetError(writer);
  48. var handle = GetStdHandle(-11); // get stdout handle (should be CONOUT$ at this point)
  49. if (GetConsoleMode(handle, out var mode))
  50. {
  51. mode |= EnableVTProcessing;
  52. if (!SetConsoleMode(handle, mode))
  53. {
  54. UseVTEscapes = false;
  55. Console.Error.WriteLine("Could not enable VT100 escape code processing (maybe you're running an old Windows?): " +
  56. new Win32Exception(Marshal.GetLastWin32Error()).Message);
  57. }
  58. }
  59. else
  60. {
  61. UseVTEscapes = false;
  62. Console.Error.WriteLine("Could not enable VT100 escape code processing (maybe you're running an old Windows?): " +
  63. new Win32Exception(Marshal.GetLastWin32Error()).Message);
  64. }
  65. }
  66. }
  67. private static void InitializeInStream()
  68. {
  69. var fs = CreateFileStream("CONIN$", GenericRead, FileShareRead, FileAccess.Read, out inHandle);
  70. if (fs != null)
  71. {
  72. Console.SetIn(ConIn = new StreamReader(fs));
  73. }
  74. }
  75. private static FileStream CreateFileStream(string name, uint win32DesiredAccess, uint win32ShareMode,
  76. FileAccess dotNetFileAccess, out SafeFileHandle handle)
  77. {
  78. var file = new SafeFileHandle(CreateFile(name, win32DesiredAccess, win32ShareMode, IntPtr.Zero, OpenExisting, FileAttributeNormal, IntPtr.Zero), true);
  79. if (!file.IsInvalid)
  80. {
  81. handle = file;
  82. var fs = new FileStream(file, dotNetFileAccess);
  83. return fs;
  84. }
  85. handle = null;
  86. return null;
  87. }
  88. #region Win API Functions and Constants
  89. [DllImport("kernel32.dll")]
  90. [DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
  91. private static extern bool AllocConsole();
  92. [DllImport("kernel32.dll")]
  93. [DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
  94. private static extern bool AttachConsole(int dwProcessId);
  95. [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
  96. [DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
  97. private static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode,
  98. IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
  99. [DllImport("kernel32.dll", SetLastError = true)]
  100. [DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
  101. private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
  102. [DllImport("kernel32.dll", SetLastError = true)]
  103. [DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
  104. private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
  105. [DllImport("kernel32.dll")]
  106. [DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
  107. private static extern IntPtr GetStdHandle(int nStdHandle);
  108. private const uint EnableVTProcessing = 0x0004;
  109. private const uint GenericWrite = 0x40000000;
  110. private const uint GenericRead = 0x80000000;
  111. private const uint FileShareRead = 0x00000001;
  112. private const uint FileShareWrite = 0x00000002;
  113. private const uint OpenExisting = 0x00000003;
  114. private const uint FileAttributeNormal = 0x80;
  115. private const uint ErrorAccessDenied = 5;
  116. private const int AttachParent = -1;
  117. #endregion
  118. }
  119. }