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月前
|
IDE 开发工具 C++
[记录][问题]Win32调用C++/WinRT DLL
[记录][问题]Win32调用C++/WinRT DLL
|
8月前
|
C语言 C++ Windows
【c++】设置控制台窗口字体颜色和背景色(system和SetConsoleTextAttribute函数 )(内含超好玩的c++游戏链接)
【c++】设置控制台窗口字体颜色和背景色(system和SetConsoleTextAttribute函数 )(内含超好玩的c++游戏链接)
260 0
【c++】设置控制台窗口字体颜色和背景色(system和SetConsoleTextAttribute函数 )(内含超好玩的c++游戏链接)
|
11月前
|
C++
C++ 控制台窗口中MessageBox() 的用法
C++ 控制台窗口中MessageBox() 的用法
176 0
|
11月前
|
C++
C++ 设置控制台文本属性画一个DOS时代的字符窗口
C++ 设置控制台文本属性画一个DOS时代的字符窗口
57 0
|
数据安全/隐私保护 C++
C++控制台制作ATM机
C++控制台制作ATM机
266 0
|
API C语言 C++
C语言或者C++中隐藏控制台窗口
C语言或者C++中隐藏控制台窗口
408 0
|
API C++
C/C++ 改变控制台输文字颜色:SetConsoleTextAttribute()
C/C++ 改变控制台输文字颜色:SetConsoleTextAttribute()
437 0
C/C++ 改变控制台输文字颜色:SetConsoleTextAttribute()
C++控制台实现客户端与服务端即时通信(C/S)
C++控制台实现客户端与服务端即时通信(C/S)
287 0
|
C++ Windows
【Visual Studio】Visual Studio 2019 创建 Windows 控制台程序 ( 安装 ‘使用 C++ 的桌面开发‘ 组件 | 创建并运行 Windows 控制台程序 )(二)
【Visual Studio】Visual Studio 2019 创建 Windows 控制台程序 ( 安装 ‘使用 C++ 的桌面开发‘ 组件 | 创建并运行 Windows 控制台程序 )(二)
191 0
【Visual Studio】Visual Studio 2019 创建 Windows 控制台程序 ( 安装 ‘使用 C++ 的桌面开发‘ 组件 | 创建并运行 Windows 控制台程序 )(二)
|
C++ Windows
【Visual Studio】Visual Studio 2019 创建 Windows 控制台程序 ( 安装 ‘使用 C++ 的桌面开发‘ 组件 | 创建并运行 Windows 控制台程序 )(一)
【Visual Studio】Visual Studio 2019 创建 Windows 控制台程序 ( 安装 ‘使用 C++ 的桌面开发‘ 组件 | 创建并运行 Windows 控制台程序 )(一)
351 0
【Visual Studio】Visual Studio 2019 创建 Windows 控制台程序 ( 安装 ‘使用 C++ 的桌面开发‘ 组件 | 创建并运行 Windows 控制台程序 )(一)