【问题标题】:Equivalent to "SIGINT" (posix) signal for catching "CTRL+C" under Windows/MinGW等效于在 Windows/MinGW 下捕获“CTRL+C”的“SIGINT”(posix)信号
【发布时间】:2013-05-25 10:25:03
【问题描述】:

我正在 Windows 下移植一个 Linux/gcc 程序,并为两者实现了常见的异常处理。我想知道 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”中断?

非常感谢。

【问题讨论】:

  • 它仍然是 SIGINT,无论如何在 Microsoft CRT 中。 SIGBREAK 用于 Ctrl+Break 击键。不,这不会通过为 SetUnhandledException() 安装的回调。它不是未处理的 :) SetConsoleCtrlHandler() 可以尽早看到它。

标签: c++ windows exception mingw sigint


【解决方案1】:

如果你想抓 ctrl+c SetConsoleCtrlHandler 可能就是你要找的。​​p>

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

【讨论】:

  • 请注意,Windows 从另一个线程调用它,而不是在同一线程上运行的信号处理程序。
猜你喜欢
  • 1970-01-01
  • 2011-11-22
  • 1970-01-01
  • 2015-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多