.netcomcom-interoprcw

how to tell if a COM object has been separated from its underlying RCW without relying on exceptions?


A way to tell if the COM Object's reference count has reached 0 is by attempting to access one of its members and catch the resulting InvalidComObjectException, which is not very elegant and doesn't seem to lend itself well. Another way is to call Marshal.ReleaseComObject and check the result, but that requires that you decrease the Com Object's reference count by 1.

Is there a straightforward way to tell ?


Solution

  • On the .NET forums, this question has been posed before.

    Q: Are there any approaches to get the Internal Reference Count without decrementing it?

    A: There are no class library methods that can provide this service.

    Q: If it's impossible to get the Internal Reference Count without decrementing it, Is there any way to increment its count after calling Marshal.ReleaseComObject()?

    A: Again, there are no class library methods that provide this service.

    They do have an awkward way of achieving this as a workaround with global APIs exported from your COM server.
    Details here: http://social.msdn.microsoft.com/Forums/vstudio/en-US/9de651e0-ad3c-44be-8855-3dc13673ac73/how-can-i-get-the-internal-marshaling-count-of-an-rcw-systemcomobject?forum=clr

    Other than that, looks like you are stuck. Hope that helps you.