I've been learning about aggregating COM objects.

I dont understand why we 'need' 2 IUnknowns (ie the delegating IUnknown and the Non-delegating IUnknown). Surely the same effect can be achieved with just the 1 usual IUnknown?


The outer component implements interface IX and aggregates interface IY, which is implemented in the inner component. the inner component only implements IY. so, when the client queries the outer component for IY, the outer component then queries the inner conponent for IY and returns this to the client. if the client then queries IY for IX, this should succeed because although the inner component does not implement IX, it forwards the query onto the outer component.

So surely this can be achieved with the following code:

Inner::QueryInterface(REFIID iid, (void**)piid)
if(iid == IID_IY) 
piid = static_cast < IY*>(this);
outerUnknown->QueryInterface(iid, piid);

so if the client then queries IY for IX, or IUnknown, it will get the outer components IX or IUnknown, which is what we want.

unless im missing something glaringly obvious! which i suspect i am.

ah ha,

the code assumes that the component is aggregated. if is not being aggregated then the code does not make sense, in fact i think it will recursively call QueryInterface if its not IY and blow the stack!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.