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

Windows编程

开发平台:

Visual C++

  1. // component.cpp
  2. // This in-process component can be installed with the ThreadingModel value
  3. // set to Apartment, Free, or Both. The can be adjusted in the last parameter
  4. // of the RegisterServer function.
  5. #define _WIN32_DCOM
  6. #include <iostream.h>  // For cout
  7. #include "compcomp.h"      // Generated by MIDL
  8. #include "registry.h"  // For registry functions
  9. HINSTANCE g_hInstance;
  10. long g_cComponents = 0;
  11. long g_cServerLocks = 0;
  12. class CInsideCOM : public ISum
  13. {
  14. public:
  15. // IUnknown
  16. ULONG __stdcall AddRef();
  17. ULONG __stdcall Release();
  18. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  19. // ISum
  20. HRESULT __stdcall Sum(int x, int y, int* retval);
  21. CInsideCOM();
  22. ~CInsideCOM();
  23. private:
  24. long m_cRef;
  25. };
  26. CInsideCOM::CInsideCOM() : m_cRef(1)
  27. {
  28. InterlockedIncrement(&g_cComponents);
  29. }
  30. CInsideCOM::~CInsideCOM()
  31. {
  32. InterlockedDecrement(&g_cComponents);
  33. }
  34. ULONG CInsideCOM::AddRef()
  35. {
  36. cout << "Component: CInsideCOM::AddRef() m_cRef = " << m_cRef + 1 << endl;
  37. return InterlockedIncrement(&m_cRef);
  38. }
  39. ULONG CInsideCOM::Release()
  40. {
  41. cout << "Component: CInsideCOM::Release() m_cRef = " << m_cRef - 1 << endl;
  42. ULONG cRef = InterlockedDecrement(&m_cRef);
  43. if(cRef != 0)
  44. return cRef;
  45. delete this;
  46. return 0;
  47. }
  48. HRESULT CInsideCOM::QueryInterface(REFIID riid, void** ppv)
  49. {
  50. if(riid == IID_IUnknown)
  51. {
  52. cout << "Component: CInsideCOM::QueryInterface() for IUnknown returning " << this << endl;
  53. *ppv = (IUnknown*)this;
  54. }
  55. else if(riid == IID_ISum)
  56. {
  57. cout << "Component: CInsideCOM::QueryInterface() for ISum returning " << this << endl;
  58. *ppv = (ISum*)this;
  59. }
  60. else 
  61. {
  62. *ppv = NULL;
  63. return E_NOINTERFACE;
  64. }
  65. AddRef();
  66. return S_OK;
  67. }
  68. HRESULT CInsideCOM::Sum(int x, int y, int* retval)
  69. {
  70. // cout << "Component: CInsideCOM::Sum() " << x << " + " << y << " = " << x + y << endl;
  71. *retval = x + y;
  72. return S_OK;
  73. }
  74. class CFactory : public IClassFactory
  75. {
  76. public:
  77. // IUnknown
  78. ULONG __stdcall AddRef();
  79. ULONG __stdcall Release();
  80. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  81. // IClassFactory
  82. HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv);
  83. HRESULT __stdcall LockServer(BOOL bLock);
  84. CFactory() : m_cRef(1) { }
  85. ~CFactory() { }
  86. private:
  87. long m_cRef;
  88. };
  89. ULONG CFactory::AddRef()
  90. {
  91. cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
  92. return InterlockedIncrement(&m_cRef);
  93. }
  94. ULONG CFactory::Release()
  95. {
  96. cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
  97. ULONG cRef = InterlockedDecrement(&m_cRef);
  98. if(cRef != 0)
  99. return cRef;
  100. delete this;
  101. return 0;
  102. }
  103. HRESULT CFactory::QueryInterface(REFIID riid, void** ppv)
  104. {
  105. if((riid == IID_IUnknown) || (riid == IID_IClassFactory))
  106. {
  107. cout << "Component: CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
  108. *ppv = (IClassFactory *)this;
  109. }
  110. else
  111. {
  112. *ppv = NULL;
  113. return E_NOINTERFACE;
  114. }
  115. AddRef();
  116. return S_OK;
  117. }
  118. HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv)
  119. {
  120. if(pUnknownOuter != NULL)
  121. return CLASS_E_NOAGGREGATION;
  122. CInsideCOM *pInsideCOM = new CInsideCOM;
  123. cout << "Component: CFactory::CreateInstance() " << pInsideCOM << endl;
  124. if(pInsideCOM == NULL)
  125. return E_OUTOFMEMORY;
  126. // QueryInterface probably for IID_IUNKNOWN
  127. HRESULT hr = pInsideCOM->QueryInterface(riid, ppv);
  128. pInsideCOM->Release();
  129. return hr;
  130. }
  131. HRESULT CFactory::LockServer(BOOL bLock)
  132. {
  133. if(bLock)
  134. InterlockedIncrement(&g_cServerLocks);
  135. else
  136. InterlockedDecrement(&g_cServerLocks);
  137. return S_OK;
  138. }
  139. HRESULT __stdcall DllCanUnloadNow()
  140. {
  141. cout << "Component: DllCanUnloadNow() " << (g_cServerLocks == 0 && g_cComponents == 0 ? "Yes" : "No") << endl;
  142. if(g_cServerLocks == 0 && g_cComponents == 0)
  143. return S_OK;
  144. else
  145. return S_FALSE;
  146. }
  147. HRESULT __stdcall DllGetClassObject(REFCLSID clsid, REFIID riid, void** ppv)
  148. {
  149. cout << "Component: DllGetClassObject" << endl;
  150. if(clsid != CLSID_InsideCOM2)
  151. return CLASS_E_CLASSNOTAVAILABLE;
  152. CFactory* pFactory = new CFactory;
  153. if(pFactory == NULL)
  154. return E_OUTOFMEMORY;
  155. // QueryInterface probably for IClassFactory
  156. HRESULT hr = pFactory->QueryInterface(riid, ppv);
  157. pFactory->Release();
  158. return hr;
  159. }
  160. HRESULT __stdcall DllRegisterServer()
  161. {
  162. char DllPath[256];
  163. OLECHAR wDllPath[256];
  164. GetModuleFileName(g_hInstance, DllPath, 256);
  165. mbstowcs(wDllPath, DllPath, 256);
  166. ITypeLib* pTypeLib;
  167. HRESULT hr = LoadTypeLibEx(wDllPath, REGKIND_REGISTER, &pTypeLib);
  168. if(FAILED(hr))
  169. return hr;
  170. pTypeLib->Release();
  171. // Adjust the threading model here
  172. return RegisterServer("comp.dll", CLSID_InsideCOM2, "Inside COM+ Sample", "Component.InsideCOM2", "Component.InsideCOM2.1", "Apartment");
  173. }
  174. HRESULT __stdcall DllUnregisterServer()
  175. {
  176. HRESULT hr = UnRegisterTypeLib(LIBID_Comp, 1, 0, LANG_NEUTRAL, SYS_WIN32);
  177. if(FAILED(hr))
  178. return hr;
  179. return UnregisterServer(CLSID_InsideCOM2, "Component.InsideCOM2", "Component.InsideCOM2.1");
  180. }
  181. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, void* pv)
  182. {
  183. g_hInstance = hInstance;
  184. return TRUE;
  185. }