使用 CREATE_NEW_PROCESS_GROUP 创建主机时将 Ctrl+C 发送到子进程

Send Ctrl+C to child process when host was created with CREATE_NEW_PROCESS_GROUP

我在树中有 3 个进程:A -> B -> C。A 是我不拥有的主机应用程序,它调用 CreateProcessdwCreationFlags = CREATE_NEW_PROCESS_GROUP 来创建进程 B。B是我的服务,C 是一个 Java 子进程,我想使用 CreateProcess 生成它并能够发送 Ctrl+C 以触发关闭挂钩。

根据 MSDN 上的进程创建标志页面,CREATE_NEW_PROCESS_GROUP 标志禁用所有子进程的 Ctrl+C:

The new process is the root process of a new process group. The process group includes all processes that are descendants of this root process. The process identifier of the new process group is the same as the process identifier, which is returned in the lpProcessInformation parameter. Process groups are used by the GenerateConsoleCtrlEvent function to enable sending a CTRL+BREAK signal to a group of console processes.If this flag is specified, CTRL+C signals will be disabled for all processes within the new process group.

我需要一种绕过此行为的方法,以便我可以向进程 C 发送 Ctrl+C 信号。可以发送 Ctrl+Break 但还不够,因为这是一个 Java 进程和 Ctrl+Break不会触发关机。我尝试使用 CREATE_NEW_CONSOLE 创建进程 C,假设这将允许新控制台接收所有信号,但 Ctrl+C 也在那里被阻止。有谁知道是否有某种方法可以覆盖或绕过 CREATE_NEW_PROCESS_GROUP 并取消此 Ctrl+C 阻止?

感谢哈利的建议!对我有用的是以下内容:

SetConsoleCtrlHandler(null, false);
SetConsoleCtrlHandler(MyActualHandler, true);

看起来进程组通过调用 SetConsoleCtrlHandler(null, true) 起作用,您只需在添加真正的处理程序之前禁用默认处理程序。