I have a .NET assembly which is invoked via COM Interop from a Delphi Host. I understand that any unhandled exception in the .NET code will be handled by the .net com interop framework and a correspoding HRESULT will be returned to the the COM host (delphi). Stack information is however lost during this Exception to HRESULT conversion.
The problem is that we would like to log all unhandled exceptions in .net code and I have not found any way to register a catch-all handler that is actually invoked whenever such an exception occurs.
I have tried:
- AppDomain.CurrentDomain.UnhandledException
- Dispatcher.CurrentDispatcher.UnhandledException
but these are not invoked when running under COM Interop.
Is it possible to define some sort of handler/callback for unhandled exceptions for the COM Interop Framework? Or somehow hook into the process of converting a Managed Exception to an HRESULT so that the exception can be logged?
Thanks!
try { .. } catch (Exception ex) { /* save ex*/; throw; }
everywhere in your C# methods exposed to COM. Alternatively, you could handleAppDomain.CurrentDomain.FirstChanceException
and store the last 1st-chance exception, which you could also expose to COM.