aggregator.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:5k
- // aggregator.cpp
- #include <iostream.h>
- #include "aggregatoraggregator.h" // Generated by MIDL
- #include "registry.h" // Add This!!!
- const CLSID CLSID_Container = {0x10000012,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}};
- const CLSID CLSID_InsideCOM = {0x10000002,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}};
- long g_cComponents = 0;
- long g_cServerLocks = 0;
- class CAggregator : public IMultiply
- {
- public:
- // IUnknown
- ULONG __stdcall AddRef();
- ULONG __stdcall Release();
- HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);
- // IMultiply
- HRESULT __stdcall Multiply(int x, int y, int* retval);
- HRESULT Init();
- CAggregator();
- ~CAggregator();
- private:
- ULONG m_cRef;
- IUnknown* m_pUnknownInner;
- };
- CAggregator::CAggregator() : m_cRef(1)
- {
- g_cComponents++;
- }
- // This code goes in an Init method because a constructor cannot return an error code
- HRESULT CAggregator::Init()
- {
- return CoCreateInstance(CLSID_InsideCOM, (IUnknown*)this, CLSCTX_INPROC_SERVER, IID_IUnknown, (void**)&m_pUnknownInner);
- }
- CAggregator::~CAggregator()
- {
- cout << "Container: CAggregator::~CAggregator()" << endl;
- g_cComponents--;
- m_pUnknownInner->Release();
- }
- ULONG CAggregator::AddRef()
- {
- cout << "Container: CAggregator::AddRef() m_cRef = " << m_cRef + 1 << endl;
- return ++m_cRef;
- }
- ULONG CAggregator::Release()
- {
- cout << "Container: CAggregator::Release() m_cRef = " << m_cRef - 1 << endl;
- if(--m_cRef != 0)
- return m_cRef;
- delete this;
- return 0;
- }
- HRESULT CAggregator::QueryInterface(REFIID riid, void** ppv)
- {
- if(riid == IID_IUnknown)
- {
- cout << "Container: CAggregator::QueryInterface() for IUnknown returning " << this << endl;
- *ppv = (IUnknown*)this;
- }
- else if(riid == IID_ISum)
- {
- cout << "Container: CAggregator::QueryInterface() for ISum calling inside object" << endl;
- return m_pUnknownInner->QueryInterface(riid, ppv);
- }
- else if(riid == IID_IMultiply)
- {
- cout << "Container: CAggregator::QueryInterface() for ISum returning " << this << endl;
- *ppv = (IMultiply*)this;
- }
- else
- {
- *ppv = NULL;
- return E_NOINTERFACE;
- }
- AddRef();
- return S_OK;
- }
- HRESULT CAggregator::Multiply(int x, int y, int* retval)
- {
- cout << "Container: CAggregator::Multiply() " << x << " * " << y << " = " << x * y << endl;
- *retval = x * y;
- return S_OK;
- }
- class CFactory : public IClassFactory
- {
- public:
- // IUnknown
- ULONG __stdcall AddRef();
- ULONG __stdcall Release();
- HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);
- // IClassFactory
- HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID iid, void** ppv);
- HRESULT __stdcall LockServer(BOOL bLock);
- CFactory() : m_cRef(1) { }
- ~CFactory() { }
- private:
- ULONG m_cRef;
- };
- ULONG CFactory::AddRef()
- {
- cout << "Container: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
- return ++m_cRef;
- }
- ULONG CFactory::Release()
- {
- cout << "Container: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
- if(--m_cRef != 0)
- return m_cRef;
- delete this;
- return 0;
- }
- HRESULT CFactory::QueryInterface(REFIID iid, void** ppv)
- {
- if((iid == IID_IUnknown) || (iid == IID_IClassFactory))
- {
- cout << "Container: CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
- *ppv = (IClassFactory *)this;
- }
- else
- {
- *ppv = NULL;
- return E_NOINTERFACE;
- }
- AddRef();
- return S_OK;
- }
- HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv)
- {
- if(pUnknownOuter != NULL)
- return CLASS_E_NOAGGREGATION;
- CAggregator *pAggregator = new CAggregator();
- if(pAggregator == NULL)
- return E_OUTOFMEMORY;
- pAggregator->Init();
- // QueryInterface probably for IID_IUnknown
- HRESULT hr = pAggregator->QueryInterface(riid, ppv);
- pAggregator->Release();
- return hr;
- }
- HRESULT CFactory::LockServer(BOOL bLock)
- {
- if(bLock)
- g_cServerLocks++;
- else
- g_cServerLocks--;
- return S_OK;
- }
- HRESULT __stdcall DllCanUnloadNow()
- {
- cout << "Container: DllCanUnloadNow() " << (g_cServerLocks == 0 && g_cComponents == 0 ? "Yes" : "No") << endl;
- if(g_cServerLocks == 0 && g_cComponents == 0)
- return S_OK;
- else
- return S_FALSE;
- }
- HRESULT __stdcall DllGetClassObject(REFCLSID clsid, REFIID iid, void** ppv)
- {
- cout << "Container: DllGetClassObject" << endl;
-
- if(clsid != CLSID_Container)
- return CLASS_E_CLASSNOTAVAILABLE;
- CFactory* pFactory = new CFactory;
- if(pFactory == NULL)
- return E_OUTOFMEMORY;
- // QueryInterface probably for IClassFactory
- HRESULT hr = pFactory->QueryInterface(iid, ppv);
- pFactory->Release();
- return hr;
- }
- HRESULT __stdcall DllRegisterServer()
- {
- return RegisterServer("aggregator.dll", CLSID_Container, "Inside COM+ Aggregation Sample", "Component.Container", "Component.Container.1", NULL);
- }
- HRESULT __stdcall DllUnregisterServer()
- {
- return UnregisterServer(CLSID_Container, "Component.Container", "Component.Container.1");
- }