1

I want to inject a DLL into a process. Once this DLL is in there, it should catch & properly handle all access violation exceptions which occur in the process. Is there any way to accomplish this?

  • 7
    How do you "properly handle" access violations from an arbitrary process? – R. Martinho Fernandes Sep 02 '11 at 19:14
  • Some compilers allow you to use catch(...) as a catch-all. – Chase Henslee Sep 02 '11 at 19:15
  • @Chase that doesn't handle access violations. This might help: http://stackoverflow.com/questions/457577/catching-access-violation-exceptions/918891#918891 – Seth Carnegie Sep 02 '11 at 19:15
  • That's not a dupe- that was non-Microsoft, whereas this is going to be Windows-specific. – Puppy Sep 02 '11 at 19:16
  • @Seth True, it also doesn't handle signals, which if you're trying to make a catch-all type DLL, you'd probably want to consider "catching". – Chase Henslee Sep 02 '11 at 19:17
  • @R. Martinho Fernandes not necessarily properly handle, but if for example another injected DLL tries to write to address 0x7FFFFFFF and causes an access violation, I want to stop that. –  Sep 02 '11 at 23:51

4 Answers4

4

How about SetUnhandledExceptionFilter( function )?

function's prototype is:

LONG __stdcall ExceptionHandler(EXCEPTION_POINTERS *ExceptionInfo);

I've used this function to create crash dumps, etc.

Marc Bernier
  • 2,928
  • 27
  • 45
1

You can use Structured Exception Handling (SEH) to catch such exceptions. Specifically, this Windows function seems to be what you want to do.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 1
    My understanding is SEH catches exceptions in the thread in which the SEH registration happened. When the DLL main runs it can certainly register a handler for access violations (for the thread it was loaded in), but how do you propose it also registers handlers for arbitrary other threads in the process? I don't think it's possible. – Kevin Sep 02 '11 at 19:33
0

To complete the collection, you can also use AddVectoredExceptionHandler.

pezcode
  • 5,490
  • 2
  • 24
  • 37
0

Pre XP, you cannot catch all exceptions. XP or later, you should use AddVectoredExceptionHandler(1, handler), although you are not guaranteed that you will always be the first vectored exception handler.

MSN
  • 53,214
  • 7
  • 75
  • 105