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.

154 lines
5.6 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
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 = true)
  20. {
  21. bool consoleAttached = true;
  22. if (alwaysCreateNewConsole
  23. || (AttachConsole(AttachParent) == 0
  24. && Marshal.GetLastWin32Error() != ErrorAccessDenied))
  25. {
  26. consoleAttached = AllocConsole() != 0;
  27. }
  28. if (consoleAttached)
  29. {
  30. InitializeStreams();
  31. IsInitialized = true;
  32. }
  33. }
  34. public 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(CreateFileW(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. EntryPoint = "AllocConsole",
  91. SetLastError = true,
  92. CharSet = CharSet.Auto,
  93. CallingConvention = CallingConvention.StdCall)]
  94. private static extern int AllocConsole();
  95. [DllImport("kernel32.dll",
  96. EntryPoint = "AttachConsole",
  97. SetLastError = true,
  98. CharSet = CharSet.Auto,
  99. CallingConvention = CallingConvention.StdCall)]
  100. private static extern uint AttachConsole(uint dwProcessId);
  101. [DllImport("kernel32.dll",
  102. EntryPoint = "CreateFileW",
  103. SetLastError = true,
  104. CharSet = CharSet.Unicode,
  105. CallingConvention = CallingConvention.StdCall)]
  106. private static extern IntPtr CreateFileW(
  107. string lpFileName,
  108. uint dwDesiredAccess,
  109. uint dwShareMode,
  110. IntPtr lpSecurityAttributes,
  111. uint dwCreationDisposition,
  112. uint dwFlagsAndAttributes,
  113. IntPtr hTemplateFile
  114. );
  115. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  116. private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
  117. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  118. private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
  119. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  120. private static extern IntPtr GetStdHandle(int nStdHandle);
  121. private const uint EnableVTProcessing = 0x0004;
  122. private const uint GenericWrite = 0x40000000;
  123. private const uint GenericRead = 0x80000000;
  124. private const uint FileShareRead = 0x00000001;
  125. private const uint FileShareWrite = 0x00000002;
  126. private const uint OpenExisting = 0x00000003;
  127. private const uint FileAttributeNormal = 0x80;
  128. private const uint ErrorAccessDenied = 5;
  129. private const uint AttachParent = 0xFFFFFFFF;
  130. #endregion
  131. }
  132. }