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";
///
/// The main entry point for the application.
///
[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
// ctrl-c
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);
}