unit exceptionhandler; {$mode delphi} interface uses jwawindows, //I use jwawindows only for the purpose of the structure declaration of PVECTORED_EXCEPTION_HANDLER, I declare and assign AddVectoredExceptionHandler myself windows, Classes, SysUtils; var exceptrsp,exceptrbp, tryBegin, tryExcept: ptrUint; //globally accessible, set each time when a thread is about to raise an exception that needs to be handled implementation type TAddVectoredExceptionHandler=function(FirstHandler: ULONG; VectoredHandler: PVECTORED_EXCEPTION_HANDLER): PVOID; stdcall; type TRemoveVectoredExceptionHandler=function(VectoredHandlerHandle: PVOID): ULONG; stdcall; type TStackWalk64=function(MachineType: DWORD; hProcess: HANDLE; hThread: HANDLE; var StackFrame: STACKFRAME64; ContextRecord: PVOID; ReadMemoryRoutine: PREAD_PROCESS_MEMORY_ROUTINE64; FunctionTableAccessRoutine: PFUNCTION_TABLE_ACCESS_ROUTINE64; GetModuleBaseRoutine: PGET_MODULE_BASE_ROUTINE64; TranslateAddress: PTRANSLATE_ADDRESS_ROUTINE64): BOOL; stdcall; type TSymFunctionTableAccess64=function(hProcess: HANDLE; AddrBase: Int64): PVOID; stdcall; type TSymGetModuleBase64=function(hProcess: HANDLE; qwAddr: Int64): Int64; stdcall; type TSymInitialize=function(hProcess: HANDLE; UserSearchPath: PSTR; fInvadeProcess: BOOL): BOOL; stdcall; var k,d: THandle; veh: PVOID; AddVectoredExceptionHandler: TAddVectoredExceptionHandler; RemoveVectoredExceptionHandler: TRemoveVectoredExceptionHandler; StackWalk64: TStackWalk64; SymFunctionTableAccess64: TSymFunctionTableAccess64; SymGetModuleBase64:TSymGetModuleBase64; SymInitialize:TSymInitialize; function MyVectoredHandler(ExceptionInfo: PEXCEPTION_POINTERS): DWORD; stdcall; var sf: STACKFRAME64; tempcontext: CONTEXT; begin //check if the exception falls within the region of the try block (* // else begin //secondary check //do a stacktrace tempcontext:=ExceptionInfo.ContextRecord^; //copy over the details ZeroMemory(@sf,sizeof(sf)); sf.AddrPC.Offset:=tempcontext.{$ifdef cpu64}Rip{$else}eip{$endif}; sf.AddrPC.Mode:=AddrModeFlat; sf.AddrFrame.Offset:=tempcontext.{$ifdef cpu64}rbp{$else}ebp{$endif}; sf.AddrFrame.Mode:=AddrModeFlat; sf.AddrStack.Offset:=tempcontext.{$ifdef cpu64}rsp{$else}esp{$endif}; sf.AddrStack.Mode:=AddrModeFlat; while StackWalk64(IMAGE_FILE_MACHINE_AMD64, GetCurrentProcess, GetCurrentThread, sf, @tempcontext, nil, SymFunctionTableAccess64, SymGetModuleBase64, nil) do begin if (sf.AddrPC.Offset>=tryBegin) and (sf.AddrPC.Offsetnil then RemoveVectoredExceptionHandler(veh); FreeLibrary(k); //never forget to unload kernel32.dll, it might not be needed anymore.... yeah.... *) end.