Geoff Chappell - Software Analyst
An essential feature of Microsoft’s Component Object Model (COM) is that objects be reusable. A general meaning for this is that one programmer may write the code for an object which may then, without adaptation, be contained by an object written by another programmer. Indeed, this containment is possible just with knowledge of the inner object’s interfaces. At its simplest, containment just has the outer object implement its interfaces in terms of the inner object’s interfaces. Though the outer object is a COM server to its clients, it appears to the inner object as a client like any other. This has the merit that the inner object does not need to have anticipated the reuse, but it leaves the outer object with unwelcome overhead since even methods that are passed straight through to the inner object need to be explicitly coded as such.
COM also provides for a particular sort of containment, called aggregation, in which the containment is greatly eased for the outer object because the inner object is explicitly prepared for it. The effect is that the inner object’s methods are exposed as if they are the outer object’s but without the outer object having to code for delegation of every method. An essential part of the technique is that the inner object implements an explicit IUnknown interface that gives access to its other interfaces, but has all its other interfaces delegate their IUnknown methods to an outer IUnknown interface that is provided by the outer object when creating the inner object. The techniques comes with some rules, both for implementation of the inner object and for how the outer object may use the inner object’s interfaces. In particular, when the outer object successfully queries the inner object’s explicit IUnknown interface for an interface other than IUnknown, it must follow by releasing its own outer IUnknown interface. Correspondingly, when releasing an inner object’s interface other than IUnknown, the outer object must first add a reference to the outer IUnknown.
Since version 5.00, SHLWAPI exports two functions to help with these rules for proper reference counting in aggregation:
Neither is yet documented.