none
Trap native thread exceptions RRS feed

  • Question


  • What technique is used to catch unhandled native thread exceptions called from managed code?  Our managed C# app pinvokes and starts threads in native code.
    Sometimes these native threads crash; we want a method to catch all unhandled exceptions (native and managed).

    I tried pinvoking the SetUnhandledExceptionFilter to catch native exceptions but it only works for managed exceptions.
    Does the pinvoke marshaller install its own exception handler which supersedes my call to SetUnhandledExceptionFilter?  What options do I have?  
    Wednesday, June 11, 2008 1:49 PM

Answers

  • The P/Invoke marshaler will install an SEH handler for the duration of the call to catch any unmanaged exceptions and translate them to System.Exception derived exceptions like AccessViolationException.  However, this handler can only catch exceptions thrown on the current thread.  If your unmanaged code starts threads by itself, it is responsible for registering its own handler.  You do this in C++ with the __try/__except keywords.  If you want your managed code to report those exceptions, you'd have to establish a managed callback delegate that the __except handler can call to pass the exception details.  You'll have to translate the EXCEPTION_POINTERS info yourself.
    Hans Passant.
    • Marked as answer by kweel Wednesday, June 11, 2008 7:28 PM
    Wednesday, June 11, 2008 4:13 PM
    Moderator