component.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 "Componentcomponent.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() : m_cRef(1) { InterlockedIncrement(&g_cComponents); }
  21. ~CInsideCOM() { InterlockedDecrement(&g_cComponents); }
  22. private:
  23. long m_cRef;
  24. };
  25. ULONG CInsideCOM::AddRef()
  26. {
  27. cout << "Component: CInsideCOM::AddRef() m_cRef = " << m_cRef + 1 << endl;
  28. return InterlockedIncrement(&m_cRef);
  29. }
  30. ULONG CInsideCOM::Release()
  31. {
  32. cout << "Component: CInsideCOM::Release() m_cRef = " << m_cRef - 1 << endl;
  33. ULONG cRef = InterlockedDecrement(&m_cRef);
  34. if(cRef != 0)
  35. return cRef;
  36. delete this;
  37. return 0;
  38. }
  39. HRESULT CInsideCOM::QueryInterface(REFIID riid, void** ppv)
  40. {
  41. if(riid == IID_IUnknown)
  42. {
  43. cout << "Component: CInsideCOM::QueryInterface() for IUnknown returning " << this << endl;
  44. *ppv = (IUnknown*)this;
  45. }
  46. else if(riid == IID_ISum)
  47. {
  48. cout << "Component: CInsideCOM::QueryInterface() for ISum returning " << this << endl;
  49. *ppv = (ISum*)this;
  50. }
  51. else 
  52. {
  53. *ppv = NULL;
  54. return E_NOINTERFACE;
  55. }
  56. AddRef();
  57. return S_OK;
  58. }
  59. HRESULT CInsideCOM::Sum(int x, int y, int* retval)
  60. {
  61. cout << "Component: Sum() called on thread " << GetCurrentThreadId() << endl;
  62. *retval = x + y;
  63. return S_OK;
  64. }
  65. class CFactory : public IClassFactory
  66. {
  67. public:
  68. // IUnknown
  69. ULONG __stdcall AddRef();
  70. ULONG __stdcall Release();
  71. HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
  72. // IClassFactory
  73. HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv);
  74. HRESULT __stdcall LockServer(BOOL bLock);
  75. CFactory() : m_cRef(1) { }
  76. ~CFactory() { }
  77. private:
  78. long m_cRef;
  79. };
  80. ULONG CFactory::AddRef()
  81. {
  82. cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
  83. return InterlockedIncrement(&m_cRef);
  84. }
  85. ULONG CFactory::Release()
  86. {
  87. cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
  88. ULONG cRef = InterlockedDecrement(&m_cRef);
  89. if(cRef != 0)
  90. return cRef;
  91. delete this;
  92. return 0;
  93. }
  94. HRESULT CFactory::QueryInterface(REFIID riid, void** ppv)
  95. {
  96. if((riid == IID_IUnknown) || (riid == IID_IClassFactory))
  97. {
  98. cout << "Component: CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
  99. *ppv = (IClassFactory *)this;
  100. }
  101. else
  102. {
  103. *ppv = NULL;
  104. return E_NOINTERFACE;
  105. }
  106. AddRef();
  107. return S_OK;
  108. }
  109. HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv)
  110. {
  111. if(pUnknownOuter != NULL)
  112. return CLASS_E_NOAGGREGATION;
  113. CInsideCOM *pInsideCOM = new CInsideCOM;
  114. cout << "Component: CFactory::CreateInstance() " << pInsideCOM << endl;
  115. if(pInsideCOM == NULL)
  116. return E_OUTOFMEMORY;
  117. // QueryInterface probably for IID_IUNKNOWN
  118. HRESULT hr = pInsideCOM->QueryInterface(riid, ppv);
  119. pInsideCOM->Release();
  120. return hr;
  121. }
  122. HRESULT CFactory::LockServer(BOOL bLock)
  123. {
  124. if(bLock)
  125. InterlockedIncrement(&g_cServerLocks);
  126. else
  127. InterlockedDecrement(&g_cServerLocks);
  128. return S_OK;
  129. }
  130. HRESULT __stdcall DllCanUnloadNow()
  131. {
  132. cout << "Component: DllCanUnloadNow() " << (g_cServerLocks == 0 && g_cComponents == 0 ? "Yes" : "No") << endl;
  133. if(g_cServerLocks == 0 && g_cComponents == 0)
  134. return S_OK;
  135. else
  136. return S_FALSE;
  137. }
  138. HRESULT __stdcall DllGetClassObject(REFCLSID clsid, REFIID riid, void** ppv)
  139. {
  140. cout << "Component: DllGetClassObject" << endl;
  141. if(clsid != CLSID_InsideCOM)
  142. return CLASS_E_CLASSNOTAVAILABLE;
  143. CFactory* pFactory = new CFactory;
  144. if(pFactory == NULL)
  145. return E_OUTOFMEMORY;
  146. // QueryInterface probably for IClassFactory
  147. HRESULT hr = pFactory->QueryInterface(riid, ppv);
  148. pFactory->Release();
  149. return hr;
  150. }
  151. HRESULT __stdcall DllRegisterServer()
  152. {
  153. char DllPath[512];
  154. OLECHAR wDllPath[512];
  155. GetModuleFileName(g_hInstance, DllPath, 512);
  156. mbstowcs(wDllPath, DllPath, 512);
  157. ITypeLib* pTypeLib;
  158. HRESULT hr = LoadTypeLibEx(wDllPath, REGKIND_REGISTER, &pTypeLib);
  159. if(FAILED(hr))
  160. return hr;
  161. pTypeLib->Release();
  162. // Adjust the threading model here
  163. return RegisterServer("component.dll", CLSID_InsideCOM, "Inside COM+ Sample", "Component.InsideCOM", "Component.InsideCOM.1", "Apartment");
  164. }
  165. HRESULT __stdcall DllUnregisterServer()
  166. {
  167. HRESULT hr = UnRegisterTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, SYS_WIN32);
  168. if(FAILED(hr))
  169. return hr;
  170. return UnregisterServer(CLSID_InsideCOM, "Component.InsideCOM", "Component.InsideCOM.1");
  171. }
  172. BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, void* pvReserved)
  173. {
  174. cout << "Inprocess: DllMain() Reason = ";
  175. switch(fdwReason)
  176. {
  177. case DLL_PROCESS_ATTACH:
  178. cout << "DLL loaded";
  179. break;
  180. case DLL_THREAD_ATTACH:
  181. cout << "thread created";
  182. break;
  183. case DLL_PROCESS_DETACH:
  184. cout << "DLL unloaded";
  185. break;
  186. case DLL_THREAD_DETACH:
  187. cout << "thread destroyed";
  188. break;
  189. }
  190. cout << " Called on ThreadID = " << GetCurrentThreadId() << endl;
  191. g_hInstance = hInstDLL;
  192. return TRUE;
  193. }