From 1e0fc67981e101495c2e236efb37cb5db83af400 Mon Sep 17 00:00:00 2001 From: Zack Rusin Date: Tue, 23 Aug 2011 21:17:35 -0400 Subject: [PATCH] Catch exceptions on Windows. --- os_win32.cpp | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/os_win32.cpp b/os_win32.cpp index 90ce8d3..4bfdf5e 100644 --- a/os_win32.cpp +++ b/os_win32.cpp @@ -134,12 +134,50 @@ Abort(void) #endif } + +struct Interrupts +{ + Interrupts() + : set(false), + prevfilter(NULL), + handler(NULL) + {} + + bool set; + LPTOP_LEVEL_EXCEPTION_FILTER prevFilter; + + void (*handler)(int); +}; +static Interrupts interrupts; + +LONG WINAPI InterruptHandler(EXCEPTION_POINTERS *exceptionInfo) +{ + if (interrupts.handler) { + int exceptionCode = 0; + if (exceptionInfo) { + exceptionCode = exceptionInfo->ExceptionRecord.ExceptionCode; + } + + interrupts.handler(exceptionCode); + } + + if (interrupts.prevFilter) { + return interrupts.prevFilter(exceptionInfo); + } else { + return EXCEPTION_CONTINUE_SEARCH; + } +} + void CatchInterrupts(void (*func)(int)) { - signal(SIGINT, func); - signal(SIGHUP, func); - signal(SIGTERM, func); + interrupts.handler = func; + + if (!interrupts.set) { + interrupts.prevFilter = + SetUnhandledExceptionFilter(InterruptHandler); + interrupts.set = true; + } } -- 2.43.0