Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
654 views
in Technique[技术] by (71.8m points)

c++ - 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()
", stderr);
        break;
    case SIGFPE:
        fputs("Caught SIGFPE: arithmetic exception, such as divide by zero
",
                stderr);
        break;
    case SIGILL:
        fputs("Caught SIGILL: illegal instruction
", stderr);
        break;
    case SIGINT:
        fputs("Caught SIGINT: interactive attention signal, probably a ctrl+c
",
                stderr);
        break;
    case SIGSEGV:
        fputs("Caught SIGSEGV: segfault
", stderr);
        break;
    case SIGTERM:
    default:
        fputs("Caught SIGTERM: a termination request was sent to the program
",
                stderr);
        break;
    }

    // Ctrl+C interrupt => No backtrace
    if (sig != (int)SIGINT)
    {
        fprintf(stderr, "Error: signal %d:
", 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
", stderr);
        break;
    case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
        fputs("Error: EXCEPTION_ARRAY_BOUNDS_EXCEEDED
", stderr);
        break;
    case EXCEPTION_BREAKPOINT:
        fputs("Error: EXCEPTION_BREAKPOINT
", 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!
");
        return EXIT_FAILURE;
    }

    for (;;)
        ; /* Null body. */

    return EXIT_SUCCESS;
}

BOOL WINAPI ConsoleHandler(DWORD dwType)
{
    switch(dwType) {
    case CTRL_C_EVENT:
        printf("ctrl-c
");
        break;
    case CTRL_BREAK_EVENT:
        printf("break
");
        break;
    default:
        printf("Some other event
");
    }
    return TRUE;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...