title: Is it possible to intercept (or be aware of) COM Reference counting on CLR objects exposed to COM
body: <p><em>I have rephrased this question.</em></p>
  
  <p>When .net objects are exposed to COM Clients through COM iterop, a CCW (<a href="http://msdn.microsoft.com/en-us/library/f07c8z1c(VS.71).aspx" rel="nofollow noreferrer">COM Callable Wrapper</a>) is created, this sits between the COM Client and the Managed .net object.</p>
  
  <p>In the COM world, objects keep a count of the number of references that other objects have to it. Objects are deleted/freed/collected when that reference count goes to Zero. This means that COM Object termination is deterministic (we use Using/IDispose in .net for deterministic termination, object finalizers are non deterministic).</p>
  
  <p>Each CCW is a COM object, and it is reference counted like any other COM object. When the CCW dies (reference count goes to Zero) the GC won't be able to find the CLR object the CCW wrapped, and the CLR object is eligible for collection. Happy days, all is well with the world.</p>
  
  <p>What I would like to do is catch when the CCW dies (i.e. when its reference count goes to zero), and somehow signal this to the CLR object (e.g. By calling a Dispose method on the managed object).</p>
  
  <p><strong>So, is it possible to know when the reference count of a <a href="http://msdn.microsoft.com/en-us/library/f07c8z1c(VS.71).aspx" rel="nofollow noreferrer">COM Callable Wrapper</a> for a CLR class goes to Zero?</strong><br>
  and/or<br>
  <strong>Is it possible to provide my implementation of AddRef &amp; ReleaseRef for CCWs in .net?</strong></p>
  
  <p>If not the alternative is to implement these DLLs in ATL (I don't need any help with ATL, thanks). It wouldn't be rocket science but I'm reluctant to do it as I'm the only developer in-house with any real world C++, or any ATL.</p>
  
  <p><strong>Background</strong><br>
  I'm re-writing some old VB6 ActiveX DLLs in .net (C# to be exact, but this is more a .net / COM interop problem rather than a C# problem). Some of the old VB6 objects depend on reference counting to carry out actions when the object terminates (see explaination of reference counting above). These DLL's don't contain important business logic, they are utilities and helper functions that we provide to clients that integrate with us using VBScript.</p>
  
  <p><strong>What I'm not trying to do</strong></p>
  
  <ul>
  <li>Reference count .net objects instead
  of the using the Garbage Collector. 
  I'm quite happy with the GC, my
  problem isn't with the GC.</li>
  <li>Use object finalizers. Finalizers are
  non deterministic, in this instance I
  need deterministic termination (like
  the Using/IDispose idiom in .net)</li>
  <li>Implement IUnknown in unmanaged C++<br>
  If I've to go the C++ route I'll use
  ATL, thanks.</li>
  <li>Solve this using Vb6, or re-using the
  VB6 objects.   The entire point of
  this exercise is to remove our build
  dependence on Vb6.</li>
  </ul>
  
  <p>Thanks<br>
  BW</p>
  
  <p><strong>The Accepted Answer</strong><br>
  Folks a thousand thanks to <a href="https://stackoverflow.com/questions/2223147/is-it-possible-to-intercept-or-be-aware-of-com-reference-counting-on-clr-object/2260057#2260057">Steve Steiner</a>, who came up with the only (possibly workable) .net based answer, and <a href="https://stackoverflow.com/questions/2223147/is-it-possible-to-intercept-or-be-aware-of-com-reference-counting-on-clr-object/2265934#2265934">Earwicker</a>, who came up with a very simple ATL solution.</p>
  
  <p>However the accepted answer goes to <a href="https://stackoverflow.com/questions/2223147/is-it-possible-to-intercept-or-be-aware-of-com-reference-counting-on-clr-object/2252439#2252439">Bigtoe</a>, who suggests wrapping the .net objects in VbScript objects (which I hadn't considered to be honest), effectively providing a simple VbScript solution to a VbScript problem.</p>
  
  <p>Thanks to all.</p>