1 interface IUnknown 2 { 3 virtual HRESULT __stdcall QueryInterface(const IID & iid, void ** ppv) = 0; 4 virtual ULONG _stdcall AddRef() = 0; 5 virtual ULONG _stdcall Release() = 0; 6 };
1 void foo(IUnknown * pI) 2 { 3 IX * pIX = NULL; 4 HRESULT hr = pI->QueryInterface(IID_IX, (void **)&pIX); 5 if (SUCCEEDED(hr)) 6 { 7 pIX->Fx(); 8 } 9 else 10 ... 11 }
1 HRESULT __stdcall CA::QueryInterface(const IID & iid, void ** ppv) 2 { 3 if (iid == IID_IUnknown) 4 *ppv = static_cast<IUnknown *>this; 5 else if (iid == IID_IX) 6 *ppv = static_cast<IX *>this; 7 else if (iid == IID_IY) 8 *ppv = static_cast<IY *>this; 9 else 10 *ppv = NULL; 11 12 static_cast<IUnknown *>(*ppv)->AddRef(); 13 return S_OK; 14 }
原文:http://www.cnblogs.com/aoun/p/3745119.html