.NET Framework Developer Center > .NET Development Forums > .NET Base Class Library > Catch Unhandled exceptions in unmanaged code
Ask a questionAsk a question
 

AnswerCatch Unhandled exceptions in unmanaged code

  • Wednesday, November 04, 2009 3:23 AMvoid Func Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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!

Answers

  • Wednesday, November 04, 2009 3:15 PMStephen Cleary Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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

All Replies

  • Wednesday, November 04, 2009 6:49 AMAldo John Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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 1:49 PMStephen Cleary Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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 2:33 PMvoid Func Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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:44 PMStephen Cleary Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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:52 PMvoid Func Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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 3:15 PMStephen Cleary Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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
  • Wednesday, November 04, 2009 4:43 PMvoid Func Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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 byvoid Func Wednesday, November 04, 2009 4:44 PMtypo
    •  
  • Wednesday, November 11, 2009 9:41 AMeryangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.