C + + Win32 Console application catches shutdown events
#include # includeBOOLCtrlhandler (DWORD fdwctrltype) {Switch(fdwctrltype) {//handle the ctrl-c signal. Casectrl_c_event:printf ("ctrl-c event\n\n" ); return(true ); //ctrl-close:confirm The user wants to exit. Casectrl_close_event:printf ("ctrl-close event\n\n" ); return(true ); //Pass other signals to the next handler. Casectrl_break_event:printf ("Ctrl-break event\n\n" ); return false; Casectrl_logoff_event:printf ("Ctrl-logoff event\n\n" ); return false; Casectrl_shutdown_event:printf ("Ctrl-shutdown event\n\n" ); return false; default: return false; }}voidMainvoid ){ 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" ); while(1) {Sleep ( -);} }Elseprintf ("\nerror:could not set control handler");}
C + + Win32 Console application catches shutdown events