Can I decode a C++ exception from a Windows SEH exception? (And if so, how?)

c++, exception, visual-c++, winapi

Solution

Why not use the C++ machinery that already gives you the exception details? It's not exclusive with SEH filters (although it is exclusive with `SetUnhandledExceptionFilter`). You just have to nest the handlers correctly:

int main()
{
    try {
        return cppexcept_main();
    }
    catch (const std::exception& e)
    {
        //use e.what()
    }
}

int cppexcept_main()
{
    __try {
        return application_main();
    }
    __except(GrabStackTrace(GetExceptionInformation()), EXCEPTION_CONTINUE_SEARCH) {
         /* never reached due to EXCEPTION_CONTINUE_SEARCH */
    }
}

Problem

In the event of an unhandled C++ exception I want to print: - The message (`what()`) of the C++ exception - A stack trace. In order to get the stack trace, I'm using `SetUnhandledExceptionFilter` in combination with the StackWalker library: ``` struct FooStackWalker : StackWalker { virtual void OnCallstackEntry(CallstackEntryType, CallstackEntry &entry) override { std::cerr << entry.lineFileName << " (" << entry.lineNumber << "): " << entry.undFullName << std::endl; } }; LONG WINAPI UnhandledExceptionHandler(LPEXCEPTION_POINTERS pointers) { FooStackWalker walker; walker.ShowCallstack(::GetCurrentThread(), pointers->ContextRecord); ::TerminateProcess(::GetCurrentProcess(), 1); } int main() { ::SetUnhandledExceptionFilter(UnhandledExceptionHandler); } ``` I've gotten the stack trace to print just fine, but now getting `what` is difficult. Is there some way I can decode the SEH exception as a C++ exception in order to call this member function before termination?

Original source