C++ Win32控制台应用程序捕捉关闭事件
参考
[url]http://blog.sina.com.cn/s/blog\_690878d50101ls05.html\[/url\]
没有什么好解释的,直接看代码吧
#include <iostream>
#include <sstream>
using namespace std;
BOOL ConsoleEventHandler(DWORD dwCtrlType)
{
switch(dwCtrlType)
{
case CTRL_C_EVENT:// handle the ctrl-c signal
{
printf( "ctrl-c event\n\n" );
return TRUE;
}
case CTRL_CLOSE_EVENT:// ctrl-close: confirm that the user wants to exit.
{
printf( "ctrl-close event\n\n" );
return TRUE;
}
case CTRL_BREAK_EVENT:// pass other signals to the next handler.
{
printf( "ctrl-break event\n\n" );
return TRUE;
}
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()
{
if(SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleEventHandler, 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(TRUE)
{
//Do not let me go baby
Sleep(5000);
}
}
else
{
printf( "\nerror: could not set control handler");
}
}
还没有评论,来说两句吧...