我在树中有 3 个进程:A -> B -> C。A 是一个主机应用程序,我不拥有它调用 CreateProcess
和 dwCreationFlags = CREATE_NEW_PROCESS_GROUP
创建进程 B。B 是我的服务,C 是我想使用 CreateProcess
生成的 Java 子进程,并且能够发送 Ctrl+C 以触发关闭 Hook 。
根据 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)
来工作的,您只需在添加真正的处理程序之前禁用默认处理程序。
关于java - 使用 CREATE_NEW_PROCESS_GROUP 创建主机时,将 Ctrl+C 发送到子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40232752/