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

26 lines
960 B
C#
Raw Normal View History

2024-05-10 21:40:41 +02:00
namespace Geekeey.Extensions.Process.Win.Notify;
2024-04-27 20:15:05 +02:00
internal static class Program
{
public static int Main(string[] args)
{
2024-05-10 21:40:41 +02:00
var processId = uint.Parse(args[0]);
var signal = uint.Parse(args[1]);
2024-04-27 20:15:05 +02:00
// detach from the current console, if one is attached to the process.
2024-05-10 21:40:41 +02:00
Interop.Kernel32.FreeConsole();
2024-04-27 20:15:05 +02:00
var success =
// attach to the target process group
2024-05-10 21:40:41 +02:00
Interop.Kernel32.AttachConsole(processId) &&
2024-04-27 20:15:05 +02:00
// set to ignore signals on self, so the proper exit code can be return
2024-05-10 21:40:41 +02:00
Interop.Kernel32.SetConsoleCtrlHandler(null, true) &&
2024-04-27 20:15:05 +02:00
// send the signal to the process group
2024-05-10 21:40:41 +02:00
Interop.Kernel32.GenerateConsoleCtrlEvent(signal, 0);
2024-04-27 20:15:05 +02:00
2024-05-10 21:40:41 +02:00
// 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();
2024-04-27 20:15:05 +02:00
}
}