Advertisement
RandomClear

Low-level exception filter

Sep 17th, 2015
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.70 KB | None | 0 0
  1. // The following example will disable EurekaLog for all exceptions raised in ntdll, kernel32 and kernelbase DLLs.
  2.  
  3. uses
  4.   EInject;
  5.  
  6. var
  7.   EurekaLogExceptionDispatcher: TExceptionDispatcherProc;
  8.  
  9. function IgnoreException(const AContext: Windows.TContext; const AExceptionRecord: Windows.TExceptionRecord): Cardinal; stdcall;
  10. const
  11.   EXCEPTION_CONTINUE_SEARCH  = 0;
  12.   cDelphiException           = $0EEDFADE; // see System.pas
  13. var
  14.   ExceptionAddress: Pointer;
  15.   ExceptionModule: HMODULE;
  16.   ExceptionModuleFileName: String;
  17.   ExceptionModuleName: String;
  18.  
  19.   function IgnoreThisException: Boolean;
  20.   begin
  21.     Result := SameFileName(ExceptionModuleName, 'ntdll.dll') or
  22.               SameFileName(ExceptionModuleName, 'kernel32.dll') or
  23.               SameFileName(ExceptionModuleName, 'kernelbase.dll');
  24.   end;
  25.  
  26. begin
  27.   if AExceptionRecord.ExceptionCode = cDelphiException then
  28.     ExceptionAddress      := Pointer(AExceptionRecord.ExceptionInformation[0]) // see System.TExceptionRecord
  29.     // Exception object is available in TObject(AExceptionRecord.ExceptionInformation[1])
  30.   else
  31.     ExceptionAddress      := AExceptionRecord.ExceptionAddress;
  32.   ExceptionModule         := FindHInstance(ExceptionAddress);
  33.   ExceptionModuleFileName := GetModuleName(ExceptionModule);
  34.   ExceptionModuleName     := ExtractFileName(ExceptionModuleFileName);
  35.  
  36.   if IgnoreThisException then
  37.     Result := EXCEPTION_CONTINUE_SEARCH // RTL Handler will be called
  38.   else
  39.     Result := EurekaLogExceptionDispatcher(AContext, AExceptionRecord); // EurekaLog will be called
  40. end;
  41.  
  42. initialization
  43.   EurekaLogExceptionDispatcher := EInject.EventExceptionDispatcher;
  44.   EInject.EventExceptionDispatcher := IgnoreException;
  45. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement