1
votes

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

2

2 Answers

2
votes

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.

0
votes

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