process/src/Process.Win.Notify/Program.cs

26 lines
No EOL
960 B
C#

namespace Geekeey.Extensions.Process.Win.Notify;
internal static class Program
{
public static int Main(string[] args)
{
var processId = uint.Parse(args[0]);
var signal = uint.Parse(args[1]);
// detach from the current console, if one is attached to the process.
Interop.Kernel32.FreeConsole();
var success =
// attach to the target process group
Interop.Kernel32.AttachConsole(processId) &&
// set to ignore signals on self, so the proper exit code can be return
Interop.Kernel32.SetConsoleCtrlHandler(null, true) &&
// send the signal to the process group
Interop.Kernel32.GenerateConsoleCtrlEvent(signal, 0);
// we can use the kernel32 GetLastError here, instead of relaying on `Marshal.GetLastWin32Error`, because we
// are compiling this with a compiler which does not include gc. This can not cause gc pause which tampers with
// the last error code.
return success ? 0 : Interop.Kernel32.GetLastError();
}
}