aggregator.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:5k
源码类别:

Windows编程

开发平台:

Visual C++

  1. // aggregator.cpp
  2. #include <iostream.h>
  3. #include "aggregatoraggregator.h" // Generated by MIDL
  4. #include "registry.h" // Add This!!!
  5. const CLSID CLSID_Container = {0x10000012,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}};
  6. const CLSID CLSID_InsideCOM = {0x10000002,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}};
  7. long g_cComponents = 0;
  8. long g_cServerLocks = 0;
  9. class CAggregator : public IMultiply
  10. {
  11. public:
  12. // IUnknown
  13. ULONG __stdcall AddRef();
  14. ULONG __stdcall Release();
  15. HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);
  16. // IMultiply
  17. HRESULT __stdcall Multiply(int x, int y, int* retval);
  18. HRESULT Init();
  19. CAggregator();
  20. ~CAggregator();
  21. private:
  22. ULONG m_cRef;
  23. IUnknown* m_pUnknownInner;
  24. };
  25. CAggregator::CAggregator() : m_cRef(1)
  26. {
  27. g_cComponents++;
  28. }
  29. // This code goes in an Init method because a constructor cannot return an error code
  30. HRESULT CAggregator::Init()
  31. {
  32. return CoCreateInstance(CLSID_InsideCOM, (IUnknown*)this, CLSCTX_INPROC_SERVER, IID_IUnknown, (void**)&m_pUnknownInner);
  33. }
  34. CAggregator::~CAggregator()
  35. {
  36. cout << "Container: CAggregator::~CAggregator()" << endl;
  37. g_cComponents--;
  38. m_pUnknownInner->Release();
  39. }
  40. ULONG CAggregator::AddRef()
  41. {
  42. cout << "Container: CAggregator::AddRef() m_cRef = " << m_cRef + 1 << endl;
  43. return ++m_cRef;
  44. }
  45. ULONG CAggregator::Release()
  46. {
  47. cout << "Container: CAggregator::Release() m_cRef = " << m_cRef - 1 << endl;
  48. if(--m_cRef != 0)
  49. return m_cRef;
  50. delete this;
  51. return 0;
  52. }
  53. HRESULT CAggregator::QueryInterface(REFIID riid, void** ppv)
  54. {
  55. if(riid == IID_IUnknown)
  56. {
  57. cout << "Container: CAggregator::QueryInterface() for IUnknown returning " << this << endl;
  58. *ppv = (IUnknown*)this;
  59. }
  60. else if(riid == IID_ISum)
  61. {
  62. cout << "Container: CAggregator::QueryInterface() for ISum calling inside object" << endl;
  63. return m_pUnknownInner->QueryInterface(riid, ppv);
  64. }
  65. else if(riid == IID_IMultiply)
  66. {
  67. cout << "Container: CAggregator::QueryInterface() for ISum returning " << this << endl;
  68. *ppv = (IMultiply*)this;
  69. }
  70. else 
  71. {
  72. *ppv = NULL;
  73. return E_NOINTERFACE;
  74. }
  75. AddRef();
  76. return S_OK;
  77. }
  78. HRESULT CAggregator::Multiply(int x, int y, int* retval)
  79. {
  80. cout << "Container: CAggregator::Multiply() " << x << " * " << y << " = " << x * y << endl;
  81. *retval = x * y;
  82. return S_OK;
  83. }
  84. class CFactory : public IClassFactory
  85. {
  86. public:
  87. // IUnknown
  88. ULONG __stdcall AddRef();
  89. ULONG __stdcall Release();
  90. HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);
  91. // IClassFactory
  92. HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID iid, void** ppv);
  93. HRESULT __stdcall LockServer(BOOL bLock);
  94. CFactory() : m_cRef(1) { }
  95. ~CFactory() { }
  96. private:
  97. ULONG m_cRef;
  98. };
  99. ULONG CFactory::AddRef()
  100. {
  101. cout << "Container: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
  102. return ++m_cRef;
  103. }
  104. ULONG CFactory::Release()
  105. {
  106. cout << "Container: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
  107. if(--m_cRef != 0)
  108. return m_cRef;
  109. delete this;
  110. return 0;
  111. }
  112. HRESULT CFactory::QueryInterface(REFIID iid, void** ppv)
  113. {
  114. if((iid == IID_IUnknown) || (iid == IID_IClassFactory))
  115. {
  116. cout << "Container: CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
  117. *ppv = (IClassFactory *)this;
  118. }
  119. else
  120. {
  121. *ppv = NULL;
  122. return E_NOINTERFACE;
  123. }
  124. AddRef();
  125. return S_OK;
  126. }
  127. HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv)
  128. {
  129. if(pUnknownOuter != NULL)
  130. return CLASS_E_NOAGGREGATION;
  131. CAggregator *pAggregator = new CAggregator();
  132. if(pAggregator == NULL)
  133. return E_OUTOFMEMORY;
  134. pAggregator->Init();
  135. // QueryInterface probably for IID_IUnknown
  136. HRESULT hr = pAggregator->QueryInterface(riid, ppv);
  137. pAggregator->Release();
  138. return hr;
  139. }
  140. HRESULT CFactory::LockServer(BOOL bLock)
  141. {
  142. if(bLock)
  143. g_cServerLocks++;
  144. else
  145. g_cServerLocks--;
  146. return S_OK;
  147. }
  148. HRESULT __stdcall DllCanUnloadNow()
  149. {
  150. cout << "Container: DllCanUnloadNow() " << (g_cServerLocks == 0 && g_cComponents == 0 ? "Yes" : "No") << endl;
  151. if(g_cServerLocks == 0 && g_cComponents == 0)
  152. return S_OK;
  153. else
  154. return S_FALSE;
  155. }
  156. HRESULT __stdcall DllGetClassObject(REFCLSID clsid, REFIID iid, void** ppv)
  157. {
  158. cout << "Container: DllGetClassObject" << endl;
  159. if(clsid != CLSID_Container)
  160. return CLASS_E_CLASSNOTAVAILABLE;
  161. CFactory* pFactory = new CFactory;
  162. if(pFactory == NULL)
  163. return E_OUTOFMEMORY;
  164. // QueryInterface probably for IClassFactory
  165. HRESULT hr = pFactory->QueryInterface(iid, ppv);
  166. pFactory->Release();
  167. return hr;
  168. }
  169. HRESULT __stdcall DllRegisterServer()
  170. {
  171. return RegisterServer("aggregator.dll", CLSID_Container, "Inside COM+ Aggregation Sample", "Component.Container", "Component.Container.1", NULL);
  172. }
  173. HRESULT __stdcall DllUnregisterServer()
  174. {
  175. return UnregisterServer(CLSID_Container, "Component.Container", "Component.Container.1");
  176. }