Question

In COM-based Windows Explorer shell extensions error conditions are expressed using HRESULT return values returned from COM interface methods.

But if inside the body of a COM interface method (or inside a method of some C++ object called directly or indirectly in a COM method) a C++ exception (e.g. std::runtime_error, std::bad_alloc, std::invalid_argument, etc.) is thrown , what does exactly happen?

Is this just an undefined behavior?

Or is the C++ exception somehow converted to a Win32 Structured Exception (SEH) ?
If so, what is the mechanics of the conversion process?

Was it helpful?

Solution

Allowing exceptions to cross a COM boundary is verboten and behavior is unspecified. What happens depends on the C++ compiler you use. If you use MSVC++ then, yes, C++ exceptions piggy-back on top of SEH. MinGW is popular for GCC, it doesn't.

Which makes it technically possible for a client program to catch the exception. The .NET CLR does so for example, turning the unmanaged exception into a managed SEHException. This is however purely for diagnostic reasons, allowing the program to shut down in a controlled manner with a decent error message and stack trace. Actually catching the exception is very unlikely to come to a good end. You by definition cannot handle the exception, you have no hope of restoring the state of the COM server. The exception of course bypassed code in the COM server that is normally expected to execute. A memory leak would be a typical result. A lock that didn't get released is a good way to hang the program undiagnosably. Repeated exceptions are likely if you just keep trying to use the server. Only program shutdown is the reasonable approach.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top