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

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