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

Windows编程

开发平台:

Visual C++

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