相当于“SIGINT”(posix)信号,用于捕获Windows / MinGW下的“CTRL + C”

我正在windows下移植一个Linux / gcc程序,并为这两个程序实现了通用的exception处理。 我想知道MinGW / gcc的SIGINT信号相当于什么。

下面是我在Linux下如何处理它:

 static void handler(int sig) { // Catch exceptions switch(sig) { case SIGABRT: fputs("Caught SIGABRT: usually caused by an abort() or assert()\n", stderr); break; case SIGFPE: fputs("Caught SIGFPE: arithmetic exception, such as divide by zero\n", stderr); break; case SIGILL: fputs("Caught SIGILL: illegal instruction\n", stderr); break; case SIGINT: fputs("Caught SIGINT: interactive attention signal, probably a ctrl+c\n", stderr); break; case SIGSEGV: fputs("Caught SIGSEGV: segfault\n", stderr); break; case SIGTERM: default: fputs("Caught SIGTERM: a termination request was sent to the program\n", stderr); break; } // Ctrl+C interrupt => No backtrace if (sig != (int)SIGINT) { fprintf(stderr, "Error: signal %d:\n", sig); posix_print_stack_trace(); } exit(sig); } signal(SIGABRT, handler); signal(SIGFPE, handler); signal(SIGILL, handler); signal(SIGINT, handler); signal(SIGSEGV, handler); signal(SIGTERM, handler); 

在Windows下,这看起来像:

 static LONG WINAPI windows_exception_handler(EXCEPTION_POINTERS * ExceptionInfo) { switch(ExceptionInfo->ExceptionRecord->ExceptionCode) { case EXCEPTION_ACCESS_VIOLATION: fputs("Error: EXCEPTION_ACCESS_VIOLATION\n", stderr); break; case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: fputs("Error: EXCEPTION_ARRAY_BOUNDS_EXCEEDED\n", stderr); break; case EXCEPTION_BREAKPOINT: fputs("Error: EXCEPTION_BREAKPOINT\n", stderr); break; ... } } if (EXCEPTION_STACK_OVERFLOW != ExceptionInfo->ExceptionRecord->ExceptionCode) { windows_print_stacktrace(ExceptionInfo->ContextRecord); } 

我的问题是,我没有看到在Windows EXCEPTION_*可用的任何等效的SIGINT

在Windows(MinGW / gcc)下如何捕获CTRL + C中断?

非常感谢。

Solutions Collecting From Web of "相当于“SIGINT”(posix)信号,用于捕获Windows / MinGW下的“CTRL + C”"

如果你想捕获ctrl + c SetConsoleCtrlHandler可能是你在找什么。

 #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <signal.h> BOOL WINAPI ConsoleHandler(DWORD); int main(int argc, char *argv[]) { if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleHandler,TRUE)) { fprintf(stderr, "Unable to install handler!\n"); return EXIT_FAILURE; } for (;;) ; /* Null body. */ return EXIT_SUCCESS; } BOOL WINAPI ConsoleHandler(DWORD dwType) { switch(dwType) { case CTRL_C_EVENT: printf("ctrl-c\n"); break; case CTRL_BREAK_EVENT: printf("break\n"); break; default: printf("Some other event\n"); } return TRUE; }