ConsoleWindow.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // --------------------------------------------------
  2. // UnityInjector - ConsoleWindow.cs
  3. // Copyright (c) Usagirei 2015 - 2015
  4. // --------------------------------------------------
  5. using System;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. namespace UnityInjector.ConsoleUtil
  10. {
  11. internal class ConsoleWindow
  12. {
  13. public static bool IsAttatched { get; private set; }
  14. private static IntPtr _cOut;
  15. private static IntPtr _oOut;
  16. public static void Attach()
  17. {
  18. if (IsAttatched)
  19. return;
  20. if (_oOut == IntPtr.Zero)
  21. _oOut = GetStdHandle(-11);
  22. // Store Current Window
  23. IntPtr currWnd = GetForegroundWindow();
  24. //Check for existing console before allocating
  25. if (GetConsoleWindow() == IntPtr.Zero)
  26. if (!AllocConsole())
  27. throw new Exception("AllocConsole() failed");
  28. // Restore Foreground
  29. SetForegroundWindow(currWnd);
  30. _cOut = CreateFile("CONOUT$", 0x80000000 | 0x40000000, 2, IntPtr.Zero, 3, 0, IntPtr.Zero);
  31. BepInEx.ConsoleUtil.Kon.conOut = _cOut;
  32. if (!SetStdHandle(-11, _cOut))
  33. throw new Exception("SetStdHandle() failed");
  34. Init();
  35. IsAttatched = true;
  36. }
  37. public static string Title
  38. {
  39. set
  40. {
  41. if (!IsAttatched)
  42. return;
  43. if (value == null)
  44. {
  45. throw new ArgumentNullException(nameof(value));
  46. }
  47. if (value.Length > 24500)
  48. {
  49. throw new InvalidOperationException("Console title too long");
  50. }
  51. if (!SetConsoleTitle(value))
  52. {
  53. throw new InvalidOperationException("Console title invalid");
  54. }
  55. }
  56. }
  57. public static void Detach()
  58. {
  59. if (!IsAttatched)
  60. return;
  61. if (!CloseHandle(_cOut))
  62. throw new Exception("CloseHandle() failed");
  63. _cOut = IntPtr.Zero;
  64. if (!FreeConsole())
  65. throw new Exception("FreeConsole() failed");
  66. if (!SetStdHandle(-11, _oOut))
  67. throw new Exception("SetStdHandle() failed");
  68. Init();
  69. IsAttatched = false;
  70. }
  71. [DllImport("user32.dll")]
  72. private static extern IntPtr GetForegroundWindow();
  73. [DllImport("user32.dll")]
  74. [return: MarshalAs(UnmanagedType.Bool)]
  75. static extern bool SetForegroundWindow(IntPtr hWnd);
  76. [DllImport("kernel32.dll", SetLastError = true)]
  77. private static extern bool AllocConsole();
  78. [DllImport("kernel32.dll")]
  79. private static extern IntPtr GetConsoleWindow();
  80. [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
  81. private static extern bool CloseHandle(IntPtr handle);
  82. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  83. private static extern IntPtr CreateFile(
  84. string fileName,
  85. uint desiredAccess,
  86. int shareMode,
  87. IntPtr securityAttributes,
  88. int creationDisposition,
  89. int flagsAndAttributes,
  90. IntPtr templateFile);
  91. [DllImport("kernel32.dll", SetLastError = false)]
  92. private static extern bool FreeConsole();
  93. [DllImport("kernel32.dll", SetLastError = true)]
  94. private static extern IntPtr GetStdHandle(int nStdHandle);
  95. private static void Init()
  96. {
  97. var stdOut = Console.OpenStandardOutput();
  98. var stdWriter = new StreamWriter(stdOut, Encoding.Default)
  99. {
  100. AutoFlush = true
  101. };
  102. Console.SetOut(stdWriter);
  103. Console.SetError(stdWriter);
  104. }
  105. [DllImport("kernel32.dll", SetLastError = true)]
  106. private static extern bool SetStdHandle(int nStdHandle, IntPtr hConsoleOutput);
  107. [DllImport("kernel32.dll", BestFitMapping = true, CharSet = CharSet.Auto, SetLastError = true)]
  108. private static extern bool SetConsoleTitle(string title);
  109. }
  110. }