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

Windows编程

开发平台:

Visual C++

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