Skip to content

Instantly share code, notes, and snippets.

@mmozeiko
Last active December 18, 2016 19:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmozeiko/91a698249fdb9ba1c69dd1e0023552ce to your computer and use it in GitHub Desktop.
Save mmozeiko/91a698249fdb9ba1c69dd1e0023552ce to your computer and use it in GitHub Desktop.
exception emulation with vectored exception handler & restoring CPU context
#include <windows.h>
#include <stdio.h>
static bool InsideTry;
static CONTEXT ResumeContext;
static LONG WINAPI Handler(struct _EXCEPTION_POINTERS* ExceptionInfo)
{
printf("inside vectored ex handler\n");
InsideTry = false;
RtlRestoreContext(&ResumeContext, NULL);
}
void badstuff()
{
volatile int* a = 0;
*a = 1;
volatile int x = 0;
x = 1 / x;
volatile char* p = 0;
volatile char z = *p;
}
int main()
{
printf("beginning of main\n");
PVOID h;
InsideTry = true;
RtlCaptureContext(&ResumeContext);
if (InsideTry)
{
// this is "try" part
printf("setting vectored ex handler\n");
h = AddVectoredExceptionHandler(1, &Handler);
printf("calling bad stuff\n");
badstuff();
printf("after bad stuff\n"); // never gets printed
}
else
{
// this is "except" part
}
printf("removing vectored ex handler\n");
RemoveVectoredExceptionHandler(h); // cleanup
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment