Created
January 6, 2012 02:45
-
-
Save uhziel/1568697 to your computer and use it in GitHub Desktop.
Set Console Ctrl Handler in Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* filename: SetConsoleCtrlHandler.c | |
* description: | |
* This is a example of the SetConsoleCtrlHandler function that is used to | |
* install a control handler. | |
* via http://msdn.microsoft.com/en-us/library/ms685049(v=VS.85).aspx | |
* author: uhziel([email protected]) | |
* history: | |
* version date ps | |
* ------- ---------- -------------------------------- | |
* 1.00 2010-04-29 create the file. | |
*/ | |
#include <stdio.h> | |
#include <windows.h> | |
BOOL CtrlHandler(DWORD fdwCtrlType); | |
int main(int argc, char *argv[]) | |
{ | |
if(SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler, TRUE)) | |
{ | |
printf("the control handler is installed.\n"); | |
printf("-- now try pressing Ctrl+C or Ctrl+Break, or\n"); | |
printf(" try closing me or logging off.\n"); | |
printf("...waiting in a loop for events...\n"); | |
while(1) {} | |
} | |
else | |
{ | |
printf("ERROR: could not set control handler.\n"); | |
} | |
return 0; | |
} | |
BOOL CtrlHandler(DWORD fdwCtrlType) | |
{ | |
switch (fdwCtrlType) | |
{ | |
/* handle the CTRL-C signal */ | |
case CTRL_C_EVENT: | |
printf("CTRL-C event\n"); | |
Beep(750, 300); | |
return TRUE; | |
/* handle the CTRL-BREAK signal */ | |
case CTRL_BREAK_EVENT: | |
printf("CTRL-BREAK event\n"); | |
Beep(900, 200); | |
return TRUE; | |
/* handle the CTRL-CLOSE signal */ | |
case CTRL_CLOSE_EVENT: | |
printf("CTRL-CLOSE event\n"); | |
Beep(600, 200); | |
return TRUE; | |
/* handle the CTRL-LOGOFF signal */ | |
case CTRL_LOGOFF_EVENT: | |
printf("CTRL-LOGOFF event\n"); | |
Beep(1000, 200); | |
return TRUE; | |
/* handle the CTRL-SHUTDOWN signal */ | |
case CTRL_SHUTDOWN_EVENT: | |
printf("CTRL-SHUTDOWN event\n"); | |
Beep(750, 500); | |
return TRUE; | |
default: | |
return FALSE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment