Microsoft Developer Network > 포럼 홈 > .NET Base Class Library > Catch Unhandled exceptions in unmanaged code
질문하기질문하기
 

답변됨Catch Unhandled exceptions in unmanaged code

  • 2009년 11월 4일 수요일 오전 3:23void Func 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    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!

답변

  • 2009년 11월 4일 수요일 오후 3:15Stephen Cleary 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    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

모든 응답

  • 2009년 11월 4일 수요일 오전 6:49Aldo John 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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.
  • 2009년 11월 4일 수요일 오후 1:49Stephen Cleary 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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
  • 2009년 11월 4일 수요일 오후 2:33void Func 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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!

  • 2009년 11월 4일 수요일 오후 2:44Stephen Cleary 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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
  • 2009년 11월 4일 수요일 오후 2:52void Func 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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!
     
  • 2009년 11월 4일 수요일 오후 3:15Stephen Cleary 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    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
  • 2009년 11월 4일 수요일 오후 4:43void Func 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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!
    • 편집됨void Func 2009년 11월 4일 수요일 오후 4:44typo
    •  
  • 2009년 11월 11일 수요일 오전 9:41eryangMSFT, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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.