none
Handle COM library Crash inside C# library RRS feed

  • Question

  • I am calling a C library method in my .NET library as below .

    [DllImport(@"abc.dll",CallingConvention=CallingConvention.Cdecl)]
    private static extern unsafe int DoSomething(

    Calling above method using DoSomething()

    Both of these gets loaded into to w3wp since they are part of the web project. Due to some exception inside C library, whole worker process is getting crashed.

    Is there anyway to handle this inside the C# class where I call COM method?

    Wednesday, February 21, 2018 10:38 AM

All replies

  • Can you be more specific about what the "some exception" is? Does it encounter an exception or does it throw an exception? Is the DLL actually a C++ program that exposes the function as a C function? That is extremely common.

    See Exception Handling in Visual C++ and the subsections in there.

    See Managed C++ Wrapper For Unmanaged Code. That is one possibility; you could write a C++ Class Library that calls the C DLL and handle the exception in C++.



    Sam Hobbs
    SimpleSamples.Info

    Wednesday, February 21, 2018 11:03 AM
  • I am calling a C library method in my .NET library as below .

    [DllImport(@"abc.dll",CallingConvention=CallingConvention.Cdecl)]
    private static extern unsafe int DoSomething(

    Calling above method using DoSomething()

    Both of these gets loaded into to w3wp since they are part of the web project. Due to some exception inside C library, whole worker process is getting crashed.

    Is there anyway to handle this inside the C# class where I call COM method?

    Catching some unspecified exception so that a worker process does not "crash" is a half-measure and does not solve the root problem.  A better path to follow is to debug into the DoSomething function exported from abc.dll, find and fix the problem.

    And its unclear how this has anything to do with COM.

    Wednesday, February 21, 2018 11:30 AM
  • Hi Ramaprasad Upadhyaya,

    Thank you for posting here.

    For your question, could you provide the DoSomething Method in your abc.dll? I will check the way you used to call C++ dll in C# application.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 21, 2018 3:37 PM
    Moderator
  • There is unhandled exception is happening inside the dll. I dont have any control/source code of this dll
    Wednesday, February 21, 2018 4:05 PM
  • There is unhandled exception is happening inside the dll. I dont have any control/source code of this dll
    Wednesday, February 21, 2018 4:05 PM
  • If that is unhandled C++ DLL, I don't think there is much you can do because SEH in C++ is completely different and irrelevant model of error handling to .NET.

    The best you can do is to call the function in another thread, and then catch the ThreadAbortException if it cause your thread to terminate.

    Thursday, February 22, 2018 1:32 AM
    Answerer
  • All other things being equal, even if you catch the exception from the unmanaged dll or isolate the problem to a particular thread the system is still in an unstable state.  You don't know what the problem is or whether it is safe to continue.  If you choose to continue the exception could have caused problems that will manifest as hard to debug issues later during execution.

    Assuming that the OP is not erring in some way that causes the exception then it seems to me the reasonable path to take is to work with the author of the dll to find and resolve the cause of the exception.  If this isn't possible then using this problematic dll in a production environment is a questionable decision.  Of course, that's just my opinion.


    • Edited by RLWA32 Thursday, February 22, 2018 1:57 AM
    Thursday, February 22, 2018 1:56 AM
  • Hi Ramaprasad Upadhyaya,

    Thank you for feedback.

    If the crash comes from C++ dll library and you do not have control of this dll, we could not handle this exception in C#.

    If you want to call C++ dll from C#, the method in C++ dll which you want to call must have its corresponding format. Here is a thread which shows how to invoke C++ dll from C# for your reference.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/dbafec35-0759-4dde-9048-d1c0c80a2ffb/use-managed-c-class-library-with-c?forum=netfxbcl

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, February 22, 2018 6:13 AM
    Moderator