1

My C# code is being run through COM from unmanaged code (which happens to be VB6). Mainly for debugging & logging purposes I would like to catch any C# exceptions before they make their way back to the unmanaged code.

In comparison, for other "subsystems" like Winforms and WPF it is possible to do something like this - both of those offer an event that you can use to handle exceptions as you see fit. I guess in those instances your application code is being executed from those subsystems as a starting point, so that provides the 'hook' where the exceptions can be caught and corresponding events raised.

I'm looking for something similar (if it exists) from the COM interop "subsystem".

This answer mentions:

The CLR installs a catch-all exception handler in its interop layer

Does the mentioned "interop layer" makes accessible any event like this? Or some other mechanism to act on an exception before it heads to the unmanaged code?

Note - I don't see anything for COM mentioned in this very well regarded answer, so maybe that indicates this is not possible.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
  • If you just want to get notified of exceptions, you can use AppDomain.UnhandledException in any .NET code but this won't stop them, COM or not COM, winforms, WPF or not. The .NET code that translate .NET exceptions into native exceptions for COM interop is not programmable – Simon Mourier Jan 22 '21 at 17:52
  • https://learn.microsoft.com/en-us/dotnet/api/system.appdomain.firstchanceexception?view=net-5.0 – Hans Passant Jan 29 '21 at 10:11
  • @SimonMourier that seems to be the answer (that there is no programmable interface). You should consider adding it as an actual answer; more likely to help someone else in the future that way. – StayOnTarget Apr 13 '21 at 16:55

0 Answers0