From: Zack Rusin <zack@kde.org>
Date: Wed, 24 Aug 2011 01:17:35 +0000 (-0400)
Subject: Catch exceptions on Windows.
X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=1e0fc67981e101495c2e236efb37cb5db83af400;p=apitrace

Catch exceptions on Windows.
---

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