COM 聚合的源码如下,为什么HRESULT CInsideCOM::QueryInterface_NoAggregation(REFIID riid, void** ppv)成员函数中的引用计数代码((IUnknown*)(*ppv))->AddRef();会运行
ULONG CInsideCOM::AddRef_NoAggregation(),调试的时候是这样运行的,逻辑上也应该这样。但是代码上理解不了
// component.cpp
#include <iostream.h>
#include "component\component.h" // Generated by MIDL
#include "registry.h" // Add This!!!// {10000002-0000-0000-0000-000000000001}
const CLSID CLSID_InsideCOM = {0x10000002,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}};long g_cComponents = 0;
long g_cServerLocks = 0;interface INoAggregationUnknown
{
virtual HRESULT __stdcall QueryInterface_NoAggregation(REFIID riid, void** ppv)=0;
virtual ULONG __stdcall AddRef_NoAggregation()=0;
virtual ULONG __stdcall Release_NoAggregation()=0;
};class CInsideCOM : public ISum, public INoAggregationUnknown
{
public:
// IUnknown
ULONG __stdcall AddRef();
ULONG __stdcall Release();
HRESULT __stdcall QueryInterface(REFIID riid, void** ppv); // INoAggregationUnknown
ULONG __stdcall AddRef_NoAggregation();
ULONG __stdcall Release_NoAggregation();
HRESULT __stdcall QueryInterface_NoAggregation(REFIID riid, void** ppv); // ISum
HRESULT __stdcall Sum(int x, int y, int* retval); CInsideCOM(IUnknown* pUnknownOuter);
~CInsideCOM() { cout << "Component: CInsideCOM::~CInsideCOM()" << endl, g_cComponents--; }private:
ULONG m_cRef;
IUnknown* m_pUnknownOuter;
};CInsideCOM::CInsideCOM(IUnknown* pUnknownOuter) : m_cRef(1)
{
g_cComponents++;
if(pUnknownOuter != NULL)
m_pUnknownOuter = pUnknownOuter;
else
m_pUnknownOuter = (IUnknown*)(INoAggregationUnknown*)this;
}HRESULT CInsideCOM::QueryInterface_NoAggregation(REFIID riid, void** ppv)
{
if(riid == IID_IUnknown)
{
cout << "Component: CInsideCOM::QueryInterface() for IUnknown returning " << this << endl;
*ppv = (INoAggregationUnknown*)this;
}
else if(riid == IID_ISum)
{
cout << "Component: CInsideCOM::QueryInterface() for ISum returning " << this << endl;
*ppv = (ISum*)this;
}
else 
{
*ppv = NULL;
return E_NOINTERFACE;
}
((IUnknown*)(*ppv))->AddRef();
return S_OK;
}ULONG CInsideCOM::AddRef_NoAggregation()
{
return ++m_cRef;
}