]> git.cworth.org Git - apitrace/blobdiff - os_win32.cpp
Merge branch 'master' into compression
[apitrace] / os_win32.cpp
index f220ae5ec46dbbcd7368ca0204a36a669d26c835..4bfdf5e00d96373c1a7e003f42c2dfd521ef935c 100644 (file)
@@ -24,6 +24,7 @@
  **************************************************************************/
 
 #include <windows.h>
+#include <signal.h>
 #include <string.h>
 #include <stdio.h>
 
@@ -133,4 +134,51 @@ 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))
+{
+    interrupts.handler = func;
+
+    if (!interrupts.set) {
+        interrupts.prevFilter =
+            SetUnhandledExceptionFilter(InterruptHandler);
+        interrupts.set = true;
+    }
+}
+
+
 } /* namespace OS */