component with 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 with 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. IUnknown* m_pUnknownFTM;
  25. };
  26. CInsideCOM::CInsideCOM() : m_cRef(1)
  27. {
  28. InterlockedIncrement(&g_cComponents);
  29. CoCreateFreeThreadedMarshaler(this, &m_pUnknownFTM);
  30. }
  31. CInsideCOM::~CInsideCOM()
  32. {
  33. InterlockedDecrement(&g_cComponents);
  34. m_pUnknownFTM->Release();
  35. }
  36. ULONG CInsideCOM::AddRef()
  37. {
  38. cout << "Component: CInsideCOM::AddRef() m_cRef = " << m_cRef + 1 << endl;
  39. return InterlockedIncrement(&m_cRef);
  40. }
  41. ULONG CInsideCOM::Release()
  42. {
  43. cout << "Component: CInsideCOM::Release() m_cRef = " << m_cRef - 1 << endl;
  44. ULONG cRef = InterlockedDecrement(&m_cRef);
  45. if(cRef != 0)
  46. return 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. }
  57. else if(riid == IID_ISum)
  58. {
  59. cout << "Component: CInsideCOM::QueryInterface() for ISum returning " << this << endl;
  60. *ppv = (ISum*)this;
  61. }
  62. else if(riid == IID_IMarshal)
  63. {
  64. cout << "Component: CInsideCOM::QueryInterface() for IMarshal" << endl;
  65. return m_pUnknownFTM->QueryInterface(riid, ppv);
  66. }
  67. else 
  68. {
  69. *ppv = NULL;
  70. return E_NOINTERFACE;
  71. }
  72. 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) { }
  92. ~CFactory() { }
  93. private:
  94. long m_cRef;
  95. };
  96. ULONG CFactory::AddRef()
  97. {
  98. cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
  99. return InterlockedIncrement(&m_cRef);
  100. }
  101. ULONG CFactory::Release()
  102. {
  103. cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
  104. ULONG cRef = InterlockedDecrement(&m_cRef);
  105. if(cRef != 0)
  106. return cRef;
  107. delete this;
  108. return 0;
  109. }
  110. HRESULT CFactory::QueryInterface(REFIID riid, void** ppv)
  111. {
  112. if((riid == IID_IUnknown) || (riid == IID_IClassFactory))
  113. {
  114. cout << "Component: CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
  115. *ppv = (IClassFactory *)this;
  116. }
  117. else
  118. {
  119. *ppv = NULL;
  120. return E_NOINTERFACE;
  121. }
  122. AddRef();
  123. return S_OK;
  124. }
  125. HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv)
  126. {
  127. if(pUnknownOuter != NULL)
  128. return CLASS_E_NOAGGREGATION;
  129. CInsideCOM *pInsideCOM = new CInsideCOM;
  130. cout << "Component: CFactory::CreateInstance() " << pInsideCOM << endl;
  131. if(pInsideCOM == NULL)
  132. return E_OUTOFMEMORY;
  133. // QueryInterface probably for IID_IUNKNOWN
  134. HRESULT hr = pInsideCOM->QueryInterface(riid, ppv);
  135. pInsideCOM->Release();
  136. return hr;
  137. }
  138. HRESULT CFactory::LockServer(BOOL bLock)
  139. {
  140. if(bLock)
  141. InterlockedIncrement(&g_cServerLocks);
  142. else
  143. InterlockedDecrement(&g_cServerLocks);
  144. return S_OK;
  145. }
  146. HRESULT __stdcall DllCanUnloadNow()
  147. {
  148. cout << "Component: DllCanUnloadNow() " << (g_cServerLocks == 0 && g_cComponents == 0 ? "Yes" : "No") << endl;
  149. if(g_cServerLocks == 0 && g_cComponents == 0)
  150. return S_OK;
  151. else
  152. return S_FALSE;
  153. }
  154. HRESULT __stdcall DllGetClassObject(REFCLSID clsid, REFIID riid, void** ppv)
  155. {
  156. cout << "Component: DllGetClassObject" << endl;
  157. if(clsid != CLSID_InsideCOM)
  158. return CLASS_E_CLASSNOTAVAILABLE;
  159. CFactory* pFactory = new CFactory;
  160. if(pFactory == NULL)
  161. return E_OUTOFMEMORY;
  162. // QueryInterface probably for IClassFactory
  163. HRESULT hr = pFactory->QueryInterface(riid, ppv);
  164. pFactory->Release();
  165. return hr;
  166. }
  167. HRESULT __stdcall DllRegisterServer()
  168. {
  169. char DllPath[256];
  170. OLECHAR wDllPath[256];
  171. GetModuleFileName(g_hInstance, DllPath, 256);
  172. mbstowcs(wDllPath, DllPath, 256);
  173. ITypeLib* pTypeLib;
  174. HRESULT hr = LoadTypeLibEx(wDllPath, REGKIND_REGISTER, &pTypeLib);
  175. if(FAILED(hr))
  176. return hr;
  177. pTypeLib->Release();
  178. // Adjust the threading model here
  179. return RegisterServer("Component with FTM.dll", CLSID_InsideCOM, "Inside COM+ Sample", "Component.InsideCOM", "Component.InsideCOM.1", "Apartment");
  180. }
  181. HRESULT __stdcall DllUnregisterServer()
  182. {
  183. HRESULT hr = UnRegisterTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, SYS_WIN32);
  184. if(FAILED(hr))
  185. return hr;
  186. return UnregisterServer(CLSID_InsideCOM, "Component.InsideCOM", "Component.InsideCOM.1");
  187. }
  188. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, void* pv)
  189. {
  190. g_hInstance = hInstance;
  191. return TRUE;
  192. }