Registering a Control Handler Function (Windows)
来源:互联网 发布:htc手机移动网络不可用 编辑:程序博客网 时间:2024/06/05 21:02
From: http://msdn.microsoft.com/ZH-CN/library/windows/desktop/ms685049%28v=vs.85%29.aspx
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 returnsTRUE and the process terminates.
When a CTRL_BREAK_EVENT, CTRL_LOGOFF_EVENT, orCTRL_SHUTDOWN_EVENT signal is received, the control handler returnsFALSE. 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 returnsTRUE, the default handler will be used, resulting in the process being terminated.
#include <windows.h> #include <stdio.h> BOOL CtrlHandler( DWORD fdwCtrlType ) { switch( fdwCtrlType ) { // Handle the CTRL-C signal. case CTRL_C_EVENT: printf( "Ctrl-C event\n\n" ); Beep( 750, 300 ); return( TRUE ); // CTRL-CLOSE: confirm that the user wants to exit. case CTRL_CLOSE_EVENT: Beep( 600, 200 ); printf( "Ctrl-Close event\n\n" ); return( TRUE ); // Pass other signals to the next handler. case CTRL_BREAK_EVENT: Beep( 900, 200 ); printf( "Ctrl-Break event\n\n" ); return FALSE; case CTRL_LOGOFF_EVENT: Beep( 1000, 200 ); printf( "Ctrl-Logoff event\n\n" ); return FALSE; case CTRL_SHUTDOWN_EVENT: Beep( 750, 500 ); printf( "Ctrl-Shutdown event\n\n" ); return FALSE; default: return FALSE; } } int main( void ) { if( SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, TRUE ) ) { printf( "\nThe Control Handler is installed.\n" ); printf( "\n -- Now try pressing Ctrl+C or Ctrl+Break, or" ); printf( "\n try logging off or closing the console...\n" ); printf( "\n(...waiting in a loop for events...)\n\n" ); while( 1 ){ } } else { printf( "\nERROR: Could not set control handler"); return 1; }return 0;}
- Registering a Control Handler Function (Windows)
- Writing a Control Handler Function
- Function not available to this responsibility After Creating and Registering a New Form
- Registering an Application to a URL Protocol
- Registering an Application to a URL Protocol
- Registering an Application to a URL Protocol
- Registering an Application to a URL Protocol
- Registering an Application to a URL Protocol
- Registering an Application to a URL Protocol
- Registering an Application to a URL Protocol
- Registering an Application to a URL Protocol
- Registering an Application to a URL Protocol
- Registering an Application to a URL Protocol
- Registering an Application to a URL Protocol
- Registering an Application to a URL Protocol
- Registering an Application to a URL Protocol
- Registering an Application to a URL Protocol
- Registering an Application to a URL Protocol
- Autofac学习教程
- 让Qt应用程序移植到Android上
- List of TCP and UDP port numbers
- 【2012年腾讯俱乐部ACM赛新手组1004】XOR的用法-Poor man on the Single’s Day
- CF 109D
- Registering a Control Handler Function (Windows)
- 世间再无乔布斯
- Ios5(五)多视图pickers应用程序实现
- uva_439 - Knight Moves
- 分水岭算法原理
- C/C++基础知识:函数指针和指针函数的基本概念
- HierarchyViewer 使用
- 第13周项目-汉诺塔
- 简单题 ( hdu2114 )