Understanding the meaning of reference in excel COM

By definition, a reference must point to a memory location. What I want to understand is whether they point to the same or different memory locations?

In the COM world object reference doesn’t point to a memory location, especially in a managed applications written in VB.NET. In .Net you deal with an RCW object which routes your calls to the COM object. Read more about that in the Runtime Callable Wrapper article in MSDN. It states the following:

The runtime creates exactly one RCW for each COM object, regardless of the number of references that exist on that object. The runtime maintains a single RCW per process for each object. If you create an RCW in one application domain or apartment, and then pass a reference to another application domain or apartment, a proxy to the first object will be used. Note that this proxy is a new managed object and not the same as the initial RCW; this means the two managed objects are not equal but do represent the same COM object.

In the COM world when you call a property or method, for example, the oXL.Workbooks property returns an instance of the Workbooks class and increases the reference counter of the corresponding COM object. Say, if you call the property twice in a raw you will get a new object instance in .NET and the refence counter will be increased twice. In that case you need to call the Marshal.ReleaseComObject for each object returned to decrease the refence counter.

Finally, you may find the Why doesn’t Excel quit? article helpful.

Also, in this context is there a way to display the reference count against a COM object?

The Marshal.ReleaseComObject method returns a new value of the reference count of the RCW associated with a COM object.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top