Program.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System.Diagnostics;
  2. using SinMaiLauncher.ChildProcessHolder;
  3. using SinMaiLauncher.Interops;
  4. namespace SinMaiLauncher;
  5. internal static class Program
  6. {
  7. private const string CmdGetConsoleWindow = "get-console-window";
  8. private const string CmdCtrlC = "ctrl-c";
  9. private const string CmdDisableQuickEdit = "disable-quick-edit";
  10. /// <summary>
  11. /// The main entry point for the application.
  12. /// </summary>
  13. [STAThread]
  14. private static int Main(string[] args)
  15. {
  16. if (args.Length == 0)
  17. {
  18. // To customize application configuration such as set high DPI settings or default font,
  19. // see https://aka.ms/applicationconfiguration.
  20. ApplicationConfiguration.Initialize();
  21. Application.Run(new MainForm());
  22. return 0;
  23. }
  24. //args:
  25. // get-console-window <pid>
  26. // ctrl-c <pid>
  27. if (args.Length == 2)
  28. {
  29. switch (args[0])
  30. {
  31. case CmdGetConsoleWindow:
  32. #if DEBUG
  33. //Debugger.Launch();
  34. //Debugger.Break();
  35. #endif
  36. return (int?)ConsoleInterops.ExternalProcess.GetConsoleWindow(uint.Parse(args[1])) ?? -1;
  37. case CmdDisableQuickEdit:
  38. return ConsoleInterops.ExternalProcess.DisableQuickEdit(uint.Parse(args[1])) ? 1 : 0;
  39. case CmdCtrlC:
  40. return (int)ConsoleInterops.ExternalProcess.SendCtrlC(uint.Parse(args[1]));
  41. }
  42. }
  43. return -1;
  44. }
  45. internal static int GetConsoleWindow(int pid)
  46. {
  47. var pProcess = new Process
  48. {
  49. StartInfo =
  50. {
  51. FileName = Application.ExecutablePath,
  52. ArgumentList = { CmdGetConsoleWindow, pid.ToString() }
  53. }
  54. };
  55. pProcess.Start();
  56. pProcess.WaitForExit();
  57. var ret = pProcess.ExitCode;
  58. return ret;
  59. }
  60. internal static bool DisableQuickEdit(int pid)
  61. {
  62. var pProcess = new Process
  63. {
  64. StartInfo =
  65. {
  66. FileName = Application.ExecutablePath,
  67. ArgumentList = { CmdDisableQuickEdit, pid.ToString() }
  68. }
  69. };
  70. pProcess.Start();
  71. pProcess.WaitForExit();
  72. var ret = pProcess.ExitCode;
  73. return ret == 1;
  74. }
  75. internal static ConsoleInterops.ExternalProcess.ResultOfSendCtrlC SendCtrlC(int pid)
  76. {
  77. var pProcess = new Process
  78. {
  79. StartInfo =
  80. {
  81. FileName = Application.ExecutablePath,
  82. ArgumentList = { CmdCtrlC, pid.ToString() }
  83. }
  84. };
  85. pProcess.Start();
  86. pProcess.WaitForExit();
  87. var ret = pProcess.ExitCode;
  88. return (ConsoleInterops.ExternalProcess.ResultOfSendCtrlC)ret;
  89. }
  90. public static Process CreateConsoleProcess(string workingDir, string exePath, string args)
  91. {
  92. return new Process
  93. {
  94. StartInfo =
  95. {
  96. UseShellExecute = true,
  97. CreateNoWindow = false,
  98. RedirectStandardInput = false,
  99. RedirectStandardError = false,
  100. RedirectStandardOutput = false,
  101. WorkingDirectory = workingDir,
  102. FileName = exePath,
  103. Arguments = args,
  104. },
  105. EnableRaisingEvents = true
  106. };
  107. }
  108. public static Process CreateConsoleProcess(IProcessStartInfo si) => CreateConsoleProcess(si.WorkingDir, si.Exe, si.Arg);
  109. }