123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using System.Runtime.InteropServices;
- namespace SinMaiLauncher.Interops;
- internal static class ConsoleInterops
- {
- private static class NativeConsole
- {
- [DllImport("kernel32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool AllocConsole();
- [DllImport("kernel32.dll")]
- public static extern nint GetStdHandle(int nStdHandle);
- [DllImport("kernel32.dll")]
- public static extern bool SetConsoleMode(nint hConsoleHandle, int dwMode);
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern nint GetConsoleWindow();
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern bool AttachConsole(uint dwProcessId);
- [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
- public static extern bool FreeConsole();
- [DllImport("kernel32.dll")]
- public static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate? HandlerRoutine, bool Add);
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern bool GenerateConsoleCtrlEvent(ConsoleCtrlEvent ctrlEvent, uint processGroupId);
- public delegate bool ConsoleCtrlDelegate(uint ctrlType);
-
- public enum ConsoleCtrlEvent
- {
- CTRL_C_EVENT = 0,
- CTRL_BREAK_EVENT = 1
- }
- public const int STD_INPUT_HANDLE = -10;
- public const int ENABLE_QUICK_EDIT_MODE = 0x0040;
- public const int ENABLE_EXTENDED_FLAGS = 0x0080;
- }
- public static class ExternalProcess
- {
- public static IntPtr? GetConsoleWindow(uint pid)
- {
- if (NativeConsole.AttachConsole(pid) == false) return null;
- var handle = NativeConsole.GetConsoleWindow();
- return handle;
- }
- public static bool DisableQuickEdit(uint pid)
- {
- if (NativeConsole.AttachConsole(pid) == false) return false;
- nint handle = NativeConsole.GetStdHandle(NativeConsole.STD_INPUT_HANDLE);
-
- int mode = NativeConsole.ENABLE_EXTENDED_FLAGS;
- return NativeConsole.SetConsoleMode(handle, mode);
- }
- public enum ResultOfSendCtrlC
- {
- Success = 0,
- AttachConsoleFailed = 1,
- GenerateCtrlEventFailed = 2
- }
- public static ResultOfSendCtrlC SendCtrlC(uint processId)
- {
-
- if (!NativeConsole.AttachConsole(processId)) return ResultOfSendCtrlC.AttachConsoleFailed;
-
- NativeConsole.SetConsoleCtrlHandler(null, true);
-
- if (!NativeConsole.GenerateConsoleCtrlEvent(NativeConsole.ConsoleCtrlEvent.CTRL_C_EVENT, 0))
- {
- return ResultOfSendCtrlC.GenerateCtrlEventFailed;
- }
-
- NativeConsole.FreeConsole();
-
- return ResultOfSendCtrlC.Success;
- }
- }
- public static class CurrentProcess
- {
- public static void ConsoleAllocate() => NativeConsole.AllocConsole();
- public static void ConsoleDisableQuickEdit()
- {
- nint handle = NativeConsole.GetStdHandle(NativeConsole.STD_INPUT_HANDLE);
-
- int mode = NativeConsole.ENABLE_EXTENDED_FLAGS;
- NativeConsole.SetConsoleMode(handle, mode);
- }
- public static void ConsoleWindowSetParent(Control container)
- {
- var consoleHandle = NativeConsole.GetConsoleWindow();
-
- WindowInterops.SetParent(consoleHandle, container.Handle);
-
- WindowInterops.RemoveBorderVisible(consoleHandle);
- }
- public static void ConsoleWindowFill(Control container)
- {
- WindowInterops.FillWindow(NativeConsole.GetConsoleWindow(), container);
- }
- }
- }
|