component.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:9k
- // component.cpp
- #include <iostream.h>
- #include <stdio.h>
- #include "Componentcomponent.h" // Generated by MIDL
- #include "registry.h" // Need this
- HINSTANCE g_hInstance;
- long g_cComponents = 0;
- long g_cServerLocks = 0;
- struct num
- {
- num(long value) : number(value), pNextNum(0) { };
- long number;
- num* pNextNum;
- };
- class CInsideCOM : public INumbers, public IEnumVARIANT
- {
- public:
- // IUnknown
- ULONG __stdcall AddRef();
- ULONG __stdcall Release();
- HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);
- // IEnumVARIANT
- HRESULT __stdcall Next(ULONG celt, VARIANT* rgVar, ULONG* pCeltFetched);
- HRESULT __stdcall Skip(ULONG celt);
- HRESULT __stdcall Reset();
- HRESULT __stdcall Clone(IEnumVARIANT** ppEnum);
- // IDispatch
- HRESULT __stdcall GetTypeInfoCount(UINT* pCountTypeInfo);
- HRESULT __stdcall GetTypeInfo(UINT iTypeInfo, LCID lcid, ITypeInfo** ppITypeInfo);
- HRESULT __stdcall GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId);
- HRESULT __stdcall Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr);
- // INumbers
- HRESULT __stdcall get_Item(long n, long* pVal);
- HRESULT __stdcall get__NewEnum(IUnknown** pVal);
- HRESULT __stdcall get_Count(long *pVal);
- HRESULT __stdcall Add(long Val);
- HRESULT __stdcall Remove(long index);
- CInsideCOM();
- ~CInsideCOM() { cout << "Component: CInsideCOM::~CInsideCOM()" << endl, g_cComponents--; }
- bool Init(void);
- private:
- ULONG m_cRef;
- ITypeInfo* m_pTypeInfo;
- num* m_pFirstNum;
- num* m_pCurrentNum;
- };
- CInsideCOM::CInsideCOM() : m_cRef(1), m_pCurrentNum(0)
- {
- g_cComponents++;
- m_pFirstNum = 0;
- m_pCurrentNum = 0;
- }
- HRESULT CInsideCOM::Next(ULONG celt, VARIANT* rgVar, ULONG* pCeltFetched)
- {
- // Capture walking past the end of the list from a previous call
- if(!m_pCurrentNum)
- {
- if(pCeltFetched != 0)
- *pCeltFetched = 0;
- return S_FALSE;
- }
- for(unsigned count = 0; count < celt; count++)
- {
- // Capture walking past the end of the list from a previous loop iteration
- if(!m_pCurrentNum)
- {
- if(pCeltFetched != 0)
- *pCeltFetched = count+1;
- return S_FALSE;
- }
- VariantInit(&rgVar[count]);
- rgVar[count].intVal = m_pCurrentNum->number;
- rgVar[count].vt = VT_I4;
- m_pCurrentNum = m_pCurrentNum->pNextNum;
- }
- if(pCeltFetched != 0)
- *pCeltFetched = count+1;
- return S_OK;
- }
- HRESULT CInsideCOM::Skip(ULONG celt)
- {
- for(unsigned count = 0; count < celt; count++)
- if(m_pCurrentNum->pNextNum)
- m_pCurrentNum = m_pCurrentNum->pNextNum;
- else
- break;
- return S_OK;
- }
- HRESULT CInsideCOM::Reset()
- {
- m_pCurrentNum = m_pFirstNum;
- return S_OK;
- }
- HRESULT CInsideCOM::Clone(IEnumVARIANT** ppEnum)
- {
- *ppEnum = 0;
- return E_NOTIMPL;
- }
- HRESULT CInsideCOM::Add(long Val)
- {
- if(!m_pFirstNum)
- {
- m_pFirstNum = new num(Val);
- m_pCurrentNum = m_pFirstNum;
- return S_OK;
- }
- num* pNum = m_pFirstNum;
- while(pNum->pNextNum)
- pNum = pNum->pNextNum;
- pNum->pNextNum = new num(Val);
- return S_OK;
- }
- HRESULT CInsideCOM::Remove(long index)
- {
- num* pPreviousNum = 0;
- num* pNum = m_pFirstNum;
- if(!pNum)
- return E_INVALIDARG;
- if(index < 1)
- return E_INVALIDARG;
- // deleting the first number
- if(index - 1 == 0)
- {
- // are the first and current num pointing to the same element
- if(m_pFirstNum == m_pCurrentNum)
- m_pCurrentNum = m_pFirstNum->pNextNum;
- m_pFirstNum = m_pFirstNum->pNextNum;
- delete pNum;
- return S_OK;
- }
- for(long count = 0; count < index-1; count++)
- {
- pPreviousNum = pNum;
- if(pNum->pNextNum)
- pNum = pNum->pNextNum;
- else
- return E_INVALIDARG;
- }
- pPreviousNum->pNextNum = pNum->pNextNum;
- delete pNum;
- return S_OK;
- }
- HRESULT CInsideCOM::get__NewEnum(IUnknown** pVal)
- {
- return QueryInterface(IID_IUnknown, (void**)pVal);
- }
- HRESULT CInsideCOM::get_Item(long n, long* pVal)
- {
- num* pNum = m_pFirstNum;
- if(!pNum)
- return E_INVALIDARG;
- for(long count = 0; count < n-1; count++)
- if(pNum->pNextNum)
- pNum = pNum->pNextNum;
- else
- return E_INVALIDARG;
- *pVal = pNum->number;
- return S_OK;
- }
- HRESULT CInsideCOM::get_Count(long *pVal)
- {
- num* pNum = m_pFirstNum;
- if(!pNum)
- {
- *pVal = 0;
- return S_OK;
- }
- long count = 0;
- while(pNum->pNextNum)
- {
- pNum = pNum->pNextNum;
- count++;
- }
- *pVal = count+1;
- return S_OK;
- }
- HRESULT CInsideCOM::GetTypeInfoCount(UINT* pCountTypeInfo)
- {
- *pCountTypeInfo = 1;
- return S_OK;
- }
- HRESULT CInsideCOM::GetTypeInfo(UINT iTypeInfo, LCID lcid, ITypeInfo** ppITypeInfo)
- {
- *ppITypeInfo = NULL;
- if(iTypeInfo != 0)
- return DISP_E_BADINDEX;
- m_pTypeInfo->AddRef();
- *ppITypeInfo = m_pTypeInfo;
- return S_OK;
- }
- HRESULT CInsideCOM::GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId)
- {
- if(riid != IID_NULL)
- return DISP_E_UNKNOWNINTERFACE;
- return DispGetIDsOfNames(m_pTypeInfo, rgszNames, cNames, rgDispId);
- }
- HRESULT CInsideCOM::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
- {
- if(riid != IID_NULL)
- return DISP_E_UNKNOWNINTERFACE;
- return DispInvoke(this, m_pTypeInfo, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
- }
- bool CInsideCOM::Init(void)
- {
- ITypeLib* pTypeLib;
- if(FAILED(LoadRegTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, &pTypeLib)))
- return false;
- HRESULT hr = pTypeLib->GetTypeInfoOfGuid(IID_INumbers, &m_pTypeInfo);
- pTypeLib->Release();
- if(FAILED(hr))
- return false;
- return true;
- }
- ULONG CInsideCOM::AddRef()
- {
- cout << "Component: CInsideCOM::AddRef() m_cRef = " << m_cRef + 1 << endl;
- return ++m_cRef;
- }
- ULONG CInsideCOM::Release()
- {
- cout << "Component: CInsideCOM::Release() m_cRef = " << m_cRef - 1 << endl;
- if(--m_cRef != 0)
- return m_cRef;
- m_pTypeInfo->Release();
- delete this;
- return 0;
- }
- HRESULT CInsideCOM::QueryInterface(REFIID riid, void** ppv)
- {
- if(riid == IID_IUnknown)
- *ppv = reinterpret_cast<IUnknown*>(this);
- else if(riid == IID_INumbers)
- *ppv = (INumbers*)this;
- else if(riid == IID_IDispatch)
- *ppv = (IDispatch*)this;
- else if(riid == IID_IEnumVARIANT)
- *ppv = (IEnumVARIANT*)this;
- else
- {
- *ppv = NULL;
- return E_NOINTERFACE;
- }
- AddRef();
- 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:
- long m_cRef;
- };
- ULONG CFactory::AddRef()
- {
- cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
- return ++m_cRef;
- }
- ULONG CFactory::Release()
- {
- cout << "Component: 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 << "Component: 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 iid, void** ppv)
- {
- if(pUnknownOuter != NULL)
- return CLASS_E_NOAGGREGATION;
- CInsideCOM *pInsideCOM = new CInsideCOM;
- cout << "Component: CFactory::CreateInstance() " << pInsideCOM << endl;
- if(pInsideCOM == NULL)
- return E_OUTOFMEMORY;
- // Call the Init method to load the type information
- pInsideCOM->Init();
- HRESULT hr = pInsideCOM->QueryInterface(iid, ppv);
- pInsideCOM->Release();
- return hr;
- }
- HRESULT CFactory::LockServer(BOOL bLock)
- {
- if(bLock)
- g_cServerLocks++;
- else
- g_cServerLocks--;
- return S_OK;
- }
- HRESULT __stdcall DllCanUnloadNow()
- {
- cout << "Component: 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 << "Component: DllGetClassObject" << endl;
-
- if(clsid != CLSID_InsideCOM)
- 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()
- {
- char DllPath[256];
- OLECHAR wDllPath[256];
- GetModuleFileName(g_hInstance, DllPath, 256);
- mbstowcs(wDllPath, DllPath, 256);
- ITypeLib* pTypeLib;
- HRESULT hr = LoadTypeLibEx(wDllPath, REGKIND_REGISTER, &pTypeLib);
- if(FAILED(hr))
- return hr;
- pTypeLib->Release();
- return RegisterServer("component.dll", CLSID_InsideCOM, "Inside COM+ Sample", "Component.InsideCOM", "Component.InsideCOM.1", NULL);
- }
- HRESULT __stdcall DllUnregisterServer()
- {
- UnRegisterTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, SYS_WIN32);
- return UnregisterServer(CLSID_InsideCOM, "Component.InsideCOM", "Component.InsideCOM.1");
- }
- BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, void* pv)
- {
- g_hInstance = hInstance;
- return TRUE;
- }