#include <stdio.h>
#include <stdlib.h>
bool ctrlhandler(DWORD fdwctrltype)
{
switch (fdwctrltype)
{
// handle the ctrl-c signal.
case CTRL_C_EVENT:
printf("ctrl-c event\n\n");
return(false);
// 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;
}
}
int main(int argc, char **argv) {
if (SetConsoleCtrlHandler((PHANDLER_ROUTINE)ctrlhandler, true))
{
while (1) { Sleep(1000); }
}
else
{
printf("exit \r\n");
}
}
win32 c++控制台应用程序捕获ctrl+c程序关闭事件
最新推荐文章于 2024-03-07 09:49:56 发布