marco4net
marco4net

Reputation: 175

How to do exception handling in mixed applications? (managed application using unmanaged DLL)

Is it possible to throw exceptions in an unmanaged DLL and handle it in a managed application?

My unmanaged C++ DLL throws exceptions in case of errors, and they should be handled in the calling executable application.

CMyFileException *x = new CMyFileException;
throw(x);

This previously worked, but now the application is compiled with different flags (/clr etc) cause we need to use some managed code. When the exception is thrown, I receive a System.ExecutionEngineException which does not seem to be caught even with catch(...) in my calling exe.

I have seen that compiler option /clr implies /EHa for the exception handling, but this seemed to be the option to select as far as I understand it now.

Would there be any other alternative (compiler/linker setting) to throw and catch exceptions over module (DLL/exe) boundaries?

Thanks for your support! Marco

Upvotes: 1

Views: 1612

Answers (2)

Hans Passant
Hans Passant

Reputation: 942518

There is something else going on. An ExecutionEngineException is thrown by the CLR when it discovers that the garbage collected heap is corrupted. That's not hard to do when you run unmanaged code in a managed program. A simple buffer overrun is enough. Finding the bug is however not easy.

Upvotes: 2

Benny
Benny

Reputation: 8815

it is possible to throw exception in unmanaged code, but the exception will eventually be caught by clr and wrapp it as SEH exceptions.

you can read this question:

Best practice for translating exceptions in C++/CLI wrapper class

Upvotes: 0

Related Questions