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"
  5. const REG_DATA g_regData[] = {
  6.     { "CLSID\{10000002-0000-0000-0000-000000000001}", 0, "Inside COM+: In Process Component" },
  7. { "CLSID\{10000002-0000-0000-0000-000000000001}\InprocServer32", 0, (const char*)-1 }, 
  8. { "CLSID\{10000002-0000-0000-0000-000000000001}\ProgID", 0, "Component.InsideCOM.1" },
  9. { "CLSID\{10000002-0000-0000-0000-000000000001}\VersionIndependentProgID", 0, "Component.InsideCOM" },
  10. { "Component.InsideCOM", 0, "Inside COM+: In Process Component" },
  11. { "Component.InsideCOM\CLSID", 0, "{10000002-0000-0000-0000-000000000001}" },
  12. { "Component.InsideCOM\CurVer", 0, "Component.InsideCOM.1" },
  13. { "Component.InsideCOM.1", 0, "Inside COM+: In Process Component" },
  14. { "Component.InsideCOM.1\CLSID", 0, "{10000002-0000-0000-0000-000000000001}" },
  15. { 0, 0, 0 }
  16. };
  17. HINSTANCE g_hInstance;
  18. long g_cLocks = 0;
  19. class CInsideCOM : public ISum
  20. {
  21. public:
  22. // IUnknown
  23. ULONG __stdcall AddRef();
  24. ULONG __stdcall Release();
  25. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  26. // ISum
  27. HRESULT __stdcall Sum(int x, int y, int* retval);
  28. CInsideCOM() : m_cRef(1) { g_cLocks++; }
  29. ~CInsideCOM()
  30. {
  31. cout << "Component: CInsideCOM::~CInsideCOM()" << endl;
  32. g_cLocks--;
  33. }
  34. private:
  35. ULONG m_cRef;
  36. };
  37. ULONG CInsideCOM::AddRef()
  38. {
  39. cout << "Component: CInsideCOM::AddRef() m_cRef = " << m_cRef + 1 << endl;
  40. return ++m_cRef;
  41. }
  42. ULONG CInsideCOM::Release()
  43. {
  44. cout << "Component: CInsideCOM::Release() m_cRef = " << m_cRef - 1 << endl;
  45. if(--m_cRef != 0)
  46. return m_cRef;
  47. delete this;
  48. return 0;
  49. }
  50. HRESULT CInsideCOM::QueryInterface(REFIID riid, void** ppv)
  51. {
  52. if(riid == IID_IUnknown)
  53. {
  54. cout << "Component: CInsideCOM::QueryInterface() for IUnknown returning " << this << endl;
  55. *ppv = (IUnknown*)this;
  56. // *ppv = static_cast<IUnknown*>(this);
  57. }
  58. else if(riid == IID_ISum)
  59. {
  60. cout << "Component: CInsideCOM::QueryInterface() for ISum returning " << this << endl;
  61. *ppv = (ISum*)this;
  62. // *ppv = static_cast<ISum*>(this);
  63. }
  64. else 
  65. {
  66. *ppv = NULL;
  67. return E_NOINTERFACE;
  68. }
  69. AddRef();
  70. // reinterpret_cast<IUnknown*>(*ppv)->AddRef();
  71. return S_OK;
  72. }
  73. HRESULT CInsideCOM::Sum(int x, int y, int* retval)
  74. {
  75. cout << "Component: CInsideCOM::Sum() " << x << " + " << y << " = " << x + y << endl;
  76. *retval = x + y;
  77. return S_OK;
  78. }
  79. class CFactory : public IClassFactory
  80. {
  81. public:
  82. // IUnknown
  83. ULONG __stdcall AddRef();
  84. ULONG __stdcall Release();
  85. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  86. // IClassFactory
  87. HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv);
  88. HRESULT __stdcall LockServer(BOOL bLock);
  89. CFactory() : m_cRef(1) { g_cLocks++; }
  90. ~CFactory() { g_cLocks--; }
  91. private:
  92. ULONG m_cRef;
  93. };
  94. ULONG CFactory::AddRef()
  95. {
  96. cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
  97. return ++m_cRef;
  98. // g_cLocks++;
  99. // return 2;
  100. }
  101. ULONG CFactory::Release()
  102. {
  103. cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
  104. if(--m_cRef != 0)
  105. return m_cRef;
  106. delete this;
  107. return 0;
  108. // g_cLocks--;
  109. // return 1;
  110. }
  111. HRESULT CFactory::QueryInterface(REFIID riid, void** ppv)
  112. {
  113. if(riid == IID_IUnknown)
  114. {
  115. cout << "Component: CFactory::QueryInterface() for IUnknown returning " << this << endl;
  116. *ppv = (IUnknown*)this;
  117. }
  118. if(riid == IID_IClassFactory)
  119. {
  120. cout << "Component: CFactory::QueryInteface() for IClassFactory " << this << endl;
  121. *ppv = (IClassFactory*)this;
  122. }
  123. else
  124. {
  125. *ppv = NULL;
  126. return E_NOINTERFACE;
  127. }
  128. AddRef();
  129. return S_OK;
  130. }
  131. HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv)
  132. {
  133. if(pUnknownOuter != NULL)
  134. return CLASS_E_NOAGGREGATION;
  135. CInsideCOM *pInsideCOM = new CInsideCOM;
  136. cout << "Component: CFactory::CreateInstance() " << pInsideCOM << endl;
  137. if(pInsideCOM == NULL)
  138. return E_OUTOFMEMORY;
  139. // QueryInterface probably for IID_IUnknown
  140. HRESULT hr = pInsideCOM->QueryInterface(riid, ppv);
  141. pInsideCOM->Release();
  142. return hr;
  143. }
  144. HRESULT CFactory::LockServer(BOOL bLock)
  145. {
  146. if(bLock)
  147. g_cLocks++;
  148. else
  149. g_cLocks--;
  150. return S_OK;
  151. }
  152. HRESULT __stdcall DllCanUnloadNow()
  153. {
  154. cout << "Component: DllCanUnloadNow() " << (g_cLocks == 0 ? "Yes" : "No") << endl;
  155. if(g_cLocks == 0)
  156. return S_OK;
  157. else
  158.     return S_FALSE;
  159. }
  160. HRESULT __stdcall DllGetClassObject(REFCLSID clsid, REFIID riid, void** ppv)
  161. {
  162. cout << "Component: DllGetClassObject" << endl;
  163. if(clsid != CLSID_InsideCOM)
  164. return CLASS_E_CLASSNOTAVAILABLE;
  165. CFactory* pFactory = new CFactory;
  166. if(pFactory == NULL)
  167. return E_OUTOFMEMORY;
  168. // QueryInterface probably for IClassFactory
  169. HRESULT hr = pFactory->QueryInterface(riid, ppv);
  170. pFactory->Release();
  171. return hr;
  172. }
  173. HRESULT __stdcall DllRegisterServer()
  174. {
  175. char DllPath[MAX_PATH];
  176. GetModuleFileName(g_hInstance, DllPath, sizeof(DllPath));
  177. OLECHAR wDllPath[MAX_PATH];
  178. mbstowcs(wDllPath, DllPath, sizeof(wDllPath));
  179. ITypeLib* pTypeLib;
  180. HRESULT hr = LoadTypeLibEx(wDllPath, REGKIND_REGISTER, &pTypeLib);
  181. if(FAILED(hr))
  182. return hr;
  183. pTypeLib->Release();
  184. return RegisterServerEx(g_regData, DllPath);
  185. }
  186. HRESULT __stdcall DllUnregisterServer()
  187. {
  188. UnRegisterTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, SYS_WIN32);
  189. return UnregisterServerEx(g_regData);
  190. }
  191. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, void* pv)
  192. {
  193. g_hInstance = hInstance;
  194. return TRUE;
  195. }