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

Windows编程

开发平台:

Visual C++

  1. // component.cpp
  2. #include <iostream.h>
  3. #include "Componentcomponent.h" // Generated by MIDL
  4. #include "registry.h" // Add This!!!
  5. HINSTANCE g_hInstance;
  6. long g_cComponents = 0;
  7. long g_cServerLocks = 0;
  8. class CInsideCOM : public ISum, public ISupportErrorInfo
  9. {
  10. public:
  11. // IUnknown
  12. ULONG __stdcall AddRef();
  13. ULONG __stdcall Release();
  14. HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);
  15. // ISum
  16. HRESULT __stdcall Sum(int x, int y, int* retval);
  17. // ISupportErrorInfo
  18. HRESULT __stdcall InterfaceSupportsErrorInfo(REFIID riid);
  19. CInsideCOM() : m_cRef(1) { g_cComponents++; }
  20. ~CInsideCOM() { cout << "Component: CInsideCOM::~CInsideCOM()" << endl, g_cComponents--; }
  21. private:
  22. ULONG m_cRef;
  23. };
  24. ULONG CInsideCOM::AddRef()
  25. {
  26. cout << "Component: CInsideCOM::AddRef() m_cRef = " << m_cRef + 1 << endl;
  27. return ++m_cRef;
  28. }
  29. ULONG CInsideCOM::Release()
  30. {
  31. cout << "Component: CInsideCOM::Release() m_cRef = " << m_cRef - 1 << endl;
  32. if(--m_cRef != 0)
  33. return m_cRef;
  34. delete this;
  35. return 0;
  36. }
  37. HRESULT CInsideCOM::QueryInterface(REFIID riid, void** ppv)
  38. {
  39. if(riid == IID_IUnknown)
  40. {
  41. cout << "Component: CInsideCOM::QueryInterface() for IUnknown returning " << this << endl;
  42. *ppv = reinterpret_cast<IUnknown*>(this);
  43. }
  44. else if(riid == IID_ISum)
  45. {
  46. cout << "Component: CInsideCOM::QueryInterface() for ISum returning " << this << endl;
  47. *ppv = (ISum*)this;
  48. }
  49. else if(riid == IID_ISupportErrorInfo)
  50. {
  51. cout << "Component: CInsideCOM::QueryInterface() for ISum returning " << this << endl;
  52. *ppv = (ISupportErrorInfo*)this;
  53. }
  54. else 
  55. {
  56. *ppv = NULL;
  57. return E_NOINTERFACE;
  58. }
  59. AddRef();
  60. return S_OK;
  61. }
  62. HRESULT CInsideCOM::InterfaceSupportsErrorInfo(REFIID riid)
  63. {
  64. if(riid == IID_ISum)
  65. return S_OK;
  66. else
  67. return S_FALSE;
  68. }
  69. HRESULT CInsideCOM::Sum(int x, int y, int* retval)
  70. {
  71. if(x < 0 || y < 0)
  72. {
  73. ICreateErrorInfo* pCreateErrorInfo;
  74. CreateErrorInfo(&pCreateErrorInfo);
  75. pCreateErrorInfo->SetDescription(L"Negative numbers not allowed");
  76. pCreateErrorInfo->SetGUID(IID_ISum);
  77. pCreateErrorInfo->SetSource(L"Component.InsideCOM");
  78. // If you support a help file add these lines...
  79. // pCreateErrorInfo->SetHelpFile(L"c:componentmyhelpfile.hlp");
  80. // pCreateErrorInfo->SetHelpContext(234);
  81. IErrorInfo* pErrorInfo;
  82. pCreateErrorInfo->QueryInterface(IID_IErrorInfo, (void**)&pErrorInfo);
  83. SetErrorInfo(0, pErrorInfo);
  84. pErrorInfo->Release();
  85. pCreateErrorInfo->Release();
  86. return E_INVALIDARG;
  87. }
  88. cout << "Component: CInsideCOM::Sum() " << x << " + " << y << " = " << x + y << endl;
  89. *retval = x + y;
  90. return S_OK;
  91. }
  92. class CFactory : public IClassFactory
  93. {
  94. public:
  95. // IUnknown
  96. ULONG __stdcall AddRef();
  97. ULONG __stdcall Release();
  98. HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);
  99. // IClassFactory
  100. HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID iid, void** ppv);
  101. HRESULT __stdcall LockServer(BOOL bLock);
  102. CFactory() : m_cRef(1) { }
  103. ~CFactory() { }
  104. private:
  105. ULONG m_cRef;
  106. };
  107. ULONG CFactory::AddRef()
  108. {
  109. cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
  110. return ++m_cRef;
  111. }
  112. ULONG CFactory::Release()
  113. {
  114. cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
  115. if(--m_cRef != 0)
  116. return m_cRef;
  117. delete this;
  118. return 0;
  119. }
  120. HRESULT CFactory::QueryInterface(REFIID iid, void** ppv)
  121. {
  122. if((iid == IID_IUnknown) || (iid == IID_IClassFactory))
  123. {
  124. cout << "Component: CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
  125. *ppv = (IClassFactory *)this;
  126. }
  127. else
  128. {
  129. *ppv = NULL;
  130. return E_NOINTERFACE;
  131. }
  132. AddRef();
  133. return S_OK;
  134. }
  135. HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv)
  136. {
  137. if(pUnknownOuter != NULL)
  138. return CLASS_E_NOAGGREGATION;
  139. CInsideCOM *pInsideCOM = new CInsideCOM;
  140. cout << "Component: CFactory::CreateInstance() " << pInsideCOM << endl;
  141. if(pInsideCOM == NULL)
  142. return E_OUTOFMEMORY;
  143. // QueryInterface probably for IID_IUnknown
  144. HRESULT hr = pInsideCOM->QueryInterface(riid, ppv);
  145. pInsideCOM->Release();
  146. return hr;
  147. }
  148. HRESULT CFactory::LockServer(BOOL fLock)
  149. {
  150. if(fLock)
  151. g_cServerLocks++;
  152. else
  153. g_cServerLocks--;
  154. return S_OK;
  155. }
  156. HRESULT __stdcall DllCanUnloadNow()
  157. {
  158. cout << "Component: DllCanUnloadNow() " << (g_cServerLocks == 0 && g_cComponents == 0 ? "Yes" : "No") << endl;
  159. if(g_cServerLocks == 0 && g_cComponents == 0)
  160. return S_OK;
  161. else
  162. return S_FALSE;
  163. }
  164. HRESULT __stdcall DllGetClassObject(REFCLSID clsid, REFIID iid, void** ppv)
  165. {
  166. cout << "Component: DllGetClassObject" << endl;
  167. if(clsid != CLSID_InsideCOM)
  168. return CLASS_E_CLASSNOTAVAILABLE;
  169. CFactory* pFactory = new CFactory;
  170. if(pFactory == NULL)
  171. return E_OUTOFMEMORY;
  172. // QueryInterface probably for IClassFactory
  173. HRESULT hr = pFactory->QueryInterface(iid, ppv);
  174. pFactory->Release();
  175. return hr;
  176. }
  177. HRESULT __stdcall DllRegisterServer()
  178. {
  179. char DllPath[256];
  180. OLECHAR wDllPath[256];
  181. GetModuleFileName(g_hInstance, DllPath, 256);
  182. mbstowcs(wDllPath, DllPath, 256);
  183. ITypeLib* pTypeLib;
  184. HRESULT hr = LoadTypeLibEx(wDllPath, REGKIND_REGISTER, &pTypeLib);
  185. if(FAILED(hr))
  186. return hr;
  187. pTypeLib->Release();
  188. return RegisterServer("component.dll", CLSID_InsideCOM, "Inside COM Sample", "Component.InsideCOM", "Component.InsideCOM.1", "Apartment");
  189. }
  190. HRESULT __stdcall DllUnregisterServer()
  191. {
  192. HRESULT hr = UnRegisterTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, SYS_WIN32);
  193. if(FAILED(hr))
  194. return hr;
  195. return UnregisterServer(CLSID_InsideCOM, "Component.InsideCOM", "Component.InsideCOM.1");
  196. }
  197. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, void* pv)
  198. {
  199. g_hInstance = hInstance;
  200. return TRUE;
  201. }