views:

150

answers:

3

Is it possible for a process to catch an unhandled exception of another process on the system?

If possible, under which circumstances is it possible? Is it for instance possible if the second process is not started by the first?

I am mainly looking for an answer regarding native c++.

+4  A: 

Native (AKA standard) C++ has no real concept of multiple processes, and has no means of catching exceptions thrown across process boundaries. And no means of throwing exceptions across such boundaries, come to that.

anon
+4  A: 

Windows Exceptions: Structured Exception Handling (SEH) is per thread. Another thread in the process might be able to manipulate the stack of the target thread to insert its own handler, but that is going to be hard to get right (especially with the lack of consistent calling convention on x86). Another process could inject a dll & thread into a process to do this. This will be hard to get right, especially without close coupling to the details of the target process (what functions are called and how).

On second thoughts debuggers can do this, so the Win32 debugger APIs must have this capability. A process can debug other processes in the same session (with lower or equal integrity level), or if the user has the "debug process" privileged any process.

Richard
+1 for mentioning that being a debugger might help.
OregonGhost
but in case of debugger, maybe the debugged code somehow was changed due to injection of debug code generated by compiler to communicate with debugger
uray
@uray: no, thankfully that doesn't happen. It would be really hard to debug problems that suddenly disappear when you attach a debugger. Although it does change timing, making debugging multi-threading races difficult.
Hans Passant
+1  A: 

Yes. Matt Pietrek explains how. Scroll down to the "VectoredExceptionHandling is a clean, easily extensible way to see all exceptions" part. There's example code as well.

MSalters
Sounds good, rarely works in practice. You catch exceptions that you shouldn't catch. Like C++ exceptions that have a catch handler. Vista's AddVectoredContinueHandler works better, but is still poison if the program executes any managed code. As long as you have to inject a DLL anyway, SetUnhandledExceptionFilter() is the better mousetrap.
Hans Passant
Just answering the question _if and how_ it's possible. A "continue handler" doesn't catch them. I'd agree with your sentiment that you probably _shouldn't_ catch them.
MSalters