C++:程序终止时执行函数
C++: Execute function when program terminates
某些程序在终止时弹出 "Save before exit?" 消息。
我想知道我是否可以使用 C++ 控制台应用程序来实现它。
所以我尝试了一些标准函数,比如 signal
和 atexit
.
但它们仅在以下情况下有效:
main()
returns (atexit
)
- 通过 Ctrl+C 发送中断(在 Windows、
SIGINT
上)
- 发生错误(
SIGABRT
)
是啊,怎么样?是否只能使用 GUI 应用程序?
在评论中,您说:
I want exit events to happen when that 'X' button is pressed(On windows).
That's part of GUI I guess.
Than what kind of request is sent to program when the exit button of the console is pressed?
您可以使用 SetConsoleCtrlHandler()
to register a user defined callback function 在控制台 window 关闭时收到 CTRL_CLOSE_EVENT
通知:
A signal that the system sends to all processes attached to a console when the user closes the console (either by clicking Close on the console window's window menu, or by clicking the End Task button command from Task Manager).
某些程序在终止时弹出 "Save before exit?" 消息。
我想知道我是否可以使用 C++ 控制台应用程序来实现它。
所以我尝试了一些标准函数,比如 signal
和 atexit
.
但它们仅在以下情况下有效:
main()
returns (atexit
)- 通过 Ctrl+C 发送中断(在 Windows、
SIGINT
上) - 发生错误(
SIGABRT
)
是啊,怎么样?是否只能使用 GUI 应用程序?
在评论中,您说:
I want exit events to happen when that 'X' button is pressed(On windows). That's part of GUI I guess. Than what kind of request is sent to program when the exit button of the console is pressed?
您可以使用 SetConsoleCtrlHandler()
to register a user defined callback function 在控制台 window 关闭时收到 CTRL_CLOSE_EVENT
通知:
A signal that the system sends to all processes attached to a console when the user closes the console (either by clicking Close on the console window's window menu, or by clicking the End Task button command from Task Manager).