在 Windows 上运行任意子进程并仍然干净地终止?

发布于 2024-08-05 03:03:06 字数 688 浏览 7 评论 0原文

我有一个应用程序 A,我希望能够调用用户在配置文件中指定的任意其他进程。

批处理脚本 B 就是这样一个过程,用户希望由 A 调用。B 设置一些环境变量,显示一些消息并调用编译器 C 来执行一些工作。

Windows 是否提供了干净终止任意进程的标准方法?假设 A 在控制台中运行并收到 CTRL+C。它可以将其传递给 B 和 C 吗?假设A运行在一个窗口中,用户尝试关闭该窗口,是否可以取消B和C?

TerminateProcess 是一种选择,但不是一个很好的选择。如果 A 在 B 上使用 TerminateProcess,则 C 会继续运行。如果 C 长时间运行,这可能会导致严重的问题,因为我们可能会启动另一个 C 实例来操作相同的文件,而第一个 C 实例仍在秘密工作。此外,TerminateProcess 不会导致干净退出。

GenerateConsoleCtrlEvent 听起来不错,并且当所有内容都在控制台中运行时可能会起作用,但文档说您只能将 CTRL+C 发送到您自己的控制台,因此如果 A 在窗口中运行则无济于事。

Windows 上是否有与 SIGINT 等效的东西?我很想找到这样一篇文章: http://www.cons.org/cracauer/ sigint.html(适用于 Windows)。

I have an application A that I would like to be able to invoke arbitrary other processes as specified by a user in a configuration file.

Batch script B is one such process a user would like to be invoked by A. B sets up some environment variables, shows some messages and invokes a compiler C to do some work.

Does Windows provide a standard way for arbitrary processes to be terminated cleanly? Suppose A is run in a console and receives a CTRL+C. Can it pass this on to B and C? Suppose A runs in a window and the user tries to close the window, can it cancel B and C?

TerminateProcess is an option, but not a very good one. If A uses TerminateProcess on B, C keeps running. This could cause nasty problems if C is long-running, since we might start another instance of C to operate on the same files while the first instance of C is still secretly at work. In addition, TerminateProcess doesn't result in a clean exit.

GenerateConsoleCtrlEvent sounds nice, and might work when everything's running in a console, but the documentation says that you can only send CTRL+C to your own console, and so wouldn't help if A were running in a window.

Is there any equivalent to SIGINT on Windows? I would love to find an article like this one: http://www.cons.org/cracauer/sigint.html for Windows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拔了角的鹿 2024-08-12 03:03:06

正如 @Shakta 所说 GenerateConsoleCtrlEvent() 非常棘手,但您可以在没有帮助进程的情况下发送 Ctrl+C。

void SendControlC(int pid)
{
    FreeConsole(); // detach from the current console
    AttachConsole(pid); // attach to process console
    SetConsoleCtrlHandler(NULL, TRUE); // disable Control+C handling for our app
    GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0); // generate Control+C event
}

As @Shakta said GenerateConsoleCtrlEvent() is very tricky, but you can send Ctrl+C without helper process.

void SendControlC(int pid)
{
    FreeConsole(); // detach from the current console
    AttachConsole(pid); // attach to process console
    SetConsoleCtrlHandler(NULL, TRUE); // disable Control+C handling for our app
    GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0); // generate Control+C event
}
懒猫 2024-08-12 03:03:06

我想我在这个问题上有点晚了,但无论如何我都会为遇到同样问题的人写一些东西。

我的问题是类似的,我希望我的应用程序是一个 GUI 应用程序,但执行的进程应该在后台运行,而不附加任何交互式控制台窗口。

我设法使用GenerateConsoleCtrlEvent()解决了这个问题。棘手的部分是文档并不清楚它的具体使用方式和陷阱。

我的解决方案基于所描述的内容 这里。但这也没有真正解释所有细节,因此这里是有关如何使其工作的详细信息。

  1. 创建一个新的帮助应用程序“Helper.exe”。该应用程序将位于您的应用程序(父级)和您希望能够关闭的子进程之间。它还将创建实际的子进程。您必须有这个“中间人”进程,否则GenerateConsoleCtrlEvent() 将失败。

  2. 使用某种 IPC 机制从父进程向帮助程序进程传达帮助程序应该关闭子进程的信息。当助手收到此事件时,它会调用“GenerateConsoleCtrlEvent(CTRL_BREAK, 0)”,该事件会关闭自身和子进程。我自己为此使用了一个事件对象,当父进程想要​​取消子进程时,它会完成该事件对象。

要创建 Helper.exe,请使用 CREATE_NO_WINDOW 和 CREATE_NEW_PROCESS_GROUP 创建它。
创建子进程时,创建的子进程不带标志 (0),这意味着它将从其父进程派生控制台。如果不这样做将导致它忽略该事件。

每一步都这样完成是非常重要的。我一直在尝试各种不同的组合,但这种组合是唯一有效的。您无法发送 CTRL_C 事件。它将返回成功,但会被进程忽略。 CTRL_BREAK 是唯一有效的方法。这并不重要,因为它们最终都会调用 ExitProcess() 。

您也不能使用子进程 id 的进程分组 id 直接调用GenerateConsoleCtrlEvent(),从而允许帮助进程继续生存。这也会失败。

我花了一整天的时间试图让它发挥作用。这个解决方案对我有用,但如果有人有其他需要添加的内容,请做。我去了所有其他网络,发现很多人有类似的问题,但没有明确的解决方案。 GenerateConsoleCtrlEvent() 的工作原理也有点奇怪,所以如果有人知道更多细节,请分享。

I guess I'm a bit late on this question but I'll write something anyway for anyone having the same problem.

My problem is similar in that I'd like my application to be a GUI application but the processes executed should be run in the background without any interactive console window attached.

I managed to solve this using GenerateConsoleCtrlEvent(). The tricky part is just that the documentation is not really clear on exactly how it can be used and the pitfalls with it.

My solution is based on what is described here. But that didn't really explain all the details either, so here is the details on how to get it working.

  1. Create a new helper application "Helper.exe". This application will sit between your application (parent) and the child process you want to be able to close. It will also create the actual child process. You must have this "middle man" process or GenerateConsoleCtrlEvent() will fail.

  2. Use some kind of IPC mechanism to communicate from the parent to the helper process that the helper should close the child process. When the helper get this event it calls "GenerateConsoleCtrlEvent(CTRL_BREAK, 0)" which closes down itself and the child process. I used an event object for this myself which the parent completes when it wants to cancel the child process.

To create your Helper.exe create it with CREATE_NO_WINDOW and CREATE_NEW_PROCESS_GROUP.
And when creating the child process create it with no flags (0) meaning it will derive the console from its parent. Failing to do this will cause it to ignore the event.

It is very important that each step is done like this. I've been trying all different kinds of combinations but this combination is the only one that works. You can't send a CTRL_C event. It will return success but will be ignored by the process. CTRL_BREAK is the only one that works. Doesn't really matter since they will both call ExitProcess() in the end.

You also can't call GenerateConsoleCtrlEvent() with a process groupd id of the child process id directly allowing the helper process to continue living. This will fail as well.

I spent a whole day trying to get this working. This solution works for me but if anyone has anything else to add please do. I went all other the net finding lots of people with similar problems but no definite solution to the problem. How GenerateConsoleCtrlEvent() works is also a bit weird so if anyone knows more details on it please share.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文