ConsoleWindow.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. private static bool _attached;
  14. private static IntPtr _cOut;
  15. private static IntPtr _oOut;
  16. public static void Attach()
  17. {
  18. if (_attached)
  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. _attached = true;
  36. }
  37. public static void Detach()
  38. {
  39. if (!_attached)
  40. return;
  41. if (!CloseHandle(_cOut))
  42. throw new Exception("CloseHandle() failed");
  43. _cOut = IntPtr.Zero;
  44. if (!FreeConsole())
  45. throw new Exception("FreeConsole() failed");
  46. if (!SetStdHandle(-11, _oOut))
  47. throw new Exception("SetStdHandle() failed");
  48. Init();
  49. _attached = false;
  50. }
  51. [DllImport("user32.dll")]
  52. private static extern IntPtr GetForegroundWindow();
  53. [DllImport("user32.dll")]
  54. [return: MarshalAs(UnmanagedType.Bool)]
  55. static extern bool SetForegroundWindow(IntPtr hWnd);
  56. [DllImport("kernel32.dll", SetLastError = true)]
  57. private static extern bool AllocConsole();
  58. [DllImport("kernel32.dll")]
  59. private static extern IntPtr GetConsoleWindow();
  60. [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
  61. private static extern bool CloseHandle(IntPtr handle);
  62. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  63. private static extern IntPtr CreateFile(
  64. string fileName,
  65. uint desiredAccess,
  66. int shareMode,
  67. IntPtr securityAttributes,
  68. int creationDisposition,
  69. int flagsAndAttributes,
  70. IntPtr templateFile);
  71. [DllImport("kernel32.dll", SetLastError = false)]
  72. private static extern bool FreeConsole();
  73. [DllImport("kernel32.dll", SetLastError = true)]
  74. private static extern IntPtr GetStdHandle(int nStdHandle);
  75. private static void Init()
  76. {
  77. var stdOut = Console.OpenStandardOutput();
  78. var stdWriter = new StreamWriter(stdOut, Encoding.Default)
  79. {
  80. AutoFlush = true
  81. };
  82. Console.SetOut(stdWriter);
  83. Console.SetError(stdWriter);
  84. }
  85. [DllImport("kernel32.dll", SetLastError = true)]
  86. private static extern bool SetStdHandle(int nStdHandle, IntPtr hConsoleOutput);
  87. }
  88. }