none
Catch Unhandled exceptions in unmanaged code

    Question

  • Hi,

    I have an application which has both managed and unmanged code. The executable is written in managed c#. It uses a set of win32 dlls(unmanaged). There are multiple threads running in these win32 dlls.

    The executable (managed C# application) has code to handle unhandled exceptions. This code portion logs the details of the unhnadled exception and exits the application. If an unhandled exception is thrown in a win32 dll, the code for handling the unhandled exception in the main application (executable) does not handle it. This prevents us from identifying the module which has the issue.

    Are there any ways to catch unhandled exceptions thrown in the win32 dlls, in the managed C# application?

    Please help me.

    Thanks!

    Wednesday, November 04, 2009 3:23 AM

Answers

  • When a thread throws an exception, only handlers in that thread's call stack are considered.

    The reason it works for managed threads is because the CLR sticks a handler at the top level, which will invoke the unhandled exception handler. Unmanaged threads do not have this top-level handler, so they don't forward their exceptions to the unhandled exception handler.

           -Steve
    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ

    Microsoft Certified Professional Developer
    • Edited by Stephen ClearyMVP Wednesday, November 04, 2009 3:16 PM s/is/are
    • Marked as answer by eryang Wednesday, November 11, 2009 10:21 AM
    Wednesday, November 04, 2009 3:15 PM

All replies

  • Im not sure, but try this...

    In VS, go to Debug -> Exceptions.

    There you can see which all exceptions are thrown and not, put check mark in 'throw' for the Win32 Exceptions item.
    Wednesday, November 04, 2009 6:49 AM
  • You should be able to do a simple try/catch.

    Unhandled unmanaged exceptions are translated to OutOfMemoryException, AccessViolationException, NullReferenceException (rarely), or SEHException. This exception translation is done at the p/Invoke border.

    This is assuming that the unmanaged code uses structured exception handling (SEH) for its exceptions. MS and Borland do, but gcc does not. gcc has its own exception handling mechanism that is more portable but completely incompatible with .NET.

           -Steve
    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ

    Microsoft Certified Professional Developer
    Wednesday, November 04, 2009 1:49 PM
  • You should be able to do a simple try/catch.

    Unhandled unmanaged exceptions are translated to OutOfMemoryException, AccessViolationException, NullReferenceException (rarely), or SEHException. This exception translation is done at the p/Invoke border.



    Actually the exception was thrown from a thread running in the Win32 dll. It was not thrown from a method in the unmanaged dll, which was invoked from the managed code.

    Please help.
    Thanks!

    Wednesday, November 04, 2009 2:33 PM
  • There isn't an easy way to do this.

    You may be able to hijack thread creation, or spin off another process that debugs your process.

    Either way is quite difficult, though.

             -Steve
    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ

    Microsoft Certified Professional Developer
    Wednesday, November 04, 2009 2:44 PM
  • There isn't an easy way to do this.

    You may be able to hijack thread creation, or spin off another process that debugs your process.

    Either way is quite difficult, though.



    An unhandled exception handler is registered in the managed application. Why is it not handling all unhandled exceptions thrown from both unmanaged and managed code (Presently it handles only the unhandled exception thrown from the managed code)?

    Please help.
    Thanks!
     
    Wednesday, November 04, 2009 2:52 PM
  • When a thread throws an exception, only handlers in that thread's call stack are considered.

    The reason it works for managed threads is because the CLR sticks a handler at the top level, which will invoke the unhandled exception handler. Unmanaged threads do not have this top-level handler, so they don't forward their exceptions to the unhandled exception handler.

           -Steve
    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ

    Microsoft Certified Professional Developer
    • Edited by Stephen ClearyMVP Wednesday, November 04, 2009 3:16 PM s/is/are
    • Marked as answer by eryang Wednesday, November 11, 2009 10:21 AM
    Wednesday, November 04, 2009 3:15 PM
  • When a thread throws an exception, only handlers in that thread's call stack are considered.

    The reason it works for managed threads is because the CLR sticks a handler at the top level, which will invoke the unhandled exception handler. Unmanaged threads do not have this top-level handler, so they don't forward their exceptions to the unhandled exception handler.

    Thanks..

    I have one more question. Will it help if I invoke SetUnhandledExceptionFilter indirectly by invoking a method in win32 dll from the managed code? Will the generic unhandled exception handler in the managed code catch unhandled exceptions in managed as well as unmanaged code if I do so?

    Please help.

    Thanks!
    • Edited by void Func Wednesday, November 04, 2009 4:44 PM typo
    Wednesday, November 04, 2009 4:43 PM
  • Hi,
    Glad to see that you have got an answer for the first question, and we suggest you to post only one question per thread, so, do you mind ask your second question in a new thread? thanks,

    Thanks,
    Eric
    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    Wednesday, November 11, 2009 9:41 AM
  • Hi,

    isn't SetUnhandledExceptionFilter() what he is looking for?

    Günter
    • Proposed as answer by Gubo Wednesday, December 16, 2009 6:39 PM
    Wednesday, December 16, 2009 3:57 PM