C++ 如何通过 API 退出 Win32 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7247601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:40:47  来源:igfitidea点击:

How to exit Win32 application via API?

c++winapi

提问by bodacydo

I have a C++ Win32 application written using Win32 API and I wish to force it to exit in one of functions. Is there something like Exit()or Destroy()or Abort()something similar that would just terminate it?

我有一个使用 Win32 API 编写的 C++ Win32 应用程序,我希望强制它在其中一个函数中退出。有什么样Exit()Destroy()Abort()类似的东西,将刚刚结束了吗?

采纳答案by Necrolis

ExitProcessis the function to use, else you can use exit, note that neither insure the integrity of the shutdown (ie: you many leak handles etc and threaded operations will be terminated, even if they are halfway through an operation, like a db commit)

ExitProcess是要使用的函数,否则您可以使用exit,请注意,两者都不能确保关闭的完整性(即:您的许多泄漏句柄等和线程操作将被终止,即使它们在操作中途,如数据库提交)

回答by lisa

Aiiieeeeeeeeeeee. Don't do ANY of these things!

啊啊啊啊啊啊啊啊啊啊 不要做任何这些事情!

  • exit() and ExitProcess are the equivalent of taking a gun and shooting the process in the face.I hope I don't have to explain why that isn't nice, especially for processes that are holding shared objects like database handles that might be system-wide. I know that's the norm in Android, but this is Windows and we don't do that 'round here.

  • Calling PostQuitMessage() directly can cause memory leaks.PostQuitMessage() is intended to be called from WM_DESTROY. It is NOT NOT NOT a message intended to be used to ask a window to close. It's a callback mechanism for applications to post their exit code back to the shell.It is intended to populate WM_QUIT with the application exit code. If you call PostQuitMessage() directly, you will bypass WM_DESTROY entirely. This is problematic because many windows--and child widgets--very correctly perform their clean-up in WM_DESTROY.If you skip this message by lying to the window, you will deny components the chance to clean up.

  • And since we're on the topic, don't call WM_DESTROY directly either.Raymond Chen of Microsoft has a wonderful article where he explains "Sending a window a WM_DESTROY message is like prank calling somebody pretending to be the police".It's sort of the evil-twin of PostQuitMessage. Instead of exiting without cleaning up, you're asking the window to clean up without exiting. The window manager never knows to remove the components, and you're left with a dead orphan window in memory. Sigh.

  • exit() 和 ExitProcess 相当于拿枪射击面部。我希望我不必解释为什么这不好,特别是对于持有共享对象(如可能是系统范围的数据库句柄)的进程。我知道这是 Android 的常态,但这是 Windows,我们不会在这里这样做。

  • 直接调用 PostQuitMessage() 会导致内存泄漏。PostQuitMessage() 旨在从 WM_DESTROY 调用。它不是用于要求关闭窗口的消息。这是应用程序将其退出代码发布回 shell 的回调机制。它旨在用应用程序退出代码填充 WM_QUIT。如果您直接调用 PostQuitMessage(),您将完全绕过 WM_DESTROY。这是有问题的,因为许多窗口 - 和子小部件 -在 WM_DESTROY 中非常正确地执行它们的清理。如果您通过对窗口撒谎来跳过此消息,您将拒绝组件清理的机会。

  • 既然我们在讨论这个话题,也不要直接调用 WM_DESTROY。微软的 Raymond Chen 有一篇精彩的文章,他解释说“向窗口发送 WM_DESTROY 消息就像恶作剧打电话给假装警察的人”。它有点像 PostQuitMessage 的邪恶双胞胎。您不是不清理就退出,而是要求窗口清理而不退出。窗口管理器永远不知道要删除组件,因此内存中只剩下一个死的孤立窗口。叹。

The correct answer is to post WM_CLOSE.Anything using the default window procedure will correctly notify the window manager to destroy the window which will cascade on to WM_DESTROY then to WM_QUIT as intended.

正确答案是发布 WM_CLOSE。任何使用默认窗口过程的东西都会正确地通知窗口管理器销毁窗口,该窗口将按预期级联到 WM_DESTROY 然后到 WM_QUIT。

If you need different behavior than the standard close, the other alternative is to create a WM_USER or WM_APP message.If your window has a "fancy" WM_CLOSE--say, for example, you give the user a prompt "Are you sure you want to exit?"--and you want to skip that, you may define a custom message with WM_USER or WM_APP and call that in its place. Your app-defined message would implement the default close behavior, and the WM_CLOSE that's tied to the system gadgets performs your fancy behavior.

如果您需要与标准关闭不同的行为,另一种选择是创建 WM_USER 或 WM_APP 消息。如果你的窗口有一个“花哨的”WM_CLOSE——比如说,你给用户一个提示“你确定要退出吗?”——而你想跳过那个,你可以用 WM_USER 定义一个自定义消息或WM_APP 并在其位置调用它。您的应用定义的消息将实现默认关闭行为,而与系统小工具相关联的 WM_CLOSE 将执行您的奇特行为。

I hope that helps!

我希望这有帮助!

回答by Coincoin

PostQuitMessage()is the standard, graceful way of quitting a windowed application.

PostQuitMessage()是退出窗口应用程序的标准、优雅的方式。

It will not immediately quit but rather post a quit message that will get parsed by your main loop (if done correctly) and then quit the application.

它不会立即退出,而是发布一条退出消息,该消息将被您的主循环解析(如果正确完成),然后退出应用程序。

回答by Chad

If you're using a Win32application with a proper window procedure, you can use the function PostQuitMessage.

如果您使用Win32具有适当窗口过程的应用程序,则可以使用函数PostQuitMessage.

回答by Nawaz

If you're working with visual C++, then use:

如果您使用的是 Visual C++,请使用:

回答by tenfour

Just return from the WinMainfunction. If you have a message loop, use PostQuitMessageto break out. Don't try to exit the process in the middle of execution; it's sloppy.

只需从WinMain函数返回。如果您有消息循环,请使用PostQuitMessage来中断。不要试图在执行过程中退出;它很草率。