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