C++ WIN32控制台异常关闭回调函数

简介:
复制代码
/*
This is an example of the SetConsoleCtrlHandler function that is used to install a control handler.


When a CTRL+C signal is received, the control handler returns TRUE, indicating that it has handled the signal. Doing this prevents other control handlers from being called.

When a CTRL_CLOSE_EVENT signal is received, the control handler returns TRUE, causing the system to display a dialog box that gives the user the choice of terminating the process and closing the console or allowing the process to continue execution. If the user chooses not to terminate the process, the system closes the console when the process finally terminates.

When a CTRL+BREAK, CTRL_LOGOFF_EVENT, or CTRL_SHUTDOWN_EVENT signal is received, the control handler returns FALSE. Doing this causes the signal to be passed to the next control handler function. If no other control handlers have been registered or none of the registered handlers returns TRUE, the default handler will be used, resulting in the process being terminated.

Note that MyErrorExit is a placeholder for an application-defined function to display and handle error conditions.*/

BOOL CtrlHandler(DWORD fdwCtrlType) 
{ 
    switch (fdwCtrlType) 
    { 
        // Handle the CTRL+C signal. 
 
        case CTRL_C_EVENT: 
 
            Beep(1000, 1000); 
            return TRUE; 
 
        // CTRL+CLOSE: confirm that the user wants to exit. 
 
        case CTRL_CLOSE_EVENT: 
 
            return TRUE; 
 
        // Pass other signals to the next handler. 
 
        case CTRL_BREAK_EVENT: 
 
        case CTRL_LOGOFF_EVENT: 
 
        case CTRL_SHUTDOWN_EVENT: 
 
        default: 
 
            return FALSE; 
    } 
} 
 
void main(void) 
{ 
    BOOL fSuccess; 
 
    fSuccess = SetConsoleCtrlHandler( 
        (PHANDLER_ROUTINE) CtrlHandler,  // handler function 
        TRUE);                           // add to list 
    if (! fSuccess) 
        MyErrorExit("Could not set control handler"); 
}
复制代码

 本文转自cococo点点博客园博客,原文链接:http://www.cnblogs.com/coder2012/p/3177802.html,如需转载请自行联系原作者

相关文章
|
6月前
|
定位技术 C++ Windows
第一人称射击游戏 C++控制台版(未完成)
第一人称射击游戏 C++控制台版(未完成)
第一人称射击游戏 C++控制台版(未完成)
|
6月前
|
C++
俄罗斯方块 C++控制台版
俄罗斯方块 C++控制台版
|
IDE 开发工具 C++
[记录][问题]Win32调用C++/WinRT DLL
[记录][问题]Win32调用C++/WinRT DLL
|
7月前
|
C++
c++实现通讯录管理系统(控制台版)
c++实现通讯录管理系统(控制台版)
|
存储 算法 安全
c++游戏制作指南(一):在冷峻的控制台上,种满缤纷
c++游戏制作指南(一):在冷峻的控制台上,种满缤纷
748 0
|
C语言 C++ Windows
【c++】设置控制台窗口字体颜色和背景色(system和SetConsoleTextAttribute函数 )(内含超好玩的c++游戏链接)
【c++】设置控制台窗口字体颜色和背景色(system和SetConsoleTextAttribute函数 )(内含超好玩的c++游戏链接)
712 0
【c++】设置控制台窗口字体颜色和背景色(system和SetConsoleTextAttribute函数 )(内含超好玩的c++游戏链接)
|
C++
C++ 控制台窗口中MessageBox() 的用法
C++ 控制台窗口中MessageBox() 的用法
307 0
|
C++
C++ 设置控制台文本属性画一个DOS时代的字符窗口
C++ 设置控制台文本属性画一个DOS时代的字符窗口
79 0
|
API C语言 C++
C语言或者C++中隐藏控制台窗口
C语言或者C++中隐藏控制台窗口
539 0
|
API C++
C/C++ 改变控制台输文字颜色:SetConsoleTextAttribute()
C/C++ 改变控制台输文字颜色:SetConsoleTextAttribute()
571 0
C/C++ 改变控制台输文字颜色:SetConsoleTextAttribute()