1

I have a .NET program that uses pinvoke to run a traditional Win32 message loop and display a UI. This program also exposes COM interfaces for UI Automation accessibility.

How do I know when all the references to these COM interfaces have been released so that I can safely terminate the message loop and shutdown the app?

Normally a out-of-proc COM server would maintain a global object reference count and server lock count. Presumably this is done by the CLR somewhere and WinForms and WPF's Application.Run methods track this however I haven't been able to find any documentation for how to implement this myself.

Brad Robinson
  • 44,114
  • 19
  • 59
  • 88
  • Checkout `Marshal.ReleaseComObject` and have a little look into [some of details on how this all works under the hood](http://www.codeproject.com/Articles/990/Understanding-Classic-COM-Interoperability-With-NE), although simple explanation and example can be found [here](http://stackoverflow.com/questions/4591681/rcw-reference-counting-when-using-com-interop-in-c-sharp). – Jeremy Thompson Sep 15 '14 at 04:51
  • @JeremyThompson, those links are all talking about reference counts on COM objects from .NET. This situation is the other way around - how can I tell when no out of proc clients are referencing my .NET objects exposed via COM. – Brad Robinson Sep 15 '14 at 06:41
  • This is a core reason why .NET supports out-of-process servers so poorly. I *think* you have to start with ServicedComponent, override the Deactivate() method. It was however intended for COM+ hosting, not sure how much luck you'll have with it in your scenario. It is always painful anyway when you have a UI, it gets too murky who is in charge and having to keep running without a UI is a bug factory. Consider an explicit Quit() method to punt the problem. – Hans Passant Sep 15 '14 at 09:08
  • @HansPassant it's kind of hard to separate UI when what I'm implementing is UI Automation (ie: accessibility). Not sure what you mean by Quit() method - who would implement it, who would call it and what would it do? – Brad Robinson Sep 15 '14 at 13:31
  • 2
    Accessibility is a very different ball game from Automation. Not sure anymore what you are talking about. – Hans Passant Sep 15 '14 at 13:36
  • @HansPassant "Microsoft UI Automation is the new accessibility framework for Microsoft Windows" quoted from here: http://msdn.microsoft.com/en-us/library/ms747327(v=vs.110).aspx – Brad Robinson Sep 15 '14 at 13:53

0 Answers0