/*
simple Vectored Exception Handler example

useful as a starting point for an in-process debugger, perhaps.

www.bitpatch.com
*/


#include <windows.h>

LONG WINAPI VEHandler( _EXCEPTION_POINTERS *ei )
{
    if( 1 )// todo: if this is our breakpoint
    {
        ei->ContextRecord->Edx = 0xDDDDDDDD;
        ei->ContextRecord->Eax = 0x00001337;
        ei->ContextRecord->Eip++; // skip ahead one-byte ( jump over the 0xCC [int 3] op-code )
        return EXCEPTION_CONTINUE_EXECUTION;
    }
    else
    {
        return EXCEPTION_CONTINUE_SEARCH;
    }
}

int WINAPI WinMain( HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nCmdShow )
{
    volatile int v;

    PVOID h = AddVectoredExceptionHandler( 1, VEHandler );
    if( h == NULL ) MessageBoxA( 0,"error","AddVectoredExceptionHandler() failed",MB_OK);
    __asm{
        xor eax,eax
        mov v, eax
        int 3 // trigger exception... make sure your debugger isn't catching this!
        mov v,eax
    }
    RemoveVectoredExceptionHandler( h );
    if( v == 0x1337 ) MessageBoxA( 0,"success","success",MB_OK);
    return v;
}