如何使用 HINSTANCE 终止应用程序

发布于 2024-11-08 17:04:04 字数 124 浏览 0 评论 0原文

我正在使用 ShellExecute 从 C++ 生成一个应用程序,因此我有该应用程序的实例。

我现在如何使用该 HINSTANCE 关闭它?我可以使用 WaitForSingleObject() 等待应用程序完成吗?

I am spawning an application from c++ with ShellExecute, so I have the HINSTANCE of the app.

How can I close it now using that HINSTANCE? And can I use WaitForSingleObject() to wait for the app to finish?

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

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

发布评论

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

评论(2

嗫嚅 2024-11-15 17:04:04

没有办法优雅地关闭进程。使用 有一种突然的、不优雅的方式TerminateProcess,但这会阻止进程运行自己的清理代码,并可能留下一些“不稳定”的东西,请参阅快速概述 Windows XP 上进程如何退出

很长一段时间以来,关于“终止进程”的常识是向进程最顶层的无主窗口发送 WM_CLOSE,期望应用程序能够通过正常退出来响应。请参阅如何在 Win32 中“干净地”终止应用程序,以及有关相关主题的必读内容找到正确的窗口: 可以有多个 (或零):将进程转换为窗口

您还应该阅读我可以用 ShellExecute 函数返回的 HINSTANCE 做什么? 了解为什么您拥有的 HINSTANCE 不值得信赖,更不用说无用了...

There is no way to gracefully shutdown a process. There is an abrupt an non-graceful way by using TerminateProcess, but this will prevent the process from running its own cleanup code and may leave stuff 'in-limbo', see Quick overview of how processes exit on Windows XP.

For a long time now the common wisdom about 'terminating a process' was to send a WM_CLOSE to the process topmost unowned window(s), in expectation that the application will respond by quitting gracefully. See How To Terminate an Application "Cleanly" in Win32, and also this mandatory reading on the related subject of finding the right window: There can be more than one (or zero): Converting a process to a window

Ans you should also read What can I do with the HINSTANCE returned by the ShellExecute function? to understand why the HINSTANCE you have is not trustworthy, not to say useless...

寂寞花火° 2024-11-15 17:04:04

首先,HINSTANCE 在现代版本的 Windows 中几乎没有什么用处——但无论如何,您所拥有的并不是真正的 HINSTANCEShellExecute 的返回实际上只是一个大于或小于 32 的值,分别表示成功或失败。

幸运的是,如果您使用 ShellExecuteEx,您可以获得新进程的进程句柄,并且您可以使用它来控制进程。

@Remus 链接的 MSDN 文章很不错,但是(IMO)如果目标应用程序是(或可能是)控制台应用程序,那么还有另一个步骤可能有用。在这种情况下,它通常不会处理 WM_CLOSE 消息。但是,您可以将 DLL 注入到进程中,并让它从进程内部执行干净的关闭(例如,如果它调用 exit,并且目标程序是用 C 编写的,它将有机会刷新并关闭文件,运行任何用 atexit 注册的内容,等等)。

如果失败,您可能需要使用 GenerateConsoleCtrlEvent 向其发送控制中断。

然后,如果所有这些都无法退出,则调用 TerminateProcess

First of all, an HINSTANCE is of very little use in modern versions of Windows -- but what you have isn't really an HINSTANCE anyway. The return from ShellExecute is really just a value greater than or less than 32, to indicate success or failure respectively.

Fortunately, if you use ShellExecuteEx, you can get a process handle for the new process, and you can use that to control the process.

The MSDN article that @Remus linked is decent, but (IMO) there's another step that can be useful if the target application is (or might be) a console application. In this case, it usually won't handle a WM_CLOSE message. You can, however, inject a DLL into the process, and have that do a clean(ish) shutdown from inside the process (for example, if it calls exit, and the target program is written in C, it'll get a chance to flush and close files, run anything registered with atexit, etc., before dying).

If that fails, you might want to use GenerateConsoleCtrlEvent to send it a control-break.

Then, if all those fail to exit you call TerminateProcess.

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