3

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!

3
  • I think you best bet would be to use something like try { .. } catch (Exception ex) { /* save ex*/; throw; } everywhere in your C# methods exposed to COM. Alternatively, you could handle AppDomain.CurrentDomain.FirstChanceException and store the last 1st-chance exception, which you could also expose to COM.
    – noseratio
    Commented Mar 3, 2015 at 0:54
  • 1
    @Noseratio your suggestion works. I have found no better approach. You should add it as an answer so I can accept it.
    – santiagoIT
    Commented Mar 3, 2015 at 15:17
  • 1
    @Noseratio unfortunately the better approach you propose does not work in my situation. This is however due to a flaw in Delphi. See comments to your answer. I ended up defining the handler for the FirstChanceExceptions and storing the last one.
    – santiagoIT
    Commented Mar 4, 2015 at 23:06

1 Answer 1

2

I think the best option here would be to use something like

void ComClassMethod()
{
    try { ... } 
    catch (Exception ex) 
    { 
        /* store the ex in the object */; 
        throw; 
    } 
}

everywhere in your C# methods exposed to COM.

Alternatively, you could handle AppDomain.CurrentDomain.FirstChanceException and store the last 1st-chance exception, which you could also expose to COM.

However, pay special attention if your COM object support muliti-hreading (note, most .NET COM objects are free-threaded). In this case, you'd need to maintain a map between threads and their corresponding most-recent exceptions, either per object or globally via System.Threading.ThreadLocal<T> (decide what suits your logging requirements better).


Updated, I've been thinking, there has to be a better way of doing this. And there is one, indeed, as explained here in Adam Nathan's excellent ".NET and COM: The Complete Interoperability Guide" book. I'm not sure I can copy/paste an exact quote from the copyrighted material, but the idea is the following:

  • All .NET CCW objects implement ISupportErrorInfo.
  • The extended error information - via an IErrorInfo object - can be retrieved on COM side via GetErrorInfo API.
  • According to Adam Nathan, this object also is the .NET exception object itself, and it implements the _Exception COM interface.
  • The COM client can use _Exception to get all the details, including _Exception.StackTrace.

This seems to be exactly what you're looking for.

3
  • when using the SafeCall convention, Delphi will automatically call GetErrorInfo and raise a delphi Exception of type EOleException which is populated with the values from the retrieved IErrorInfo object. Unfortunately the EOleException does not hold any reference to the IErrorInfo object. If I understand the GetErrorInfo doc. correctly: " It transfers ownership of the error object to the caller, and clears the error state for the thread. " this means the ErrorInfo object is no longer available when calling GetErrorInfo after Delphi has done its 'magic'.
    – santiagoIT
    Commented Mar 4, 2015 at 22:47
  • Ran out of space on the previous comment. When I subsequently call, GetErrorInfo I can no longer retrieve the IErrorInfo object. It is lost forever....
    – santiagoIT
    Commented Mar 4, 2015 at 22:49
  • @santiagoIT, I see. My Delphi knowledges are quite limited, but I remember it's possible to use raw COM interfaces generated from the typelib, without smart wrappers. I understand this might not be an option for you.
    – noseratio
    Commented Mar 4, 2015 at 23:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.