Skip to content

Instantly share code, notes, and snippets.

@lookat23
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lookat23/998743615df81356ccac to your computer and use it in GitHub Desktop.
Save lookat23/998743615df81356ccac to your computer and use it in GitHub Desktop.
独立的代码片段整理 留待以后组织成一个公共类
#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" );
return( true );
// ctrl-close: confirm that the user wants to exit.
case CTRL_CLOSE_EVENT:
printf( "ctrl-close event\n\n" );
return( true );
// pass other signals to the next handler.
case CTRL_BREAK_EVENT:
printf( "ctrl-break event\n\n" );
return false;
case CTRL_LOGOFF_EVENT:
printf( "ctrl-logoff event\n\n" );
return false;
case CTRL_SHUTDOWN_EVENT:
printf( "ctrl-shutdown event\n\n" );
return false;
default:
return false;
}
}
void 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 ){ Sleep(100);}
}
else
printf( "\nerror: could not set control handler");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment