www.pudn.com > Aggregation.zip > Cmpnt1.cpp
// // Cmpnt1.cpp - Component 1 // // Interesting bits of code marked with @N. // #include#include #include "..\Agrregation\Component\Iface.h" #include "..\Agrregation\Component\Registry.h" // Trace function void trace(const char* msg) { cout << "Component 1:\t" << msg << endl ;} /////////////////////////////////////////////////////////// // // Global variables // // Static variables static HMODULE g_hModule = NULL ; // DLL module handle static long g_cComponents = 0 ; // Count of active components static long g_cServerLocks = 0 ; // Count of locks // Friendly name of component const char g_szFriendlyName[] = "YNTest Component 1" ; // Version-independent ProgID const char g_szVerIndProgID[] = "YNTest.Cmpnt1" ; // ProgID const char g_szProgID[] = "YNTest.Cmpnt1.1" ; /////////////////////////////////////////////////////////// // // Component A // class CA : public IX // public IY @N { public: // IUnknown virtual HRESULT __stdcall QueryInterface(const IID& iid, void** ppv) ; virtual ULONG __stdcall AddRef() ; virtual ULONG __stdcall Release() ; // Interface IX virtual void __stdcall Fx() { cout << "Fx" << endl ;} virtual void __stdcall FxReturnInner(IUnknown* ppv) { ppv = m_pUnknownInner;} /* @N Component1 aggregates instead of implementing interface IY. // Interface IY virtual void __stdcall Fy() { m_pIY->Fy() ;} */ // Constructor CA() ; // Destructor ~CA() ; // Initialization function called by the class factory // to create the contained component HRESULT __stdcall Init() ; // @N private: // Reference count long m_cRef ; // Pointer to the aggregated component's IY interface // (We do not have to retain an IY pointer. However, we // can use it in QueryInterface.) IY* m_pIY ; // @N // Pointer to inner component's IUnknown IUnknown* m_pUnknownInner ; // @N } ; // // Constructor // CA::CA() : m_cRef(1), m_pUnknownInner(NULL) //@N { ::InterlockedIncrement(&g_cComponents) ; } // // Destructor // CA::~CA() { ::InterlockedDecrement(&g_cComponents) ; trace("Destroy self.") ; // Prevent recursive destruction on next AddRef/Release pair. m_cRef = 1 ; // Counter the pUnknownOuter->Release in the Init method. IUnknown* pUnknownOuter = this ; pUnknownOuter->AddRef() ; // Properly release the pointer; there might be per-interface // reference counts. m_pIY->Release() ; // Release contained component. if (m_pUnknownInner != NULL) // @N { m_pUnknownInner->Release() ; } } // Initialize the component by creating the contained component. HRESULT __stdcall CA::Init() { // Get the pointer to the outer unknown. // Since this component is not aggregated, the outer unknown // is the same as the this pointer. IUnknown* pUnknownOuter = this ; trace("创建内部组件.") ; HRESULT hr = ::CoCreateInstance(CLSID_Component2, pUnknownOuter, // Outer component's IUnknown @N CLSCTX_INPROC_SERVER, IID_IUnknown, // IUnknown when aggregating @N (void**)&m_pUnknownInner) ; if (FAILED(hr)) { trace("不能创建内部组件.") ; return E_FAIL ; } // This call will increment the reference count on the outer component. trace("从内部组件获取IY接口") ; hr = m_pUnknownInner->QueryInterface(IID_IY, (void**)&m_pIY) ; //@N if (FAILED(hr)) { trace("内部组件不支持IY接口.") ; m_pUnknownInner->Release() ; return E_FAIL ; } // We need to release the reference count added to the // outer component in the above call. So call Release // on the pointer you passed to CoCreateInstance. pUnknownOuter->Release() ; //@N return S_OK ; } // // IUnknown implementation // HRESULT __stdcall CA::QueryInterface(const IID& iid, void** ppv) { if (iid == IID_IUnknown) { *ppv = static_cast (this) ; } else if (iid == IID_IX) { *ppv = static_cast (this) ; } else if (iid == IID_IY) { trace("返回内部组件的IY接口.") ; return m_pUnknownInner->QueryInterface(iid,ppv) ; //@N } else { *ppv = NULL ; return E_NOINTERFACE ; } reinterpret_cast (*ppv)->AddRef() ; return S_OK ; } ULONG __stdcall CA::AddRef() { return ::InterlockedIncrement(&m_cRef) ; } ULONG __stdcall CA::Release() { if (::InterlockedDecrement(&m_cRef) == 0) { delete this ; return 0 ; } return m_cRef ; } /////////////////////////////////////////////////////////// // // Class factory // class CFactory : public IClassFactory { public: // IUnknown virtual HRESULT __stdcall QueryInterface(const IID& iid, void** ppv) ; virtual ULONG __stdcall AddRef() ; virtual ULONG __stdcall Release() ; // Interface IClassFactory virtual HRESULT __stdcall CreateInstance(IUnknown* pUnknownOuter, const IID& iid, void** ppv) ; virtual HRESULT __stdcall LockServer(BOOL bLock) ; // Constructor CFactory() : m_cRef(1) {} // Destructor ~CFactory() {} private: long m_cRef ; } ; // // Class factory IUnknown implementation // HRESULT __stdcall CFactory::QueryInterface(REFIID iid, void** ppv) { IUnknown* pI ; if ((iid == IID_IUnknown) || (iid == IID_IClassFactory)) { pI = static_cast (this) ; } else { *ppv = NULL ; return E_NOINTERFACE ; } pI->AddRef() ; *ppv = pI ; return S_OK ; } ULONG __stdcall CFactory::AddRef() { return ::InterlockedIncrement(&m_cRef) ; } ULONG __stdcall CFactory::Release() { if (::InterlockedDecrement(&m_cRef) == 0) { delete this ; return 0 ; } return m_cRef ; } // // IClassFactory implementation // HRESULT __stdcall CFactory::CreateInstance(IUnknown* pUnknownOuter, const IID& iid, void** ppv) { // Cannot aggregate if (pUnknownOuter != NULL) { return CLASS_E_NOAGGREGATION ; } // Create component. CA* pA = new CA ; if (pA == NULL) { return E_OUTOFMEMORY ; } // Initialize the component. @N HRESULT hr = pA->Init() ; if (FAILED(hr)) { // Initialization failed. Delete component. pA->Release() ; return hr ; } // Get the requested interface. hr = pA->QueryInterface(iid, ppv) ; pA->Release() ; return hr ; } // LockServer HRESULT __stdcall CFactory::LockServer(BOOL bLock) { if (bLock) { ::InterlockedIncrement(&g_cServerLocks) ; } else { ::InterlockedDecrement(&g_cServerLocks) ; } return S_OK ; } /////////////////////////////////////////////////////////// // // Exported functions // STDAPI DllCanUnloadNow() { if ((g_cComponents == 0) && (g_cServerLocks == 0)) { return S_OK ; } else { return S_FALSE ; } } // // Get class factory. // STDAPI DllGetClassObject(const CLSID& clsid, const IID& iid, void** ppv) { // Can we create this component? if (clsid != CLSID_Component1) { return CLASS_E_CLASSNOTAVAILABLE ; } // Create class factory. CFactory* pFactory = new CFactory ; // No Addref in constructor if (pFactory == NULL) { return E_OUTOFMEMORY ; } // Get requested interface. HRESULT hr = pFactory->QueryInterface(iid, ppv) ; pFactory->Release() ; return hr ; } // // Server registration // STDAPI DllRegisterServer() { return RegisterServer(g_hModule, CLSID_Component1, g_szFriendlyName, g_szVerIndProgID, g_szProgID) ; } STDAPI DllUnregisterServer() { return UnregisterServer(CLSID_Component1, g_szVerIndProgID, g_szProgID) ; } /////////////////////////////////////////////////////////// // // DLL module information // BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, void* lpReserved) { if (dwReason == DLL_PROCESS_ATTACH) { g_hModule = (HMODULE)hModule ; } return TRUE ; }