123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- using System.Diagnostics;
- using SinMaiLauncher.ChildProcessHolder;
- using SinMaiLauncher.Interops;
- namespace SinMaiLauncher;
- internal static class Program
- {
- private const string CmdGetConsoleWindow = "get-console-window";
- private const string CmdCtrlC = "ctrl-c";
- private const string CmdDisableQuickEdit = "disable-quick-edit";
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- [STAThread]
- private static int Main(string[] args)
- {
- if (args.Length == 0)
- {
- // To customize application configuration such as set high DPI settings or default font,
- // see https://aka.ms/applicationconfiguration.
- ApplicationConfiguration.Initialize();
- Application.Run(new MainForm());
- return 0;
- }
- //args:
- // get-console-window <pid>
- // ctrl-c <pid>
- if (args.Length == 2)
- {
- switch (args[0])
- {
- case CmdGetConsoleWindow:
- #if DEBUG
- //Debugger.Launch();
- //Debugger.Break();
- #endif
- return (int?)ConsoleInterops.ExternalProcess.GetConsoleWindow(uint.Parse(args[1])) ?? -1;
- case CmdDisableQuickEdit:
- return ConsoleInterops.ExternalProcess.DisableQuickEdit(uint.Parse(args[1])) ? 1 : 0;
- case CmdCtrlC:
- return (int)ConsoleInterops.ExternalProcess.SendCtrlC(uint.Parse(args[1]));
- }
- }
- return -1;
- }
- internal static int GetConsoleWindow(int pid)
- {
- var pProcess = new Process
- {
- StartInfo =
- {
- FileName = Application.ExecutablePath,
- ArgumentList = { CmdGetConsoleWindow, pid.ToString() }
- }
- };
- pProcess.Start();
- pProcess.WaitForExit();
- var ret = pProcess.ExitCode;
- return ret;
- }
- internal static bool DisableQuickEdit(int pid)
- {
- var pProcess = new Process
- {
- StartInfo =
- {
- FileName = Application.ExecutablePath,
- ArgumentList = { CmdDisableQuickEdit, pid.ToString() }
- }
- };
- pProcess.Start();
- pProcess.WaitForExit();
- var ret = pProcess.ExitCode;
- return ret == 1;
- }
- internal static ConsoleInterops.ExternalProcess.ResultOfSendCtrlC SendCtrlC(int pid)
- {
- var pProcess = new Process
- {
- StartInfo =
- {
- FileName = Application.ExecutablePath,
- ArgumentList = { CmdCtrlC, pid.ToString() }
- }
- };
- pProcess.Start();
- pProcess.WaitForExit();
- var ret = pProcess.ExitCode;
- return (ConsoleInterops.ExternalProcess.ResultOfSendCtrlC)ret;
- }
- public static Process CreateConsoleProcess(string workingDir, string exePath, string args)
- {
- return new Process
- {
- StartInfo =
- {
- UseShellExecute = true,
- CreateNoWindow = false,
- RedirectStandardInput = false,
- RedirectStandardError = false,
- RedirectStandardOutput = false,
- WorkingDirectory = workingDir,
- FileName = exePath,
- Arguments = args,
- },
- EnableRaisingEvents = true
- };
- }
- public static Process CreateConsoleProcess(IProcessStartInfo si) => CreateConsoleProcess(si.WorkingDir, si.Exe, si.Arg);
- }
|