**************************************************************************/
#include <windows.h>
+#include <assert.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
}
-struct Interrupts
-{
- Interrupts()
- : set(false),
- prevfilter(NULL),
- handler(NULL)
- {}
-
- bool set;
- LPTOP_LEVEL_EXCEPTION_FILTER prevFilter;
+static LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter = NULL;
- void (*handler)(int);
-};
-static Interrupts interrupts;
+static void (*handler)(int) = NULL;
-LONG WINAPI InterruptHandler(EXCEPTION_POINTERS *exceptionInfo)
+static LONG WINAPI UnhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
{
- if (interrupts.handler) {
+ if (handler) {
int exceptionCode = 0;
- if (exceptionInfo) {
- exceptionCode = exceptionInfo->ExceptionRecord.ExceptionCode;
+ if (pExceptionInfo) {
+ PEXCEPTION_RECORD pExceptionRecord = pExceptionInfo->ExceptionRecord;
+ if (pExceptionRecord) {
+ exceptionCode = pExceptionRecord->ExceptionCode;
+ }
}
- interrupts.handler(exceptionCode);
+ handler(exceptionCode);
}
- if (interrupts.prevFilter) {
- return interrupts.prevFilter(exceptionInfo);
+ if (prevExceptionFilter) {
+ return prevExceptionFilter(pExceptionInfo);
} else {
- return EXCEPTION_CONTINUE_SEARCH;
+ return EXCEPTION_CONTINUE_SEARCH;
}
}
void
CatchInterrupts(void (*func)(int))
{
- interrupts.handler = func;
+ assert(!handler);
+ assert(!prevExceptionFilter);
+
+ handler = func;
- if (!interrupts.set) {
- interrupts.prevFilter =
- SetUnhandledExceptionFilter(InterruptHandler);
- interrupts.set = true;
+ if (handler && !prevExceptionFilter) {
+ prevExceptionFilter =
+ SetUnhandledExceptionFilter(UnhandledExceptionFilter);
}
}