关闭 C++ 控制台应用程序时会发生什么

2021-12-17 00:00:00 winapi c++ console-application

我想这个问题说明了一切,但是,如果有人关闭了 C++ 控制台应用程序会发生什么?如,单击顶角的x".它会立即关闭吗?它会抛出某种异常吗?这是未定义的行为吗?

I guess the question says it all, but, what happens if someone closes a c++ console app? As in, clicks the "x" in the top corner. Does it instantly close? Does it throw some sort of exception? Is it undefined behavior?

推荐答案

关闭带有x"在顶角的 c++ 控制台应用程序会抛出一个 CTRL_CLOSE_EVENT,如果您设置,您可以捕获和处理使用 SetConsoleCtrlHandler 函数的控制处理程序.在那里,您可以覆盖关闭功能并执行您想做的任何事情,然后仍然可以选择执行默认行为.

Closing a c++ console app with the "x" in the top corner throws an CTRL_CLOSE_EVENT which you could catch and process if you set a control handler using the SetConsoleCtrlHandler function. In there you could override the close functionality and perform whatever you wished to do, and then optionally still perform the default behavior.

相关文章