Click here to Skip to main content
15,884,078 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a native c++ class in a dll whose functions are called by using a managed c++ class.But when an exception occurs in native dll, the application crashes.

I want to know that how can I catch exceptions from native dll in my managed c++ class???

Can anyone help me with an example.

Thanks in advance.
Posted
Comments
Philippe Mori 24-Mar-15 9:03am    
In C++/cli, you can catch both type of exception assuming that the native DLL is compiled with compatible options and managed code is in mixed mode. Traditionnally, you don't let exception cross DLL boundaries particulary in the DLL is used from a variety of compiler. This is why COM (Active/X) uses HRESULT.
Sergey Alexandrovich Kryukov 24-Mar-15 9:08am    
Exactly.

As to "traditionally", who established that tradition? HRESULT is the lame of COM, not related to managed vs unmanaged traditions.

The problems appear when you throws something non-primitive, such as the instance of your own unmanaged class. The exception will be caught but not very informative. It's better to catch such exceptions in unmanaged part and allow the rest to propagate up the stack. Blocking propagation in the middle of stack is almost always bad, should be used on in special ugly cases...

—SA
Philippe Mori 24-Mar-15 9:35am    
In managed case, it is well defined how (managed) exception works and thus you can throw managed exceptions from whatever compiler/language you use that support .NET and catch in any other language/compiler that support .NET.

In native case, there are no such convention so the crash might be caused by incompatible options, incompatible C++ run/time or incorrect C++ code that crash while unwinding the stack.

The first thing to do would be to test simple cases like calling a function in the DLL that do nothing but throwing an exception (what type of exception are you throwing? std? MFC?...)
Sergey Alexandrovich Kryukov 24-Mar-15 10:26am    
Right. So I tried to explained the basic ideas in Solution 1 and tried to present some more or less representative code sample, please see.
—SA
Philippe Mori 24-Mar-15 11:23am    
Do you want general advices or you have specific cases that you are not able to properly handle?

What have you tried so far?

First of all, it's most likely that you don't need to catch exception write around every call to something unmanaged. You can catch part of the exceptions only at the point where you know how to handle some specific exceptions. I call such points "competency points". All exceptions should be caught only somewhere on the top stack frame of each stack and in some special points, such as inside the main message/event loop of the UI application.

Having this in mind… the simplest case is so simple:
C++
try {
   someCodeInvolvingUnmanagedCode();
} catch (FirstSpecificExceptionType ex) {
   HandleFirstSpecificExceptionType(ex);
} catch (SecondSpecificExceptionType ex) {
   HandleSecondSpecificExceptionType(ex);
}
// allow other exceptions to propagate
// up the stack
The unmanaged exception will be caught, too, it… you catch then or catch all exceptions (catch(...)). I usually recommend to catch all uncaught exceptions up the stack, as I mentioned above.

Now, please see my comment to the comment by Philippe Mori to the question. The problem is: in managed code, all exceptions will be caught in the form of some exception type derived from the common base class, System::Exception. In unmanaged code, it could be… anything. It's good it this is a system exception (integer division by zero, for example), it will be properly wrapped and delivered. What if the exception is the instance of some unmanaged C++ class? It will be caught, but you would not be able to access specific exception information. But you can catch them, too!

This is the power of mixed mode (managed + unmanaged) C++/CLI projects: you can combine managed and unmanaged code. In such cases, if you have to use some unmanaged exception classes defined somewhere, I would wrap them in some managed exception classes you could derive from System::Exception. How would someCodeInvolvingUnmanagedCode look like? It could be:
C++
void someCodeInvolvingUnmanagedCode() {
   //...
   try {
      someUnmanagedCode();
   } catch (FirstUnmanagedExceptionType ex) {
      throw gcnew FirstSemanticManagedExceptionWrapper(ex);
   } catch (SecondUnmanagedExceptionType ex) {
      throw gcnew SecondSemanticManagedExceptionWrapper(ex);
   } catch (std::exception) { // for example
      throw gcnew StdExceptionWrapper(ex);
   }
} // all exception will propagate up the stack


This way, you could have the point where you catch all exceptions as managed types derived from the System::Exception, even in one catch statement:
C++
try {
   // ...
} catch (System::Exception^ ex) {
   someCommonHandler(ex);
}


But of course you can catch it in mixed way:
C++
try {
    someMixOfManagedUnmanagedCode();
}
catch (std::exception& ex) {
   // do something
}
catch (System::Exception^ ex) {
   // do something else
}
// then exceptions not covered by these two base types
// (only unmanaged ones, if any) will propagate
// up the stack
And so on…

—SA
 
Share this answer
 
v2
Comments
Philippe Mori 24-Mar-15 11:19am    
If the managed code is a wrapper so that the code is easier to use from C#, then wrapping exceptions this way is probably the best solution...

If there are specific problems, then I have provided a few links that might help.
Sergey Alexandrovich Kryukov 24-Mar-15 12:50pm    
Thank you, Philippe. The particular is a matter of general code design; I just gave some basic ideas.

Main principles are: isolate exception handling from "regular" code, minimize handling, handle exceptions only in very few points of code, "let go" in most cases, handle only at the "competency points", handle them all on the very top of the stack of each threads. Managed + unmanaged situations complicates only a little bit.

—SA
In addition to Solution 1, using Google might help understand how it works.

There are a few compiler options related to exceptions and the behavior of some cases can be modified by code and finally there is a wide variety of way that programmer uses exceptions so it is not easy to give a specific answer.

Here are a few links :

https://www.google.ca/?gws_rd=ssl#q=catch+c%2B%2B+exception+from+managed+code[^]

http://stackoverflow.com/questions/150544/can-you-catch-a-native-exception-in-c-sharp-code[^]

http://stackoverflow.com/questions/6628412/c-cli-catching-all-net-win32-crt-exceptions[^]

https://msdn.microsoft.com/en-us/library/df24ysb6.aspx[^]

http://blogs.msdn.com/b/liviuc/archive/2009/10/13/.net-interop-and-exception-handling-in-mixed-applications.aspx[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Mar-15 12:47pm    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900