Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent to "SIGINT" (posix) signal for catching "CTRL+C" under Windows/MinGW

I am porting a Linux/gcc program under windows and implemented common exceptions handling for both. I was wondering what would be the equivalent of SIGINT signal for MinGW/gcc.

Here is how I handle it under 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);

Under Windows, this looks like :

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);
}

My problem is that I don't see any equivalent of SIGINT in the EXCEPTION_* available for Windows.

How would it be done catching a "CTRL+C" interruption under Windows (MinGW/gcc) ?

Thanks a lot.

like image 656
Gauthier Boaglio Avatar asked May 30 '13 00:05

Gauthier Boaglio


People also ask

Is Ctrl C a SIGINT?

By default, when a console window has the keyboard focus, CTRL + C or CTRL + BREAK is treated as a signal (SIGINT or SIGBREAK) and not as keyboard input.

How do you get a SIGINT signal?

When Ctrl+C is pressed, SIGINT signal is generated, we can catch this signal and run our defined signal handler. C standard defines following 6 signals in signal.

How do I catch a Ctrl C event in C?

Master C and Embedded C Programming- Learn as you go The CTRL + C is one signal in C or C++. So we can catch by signal catching technique. For this signal, the code is SIGINT (Signal for Interrupt). Here the signal is caught by signal() function.


1 Answers

If you want to catch ctrl+c SetConsoleCtrlHandler may be what you are looking for.

#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;
}
like image 197
LogicG8 Avatar answered Oct 07 '22 22:10

LogicG8