Microsoft KB Archive/272676

= BUG: CComContainedObject::QueryInterface Implementation Is Incorrect =

Article ID: 272676

Article Last Modified on 10/22/2002

-

APPLIES TO


 * Microsoft ActiveX Template Library 3.0

-



This article was previously published under Q272676



SYMPTOMS
When you are writing an aggregatable COM object in Active Template Library (ATL), unintended inner object interfaces could be exposed because of a bug in the implementation of the CComContainedObject::QueryInterface function.



CAUSE
The CComContainedObject::QueryInterface function calls the following lines in AtlCom.h:   STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject) {       HRESULT hr = OuterQueryInterface(iid, ppvObject);        if (FAILED(hr) && _GetRawUnknown != m_pOuterUnknown) hr = _InternalQueryInterface(iid, ppvObject); return hr; } If the OuterQueryInterface call fails, a &quot;blind&quot; _InternalQueryInterface is performed in the case of an aggregated object. This can cause a problem when, for instance, the outer object does not want to expose an interface of the inner object (and returns, E_FAIL, for example). The &quot;blind&quot; _InternalQueryInterface in the code returns that interface anyway.



RESOLUTION
To work around the problem and enable the aggregation to work correctly, comment out the highlighted lines in the AtlCom.h file: STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject) {       HRESULT hr = OuterQueryInterface(iid, ppvObject);        // Comment out the two lines below: // if (FAILED(hr) && _GetRawUnknown != m_pOuterUnknown) // hr = _InternalQueryInterface(iid, ppvObject); return hr; } Make these modifications to a copy of the AtlCom.h file (for instance, FixAtlCom.h). Then, in the Stdafx.h file, comment out AtlCom.h and use FixAtlCom.h instead: // #include    #include &quot;FixAtlCom.h&quot;



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



MORE INFORMATION
ATL uses CComContainedObject in the CComAggObject, CComPolyObject, and CComCachedTearOffObject classes. CComContainedObject implements IUnknown by delegating to the owner object's IUnknown. (The owner is either the outer object of an aggregation or the object for which a tear-off interface is being created.)

To prevent unintended side effects and to allow the owner object to have full control, CComContainedObject must not independently return its own interfaces in response to a QueryInterface request, if the owner object's QueryInterface (called through OuterQueryInterface) does not work correctly.

For example, assume that an inner object has an interface IID_Moo, which an outer object does not want to expose. The outer object could use a COM_INTERFACE_ENTRY_NOINTERFACE(IID_Moo) macro in its ATL COM map, right before a COM_INTERFACE_ENTRY_AGGREGATE_BLIND macro.

The current ATL code for CComContainedObject::QueryInterface will allow a QueryInterface for IID_Moo to succeed, which is not expected behavior. The changes given here will allow for the IID_Moo QueryInterface call to fail, as expected.

