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

Windows编程

开发平台:

Visual C++

  1. // component.cpp
  2. #include <iostream.h>
  3. #include <stdio.h>
  4. #include "Componentcomponent.h" // Generated by MIDL
  5. #include "registry.h"  // Need this
  6. HINSTANCE g_hInstance;
  7. long g_cComponents = 0;
  8. long g_cServerLocks = 0;
  9. struct num
  10. {
  11. num(long value) : number(value), pNextNum(0) { };
  12. long number;
  13. num* pNextNum;
  14. };
  15. class CInsideCOM : public INumbers, public IEnumVARIANT
  16. {
  17. public:
  18. // IUnknown
  19. ULONG __stdcall AddRef();
  20. ULONG __stdcall Release();
  21. HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);
  22. // IEnumVARIANT
  23.     HRESULT __stdcall Next(ULONG celt, VARIANT* rgVar, ULONG* pCeltFetched);
  24.     HRESULT __stdcall Skip(ULONG celt);
  25.     HRESULT __stdcall Reset();
  26.     HRESULT __stdcall Clone(IEnumVARIANT** ppEnum);
  27. // IDispatch
  28. HRESULT __stdcall GetTypeInfoCount(UINT* pCountTypeInfo);
  29. HRESULT __stdcall GetTypeInfo(UINT iTypeInfo, LCID lcid, ITypeInfo** ppITypeInfo);
  30. HRESULT __stdcall GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId);
  31. HRESULT __stdcall Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr);
  32. // INumbers
  33. HRESULT __stdcall get_Item(long n, long* pVal);
  34. HRESULT __stdcall get__NewEnum(IUnknown** pVal);
  35. HRESULT __stdcall get_Count(long *pVal);
  36. HRESULT __stdcall Add(long Val);
  37. HRESULT __stdcall Remove(long index);
  38. CInsideCOM();
  39. ~CInsideCOM() { cout << "Component: CInsideCOM::~CInsideCOM()" << endl, g_cComponents--; }
  40. bool Init(void);
  41. private:
  42. ULONG m_cRef;
  43. ITypeInfo* m_pTypeInfo;
  44. num* m_pFirstNum;
  45. num* m_pCurrentNum;
  46. };
  47. CInsideCOM::CInsideCOM() : m_cRef(1), m_pCurrentNum(0)
  48. {
  49. g_cComponents++;
  50. m_pFirstNum = 0;
  51. m_pCurrentNum = 0;
  52. }
  53. HRESULT CInsideCOM::Next(ULONG celt, VARIANT* rgVar, ULONG* pCeltFetched)
  54. {
  55. // Capture walking past the end of the list from a previous call
  56. if(!m_pCurrentNum)
  57. {
  58. if(pCeltFetched != 0)
  59. *pCeltFetched = 0;
  60. return S_FALSE;
  61. }
  62. for(unsigned count = 0; count < celt; count++)
  63. {
  64. // Capture walking past the end of the list from a previous loop iteration
  65. if(!m_pCurrentNum)
  66. {
  67. if(pCeltFetched != 0)
  68. *pCeltFetched = count+1;
  69. return S_FALSE;
  70. }
  71. VariantInit(&rgVar[count]);
  72. rgVar[count].intVal = m_pCurrentNum->number;
  73. rgVar[count].vt = VT_I4;
  74. m_pCurrentNum = m_pCurrentNum->pNextNum;
  75. }
  76. if(pCeltFetched != 0)
  77. *pCeltFetched = count+1; 
  78. return S_OK;
  79. }
  80. HRESULT CInsideCOM::Skip(ULONG celt)
  81. {
  82. for(unsigned count = 0; count < celt; count++)
  83. if(m_pCurrentNum->pNextNum)
  84. m_pCurrentNum = m_pCurrentNum->pNextNum;
  85. else
  86. break;
  87. return S_OK;
  88. }
  89. HRESULT CInsideCOM::Reset()
  90. {
  91. m_pCurrentNum = m_pFirstNum;
  92. return S_OK;
  93. }
  94. HRESULT CInsideCOM::Clone(IEnumVARIANT** ppEnum)
  95. {
  96. *ppEnum = 0;
  97. return E_NOTIMPL;
  98. }
  99. HRESULT CInsideCOM::Add(long Val)
  100. {
  101. if(!m_pFirstNum)
  102. {
  103. m_pFirstNum = new num(Val);
  104. m_pCurrentNum = m_pFirstNum;
  105. return S_OK;
  106. }
  107. num* pNum = m_pFirstNum;
  108. while(pNum->pNextNum)
  109. pNum = pNum->pNextNum;
  110. pNum->pNextNum = new num(Val);
  111. return S_OK;
  112. }
  113. HRESULT CInsideCOM::Remove(long index)
  114. {
  115. num* pPreviousNum = 0;
  116. num* pNum = m_pFirstNum;
  117. if(!pNum)
  118. return E_INVALIDARG;
  119. if(index < 1)
  120. return E_INVALIDARG;
  121. // deleting the first number
  122. if(index - 1 == 0)
  123. {
  124. // are the first and current num pointing to the same element
  125. if(m_pFirstNum == m_pCurrentNum)
  126. m_pCurrentNum = m_pFirstNum->pNextNum;
  127. m_pFirstNum = m_pFirstNum->pNextNum;
  128. delete pNum;
  129. return S_OK;
  130. }
  131. for(long count = 0; count < index-1; count++)
  132. {
  133. pPreviousNum = pNum;
  134. if(pNum->pNextNum)
  135. pNum = pNum->pNextNum;
  136. else
  137. return E_INVALIDARG;
  138. }
  139. pPreviousNum->pNextNum = pNum->pNextNum;
  140. delete pNum;
  141. return S_OK;
  142. }
  143. HRESULT CInsideCOM::get__NewEnum(IUnknown** pVal)
  144. {
  145. return QueryInterface(IID_IUnknown, (void**)pVal);
  146. }
  147. HRESULT CInsideCOM::get_Item(long n, long* pVal)
  148. {
  149. num* pNum = m_pFirstNum;
  150. if(!pNum)
  151. return E_INVALIDARG;
  152. for(long count = 0; count < n-1; count++)
  153. if(pNum->pNextNum)
  154. pNum = pNum->pNextNum;
  155. else
  156. return E_INVALIDARG;
  157. *pVal = pNum->number;
  158. return S_OK;
  159. }
  160. HRESULT CInsideCOM::get_Count(long *pVal)
  161. {
  162. num* pNum = m_pFirstNum;
  163. if(!pNum)
  164. {
  165. *pVal = 0;
  166. return S_OK;
  167. }
  168. long count = 0;
  169. while(pNum->pNextNum)
  170. {
  171. pNum = pNum->pNextNum;
  172. count++;
  173. }
  174. *pVal = count+1;
  175. return S_OK;
  176. }
  177. HRESULT CInsideCOM::GetTypeInfoCount(UINT* pCountTypeInfo)
  178. {
  179. *pCountTypeInfo = 1;
  180. return S_OK;
  181. }
  182. HRESULT CInsideCOM::GetTypeInfo(UINT iTypeInfo, LCID lcid, ITypeInfo** ppITypeInfo)
  183. {
  184. *ppITypeInfo = NULL;
  185. if(iTypeInfo != 0)
  186. return DISP_E_BADINDEX;
  187. m_pTypeInfo->AddRef();
  188. *ppITypeInfo = m_pTypeInfo;
  189. return S_OK;
  190. }
  191. HRESULT CInsideCOM::GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId)
  192. {
  193. if(riid != IID_NULL)
  194. return DISP_E_UNKNOWNINTERFACE;
  195. return DispGetIDsOfNames(m_pTypeInfo, rgszNames, cNames, rgDispId);
  196. }
  197. HRESULT CInsideCOM::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
  198. {
  199. if(riid != IID_NULL)
  200. return DISP_E_UNKNOWNINTERFACE;
  201. return DispInvoke(this, m_pTypeInfo, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr); 
  202. }
  203. bool CInsideCOM::Init(void)
  204. {
  205. ITypeLib* pTypeLib;
  206. if(FAILED(LoadRegTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, &pTypeLib)))
  207. return false;
  208. HRESULT hr = pTypeLib->GetTypeInfoOfGuid(IID_INumbers, &m_pTypeInfo);
  209. pTypeLib->Release();
  210. if(FAILED(hr))
  211. return false;
  212. return true;
  213. }
  214. ULONG CInsideCOM::AddRef()
  215. {
  216. cout << "Component: CInsideCOM::AddRef() m_cRef = " << m_cRef + 1 << endl;
  217. return ++m_cRef;
  218. }
  219. ULONG CInsideCOM::Release()
  220. {
  221. cout << "Component: CInsideCOM::Release() m_cRef = " << m_cRef - 1 << endl;
  222. if(--m_cRef != 0)
  223. return m_cRef;
  224. m_pTypeInfo->Release();
  225. delete this;
  226. return 0;
  227. }
  228. HRESULT CInsideCOM::QueryInterface(REFIID riid, void** ppv)
  229. {
  230. if(riid == IID_IUnknown)
  231. *ppv = reinterpret_cast<IUnknown*>(this);
  232. else if(riid == IID_INumbers)
  233. *ppv = (INumbers*)this;
  234. else if(riid == IID_IDispatch)
  235. *ppv = (IDispatch*)this;
  236. else if(riid == IID_IEnumVARIANT)
  237. *ppv = (IEnumVARIANT*)this;
  238. else 
  239. {
  240. *ppv = NULL;
  241. return E_NOINTERFACE;
  242. }
  243. AddRef();
  244. return S_OK;
  245. }
  246. class CFactory : public IClassFactory
  247. {
  248. public:
  249. // IUnknown
  250. ULONG __stdcall AddRef();
  251. ULONG __stdcall Release();
  252. HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);
  253. // IClassFactory
  254. HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID iid, void** ppv);
  255. HRESULT __stdcall LockServer(BOOL bLock);
  256. CFactory() : m_cRef(1) { }
  257. ~CFactory() { }
  258. private:
  259. long m_cRef;
  260. };
  261. ULONG CFactory::AddRef()
  262. {
  263. cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
  264. return ++m_cRef;
  265. }
  266. ULONG CFactory::Release()
  267. {
  268. cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
  269. if(--m_cRef != 0)
  270. return m_cRef;
  271. delete this;
  272. return 0;
  273. }
  274. HRESULT CFactory::QueryInterface(REFIID iid, void** ppv)
  275. {
  276. if((iid == IID_IUnknown) || (iid == IID_IClassFactory))
  277. {
  278. cout << "Component: CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
  279. *ppv = (IClassFactory *)this;
  280. }
  281. else
  282. {
  283. *ppv = NULL;
  284. return E_NOINTERFACE;
  285. }
  286. AddRef();
  287. return S_OK;
  288. }
  289. HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID iid, void** ppv)
  290. {
  291. if(pUnknownOuter != NULL)
  292. return CLASS_E_NOAGGREGATION;
  293. CInsideCOM *pInsideCOM = new CInsideCOM;
  294. cout << "Component: CFactory::CreateInstance() " << pInsideCOM << endl;
  295. if(pInsideCOM == NULL)
  296. return E_OUTOFMEMORY;
  297. // Call the Init method to load the type information
  298. pInsideCOM->Init();
  299. HRESULT hr = pInsideCOM->QueryInterface(iid, ppv);
  300. pInsideCOM->Release();
  301. return hr;
  302. }
  303. HRESULT CFactory::LockServer(BOOL bLock)
  304. {
  305. if(bLock)
  306. g_cServerLocks++;
  307. else
  308. g_cServerLocks--;
  309. return S_OK;
  310. }
  311. HRESULT __stdcall DllCanUnloadNow()
  312. {
  313. cout << "Component: DllCanUnloadNow() " << (g_cServerLocks == 0 && g_cComponents == 0 ? "Yes" : "No") << endl;
  314. if(g_cServerLocks == 0 && g_cComponents == 0)
  315. return S_OK;
  316. else
  317. return S_FALSE;
  318. }
  319. HRESULT __stdcall DllGetClassObject(REFCLSID clsid, REFIID iid, void** ppv)
  320. {
  321. cout << "Component: DllGetClassObject" << endl;
  322. if(clsid != CLSID_InsideCOM)
  323. return CLASS_E_CLASSNOTAVAILABLE;
  324. CFactory* pFactory = new CFactory;
  325. if(pFactory == NULL)
  326. return E_OUTOFMEMORY;
  327. // QueryInterface probably for IClassFactory
  328. HRESULT hr = pFactory->QueryInterface(iid, ppv);
  329. pFactory->Release();
  330. return hr;
  331. }
  332. HRESULT __stdcall DllRegisterServer()
  333. {
  334. char DllPath[256];
  335. OLECHAR wDllPath[256];
  336. GetModuleFileName(g_hInstance, DllPath, 256);
  337. mbstowcs(wDllPath, DllPath, 256);
  338. ITypeLib* pTypeLib;
  339. HRESULT hr = LoadTypeLibEx(wDllPath, REGKIND_REGISTER, &pTypeLib);
  340. if(FAILED(hr))
  341. return hr;
  342. pTypeLib->Release();
  343. return RegisterServer("component.dll", CLSID_InsideCOM, "Inside COM+ Sample", "Component.InsideCOM", "Component.InsideCOM.1", NULL);
  344. }
  345. HRESULT __stdcall DllUnregisterServer()
  346. {
  347. UnRegisterTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, SYS_WIN32);
  348. return UnregisterServer(CLSID_InsideCOM, "Component.InsideCOM", "Component.InsideCOM.1");
  349. }
  350. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, void* pv)
  351. {
  352. g_hInstance = hInstance;
  353. return TRUE;
  354. }