Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

555 lines
13 KiB

  1. // This is a part of the Active Template Library.
  2. // Copyright (C) 1996-2001 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Active Template Library Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Active Template Library product.
  10. #ifndef __ATLBASE_INL__
  11. #define __ATLBASE_INL__
  12. #pragma once
  13. #ifndef __ATLBASE_H__
  14. #error atlbase.inl requires atlbase.h to be included first
  15. #endif
  16. namespace ATL
  17. {
  18. extern CAtlWinModule _AtlWinModule;
  19. extern CAtlComModule _AtlComModule;
  20. #define _ATLCOMMODULE ATL::_AtlComModule
  21. #define _ATLWINMODULE ATL::_AtlWinModule
  22. inline ATL_DEPRECATED HRESULT AtlModuleRegisterClassObjects(_ATL_MODULE* /*pM*/, DWORD dwClsContext, DWORD dwFlags)
  23. {
  24. return AtlComModuleRegisterClassObjects(&_AtlComModule, dwClsContext, dwFlags);
  25. }
  26. inline ATL_DEPRECATED HRESULT AtlModuleRevokeClassObjects(_ATL_MODULE* /*pM*/)
  27. {
  28. return AtlComModuleRevokeClassObjects(&_AtlComModule);
  29. }
  30. inline ATL_DEPRECATED HRESULT AtlModuleGetClassObject(_ATL_MODULE* /*pM*/, REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  31. {
  32. return AtlComModuleGetClassObject(&_AtlComModule, rclsid, riid, ppv);
  33. }
  34. inline ATL_DEPRECATED HRESULT AtlModuleRegisterServer(_ATL_MODULE* /*pM*/, BOOL bRegTypeLib, const CLSID* pCLSID = NULL)
  35. {
  36. return AtlComModuleRegisterServer(&_AtlComModule, bRegTypeLib, pCLSID);
  37. }
  38. inline ATL_DEPRECATED HRESULT AtlModuleUnregisterServer(_ATL_MODULE* /*pM*/, const CLSID* pCLSID = NULL)
  39. {
  40. return AtlComModuleUnregisterServer(&_AtlComModule, FALSE, pCLSID);
  41. }
  42. inline ATL_DEPRECATED HRESULT AtlModuleUnregisterServerEx(_ATL_MODULE* /*pM*/, BOOL bUnRegTypeLib, const CLSID* pCLSID = NULL)
  43. {
  44. return AtlComModuleUnregisterServer(&_AtlComModule, bUnRegTypeLib, pCLSID);
  45. }
  46. inline ATL_DEPRECATED HRESULT AtlModuleUpdateRegistryFromResourceD(_ATL_MODULE* /*pM*/, LPCOLESTR lpszRes,
  47. BOOL bRegister, struct _ATL_REGMAP_ENTRY* pMapEntries, IRegistrar* pReg = NULL)
  48. {
  49. return AtlUpdateRegistryFromResourceD(_AtlBaseModule.GetModuleInstance(), lpszRes, bRegister, pMapEntries, pReg);
  50. }
  51. inline ATL_DEPRECATED HRESULT AtlModuleRegisterTypeLib(_ATL_MODULE* /*pM*/, LPCOLESTR lpszIndex)
  52. {
  53. return AtlRegisterTypeLib(_AtlComModule.m_hInstTypeLib, lpszIndex);
  54. }
  55. inline ATL_DEPRECATED HRESULT AtlModuleUnRegisterTypeLib(_ATL_MODULE* /*pM*/, LPCOLESTR lpszIndex)
  56. {
  57. return AtlUnRegisterTypeLib(_AtlComModule.m_hInstTypeLib, lpszIndex);
  58. }
  59. inline ATL_DEPRECATED HRESULT AtlModuleLoadTypeLib(_ATL_MODULE* /*pM*/, LPCOLESTR lpszIndex, BSTR* pbstrPath, ITypeLib** ppTypeLib)
  60. {
  61. return AtlLoadTypeLib(_AtlComModule.m_hInstTypeLib, lpszIndex, pbstrPath, ppTypeLib);
  62. }
  63. inline ATL_DEPRECATED HRESULT AtlModuleInit(_ATL_MODULE* /*pM*/, _ATL_OBJMAP_ENTRY* /*p*/, HINSTANCE /*h*/)
  64. {
  65. return S_OK;
  66. }
  67. inline ATL_DEPRECATED HRESULT AtlModuleTerm(_ATL_MODULE* /*pM*/)
  68. {
  69. return S_OK;
  70. }
  71. inline ATL_DEPRECATED void AtlModuleAddCreateWndData(_ATL_MODULE* /*pM*/, _AtlCreateWndData* pData, void* pObject)
  72. {
  73. AtlWinModuleAddCreateWndData(&_AtlWinModule, pData, pObject);
  74. }
  75. inline ATL_DEPRECATED void* AtlModuleExtractCreateWndData(_ATL_MODULE* /*pM*/)
  76. {
  77. return AtlWinModuleExtractCreateWndData(&_AtlWinModule);
  78. }
  79. template <class T>
  80. inline HRESULT CAtlModuleT<T>::RegisterServer(BOOL bRegTypeLib /*= FALSE*/, const CLSID* pCLSID /*= NULL*/)
  81. {
  82. pCLSID;
  83. bRegTypeLib;
  84. HRESULT hr = S_OK;
  85. #ifndef _ATL_NO_COM_SUPPORT
  86. hr = _AtlComModule.RegisterServer(bRegTypeLib, pCLSID);
  87. #endif // _ATL_NO_COM_SUPPORT
  88. #ifndef _ATL_NO_PERF_SUPPORT
  89. if (SUCCEEDED(hr) && _pPerfRegFunc != NULL)
  90. hr = (*_pPerfRegFunc)(_AtlBaseModule.m_hInst);
  91. #endif
  92. return hr;
  93. }
  94. template <class T>
  95. inline HRESULT CAtlModuleT<T>::UnregisterServer(BOOL bUnRegTypeLib, const CLSID* pCLSID /*= NULL*/)
  96. {
  97. bUnRegTypeLib;
  98. pCLSID;
  99. HRESULT hr = S_OK;
  100. #ifndef _ATL_NO_PERF_SUPPORT
  101. if (_pPerfUnRegFunc != NULL)
  102. hr = (*_pPerfUnRegFunc)();
  103. #endif
  104. #ifndef _ATL_NO_COM_SUPPORT
  105. if (SUCCEEDED(hr))
  106. hr = _AtlComModule.UnregisterServer(bUnRegTypeLib, pCLSID);
  107. #endif // _ATL_NO_COM_SUPPORT
  108. return hr;
  109. }
  110. template <class T>
  111. inline CAtlDllModuleT<T>::CAtlDllModuleT() throw()
  112. {
  113. _AtlComModule.ExecuteObjectMain(true);
  114. }
  115. template <class T>
  116. inline CAtlDllModuleT<T>::~CAtlDllModuleT()
  117. {
  118. _AtlComModule.ExecuteObjectMain(false);
  119. }
  120. template <class T>
  121. inline HRESULT CAtlDllModuleT<T>::GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  122. {
  123. return AtlComModuleGetClassObject(&_AtlComModule, rclsid, riid, ppv);
  124. }
  125. template <class T>
  126. inline CAtlExeModuleT<T>::CAtlExeModuleT() throw()
  127. #ifndef _ATL_NO_COM_SUPPORT
  128. : m_dwMainThreadID(::GetCurrentThreadId()),
  129. m_dwTimeOut(5000),
  130. m_dwPause(1000),
  131. m_hEventShutdown(NULL),
  132. m_bDelayShutdown(true)
  133. #endif // _ATL_NO_COM_SUPPORT
  134. {
  135. HRESULT hr = T::InitializeCom();
  136. if (FAILED(hr))
  137. {
  138. CAtlBaseModule::m_bInitFailed = true;
  139. return;
  140. }
  141. #if !defined(_ATL_NO_COM_SUPPORT)
  142. _AtlComModule.ExecuteObjectMain(true);
  143. #endif // !defined(_ATL_NO_COM_SUPPORT)
  144. }
  145. template <class T>
  146. inline CAtlExeModuleT<T>::~CAtlExeModuleT()
  147. {
  148. #ifndef _ATL_NO_COM_SUPPORT
  149. _AtlComModule.ExecuteObjectMain(false);
  150. #endif
  151. // Call term functions before COM is uninitialized
  152. Term();
  153. #ifndef _ATL_NO_COM_SUPPORT
  154. // Clean up AtlComModule before COM is uninitialized
  155. _AtlComModule.Term();
  156. #endif // _ATL_NO_COM_SUPPORT
  157. T::UninitializeCom();
  158. }
  159. template <class T>
  160. inline HRESULT CAtlExeModuleT<T>::RegisterClassObjects(DWORD dwClsContext, DWORD dwFlags)
  161. {
  162. return AtlComModuleRegisterClassObjects(&_AtlComModule, dwClsContext, dwFlags);
  163. }
  164. template <class T>
  165. inline HRESULT CAtlExeModuleT<T>::RevokeClassObjects()
  166. {
  167. return AtlComModuleRevokeClassObjects(&_AtlComModule);
  168. }
  169. inline ULONG _QIThunk::Release()
  170. {
  171. ATLASSERT(m_pUnk != NULL);
  172. if (m_bBreak)
  173. DebugBreak();
  174. ATLASSERT(m_dwRef > 0);
  175. // save copies of member variables we wish to use after the InterlockedDecrement
  176. UINT nIndex = m_nIndex;
  177. IUnknown* pUnk = m_pUnk;
  178. IID iid = m_iid;
  179. LPCTSTR lpszClassName = m_lpszClassName;
  180. bool bNonAddRefThunk = m_bNonAddRefThunk;
  181. ULONG l = InterlockedDecrement(&m_dwRef);
  182. TCHAR buf[512];
  183. wsprintf(buf, _T("QIThunk - %-10d\tRelease :\tObject = 0x%08x\tRefcount = %d\t"), nIndex, pUnk, l);
  184. OutputDebugString(buf);
  185. AtlDumpIID(iid, lpszClassName, S_OK);
  186. bool bDeleteThunk = (l == 0 && !bNonAddRefThunk);
  187. pUnk->Release();
  188. if (bDeleteThunk)
  189. _AtlDebugInterfacesModule.DeleteThunk(this);
  190. return l;
  191. }
  192. inline HINSTANCE& CComModule::get_m_hInstTypeLib()
  193. {
  194. return _AtlComModule.m_hInstTypeLib;
  195. }
  196. inline void CComModule::put_m_hInstTypeLib(HINSTANCE h)
  197. {
  198. _AtlComModule.m_hInstTypeLib = h;
  199. }
  200. inline HINSTANCE CComModule::GetTypeLibInstance()
  201. {
  202. return _AtlComModule.m_hInstTypeLib;
  203. }
  204. inline CRITICAL_SECTION& CComModule::get_m_csWindowCreate()
  205. {
  206. return _AtlWinModule.m_csWindowCreate.m_sec;
  207. }
  208. inline CRITICAL_SECTION& CComModule::get_m_csObjMap()
  209. {
  210. return _AtlComModule.m_csObjMap.m_sec;
  211. }
  212. inline CRITICAL_SECTION& CComModule::get_m_csStaticDataInit()
  213. {
  214. return m_csStaticDataInitAndTypeInfo.m_sec;
  215. }
  216. inline _AtlCreateWndData*& CComModule::get_m_pCreateWndList()
  217. {
  218. return _AtlWinModule.m_pCreateWndList;
  219. }
  220. inline void CComModule::put_m_pCreateWndList(_AtlCreateWndData* p)
  221. {
  222. _AtlWinModule.m_pCreateWndList = p;
  223. }
  224. #ifdef _ATL_DEBUG_INTERFACES
  225. inline UINT& CComModule::get_m_nIndexQI()
  226. {
  227. return _AtlDebugInterfacesModule.m_nIndexQI;
  228. }
  229. inline void CComModule::put_m_nIndexQI(UINT nIndex)
  230. {
  231. _AtlDebugInterfacesModule.m_nIndexQI = nIndex;
  232. }
  233. inline UINT& CComModule::get_m_nIndexBreakAt()
  234. {
  235. return _AtlDebugInterfacesModule.m_nIndexBreakAt;
  236. }
  237. inline void CComModule::put_m_nIndexBreakAt(UINT nIndex)
  238. {
  239. _AtlDebugInterfacesModule.m_nIndexBreakAt = nIndex;
  240. }
  241. inline CSimpleArray<_QIThunk*>* CComModule::get_m_paThunks()
  242. {
  243. return &_AtlDebugInterfacesModule.m_aThunks;
  244. }
  245. inline HRESULT CComModule::AddThunk(IUnknown** pp, LPCTSTR lpsz, REFIID iid)
  246. {
  247. return _AtlDebugInterfacesModule.AddThunk(pp, lpsz, iid);
  248. }
  249. inline HRESULT CComModule::AddNonAddRefThunk(IUnknown* p, LPCTSTR lpsz, IUnknown** ppThunkRet)
  250. {
  251. return _AtlDebugInterfacesModule.AddNonAddRefThunk(p, lpsz, ppThunkRet);
  252. }
  253. inline void CComModule::DeleteNonAddRefThunk(IUnknown* pUnk)
  254. {
  255. _AtlDebugInterfacesModule.DeleteNonAddRefThunk(pUnk);
  256. }
  257. inline void CComModule::DeleteThunk(_QIThunk* p)
  258. {
  259. _AtlDebugInterfacesModule.DeleteThunk(p);
  260. }
  261. inline bool CComModule::DumpLeakedThunks()
  262. {
  263. return _AtlDebugInterfacesModule.DumpLeakedThunks();
  264. }
  265. #endif // _ATL_DEBUG_INTERFACES
  266. inline HRESULT CComModule::Init(_ATL_OBJMAP_ENTRY* p, HINSTANCE /*h*/, const GUID* plibid)
  267. {
  268. if (plibid != NULL)
  269. m_libid = *plibid;
  270. _ATL_OBJMAP_ENTRY* pEntry;
  271. if (p != (_ATL_OBJMAP_ENTRY*)-1)
  272. {
  273. m_pObjMap = p;
  274. if (m_pObjMap != NULL)
  275. {
  276. pEntry = m_pObjMap;
  277. while (pEntry->pclsid != NULL)
  278. {
  279. pEntry->pfnObjectMain(true); //initialize class resources
  280. pEntry++;
  281. }
  282. }
  283. }
  284. for (_ATL_OBJMAP_ENTRY** ppEntry = _AtlComModule.m_ppAutoObjMapFirst; ppEntry < _AtlComModule.m_ppAutoObjMapLast; ppEntry++)
  285. {
  286. if (*ppEntry != NULL)
  287. (*ppEntry)->pfnObjectMain(true); //initialize class resources
  288. }
  289. return S_OK;
  290. }
  291. inline void CComModule::Term()
  292. {
  293. _ATL_OBJMAP_ENTRY* pEntry;
  294. if (m_pObjMap != NULL)
  295. {
  296. pEntry = m_pObjMap;
  297. while (pEntry->pclsid != NULL)
  298. {
  299. if (pEntry->pCF != NULL)
  300. pEntry->pCF->Release();
  301. pEntry->pCF = NULL;
  302. pEntry->pfnObjectMain(false); //cleanup class resources
  303. pEntry++;
  304. }
  305. }
  306. for (_ATL_OBJMAP_ENTRY** ppEntry = _AtlComModule.m_ppAutoObjMapFirst; ppEntry < _AtlComModule.m_ppAutoObjMapLast; ppEntry++)
  307. {
  308. if (*ppEntry != NULL)
  309. (*ppEntry)->pfnObjectMain(false); //cleanup class resources
  310. }
  311. CAtlModuleT<CComModule>::Term();
  312. }
  313. inline HRESULT CComModule::GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  314. {
  315. if (ppv == NULL)
  316. return E_POINTER;
  317. HRESULT hr = S_OK;
  318. _ATL_OBJMAP_ENTRY* pEntry;
  319. if (m_pObjMap != NULL)
  320. {
  321. pEntry = m_pObjMap;
  322. while (pEntry->pclsid != NULL)
  323. {
  324. if ((pEntry->pfnGetClassObject != NULL) && InlineIsEqualGUID(rclsid, *pEntry->pclsid))
  325. {
  326. if (pEntry->pCF == NULL)
  327. {
  328. CComCritSecLock<CComCriticalSection> lock(_AtlComModule.m_csObjMap, false);
  329. hr = lock.Lock();
  330. if (FAILED(hr))
  331. {
  332. ATLTRACE(atlTraceCOM, 0, _T("ERROR : Unable to lock critical section in CComModule::GetClassObject\n"));
  333. ATLASSERT(0);
  334. break;
  335. }
  336. if (pEntry->pCF == NULL)
  337. hr = pEntry->pfnGetClassObject(pEntry->pfnCreateInstance, __uuidof(IUnknown), (LPVOID*)&pEntry->pCF);
  338. }
  339. if (pEntry->pCF != NULL)
  340. hr = pEntry->pCF->QueryInterface(riid, ppv);
  341. break;
  342. }
  343. pEntry++;
  344. }
  345. }
  346. if (*ppv == NULL && hr == S_OK)
  347. hr = AtlComModuleGetClassObject(&_AtlComModule, rclsid, riid, ppv);
  348. return hr;
  349. }
  350. // Register/Revoke All Class Factories with the OS (EXE only)
  351. inline HRESULT CComModule::RegisterClassObjects(DWORD dwClsContext, DWORD dwFlags)
  352. {
  353. HRESULT hr = S_OK;
  354. _ATL_OBJMAP_ENTRY* pEntry;
  355. if (m_pObjMap != NULL)
  356. {
  357. pEntry = m_pObjMap;
  358. while (pEntry->pclsid != NULL && hr == S_OK)
  359. {
  360. hr = pEntry->RegisterClassObject(dwClsContext, dwFlags);
  361. pEntry++;
  362. }
  363. }
  364. if (hr == S_OK)
  365. hr = AtlComModuleRegisterClassObjects(&_AtlComModule, dwClsContext, dwFlags);
  366. return hr;
  367. }
  368. inline HRESULT CComModule::RevokeClassObjects()
  369. {
  370. HRESULT hr = S_OK;
  371. _ATL_OBJMAP_ENTRY* pEntry;
  372. if (m_pObjMap != NULL)
  373. {
  374. pEntry = m_pObjMap;
  375. while (pEntry->pclsid != NULL && hr == S_OK)
  376. {
  377. hr = pEntry->RevokeClassObject();
  378. pEntry++;
  379. }
  380. }
  381. if (hr == S_OK)
  382. hr = AtlComModuleRevokeClassObjects(&_AtlComModule);
  383. return hr;
  384. }
  385. // Registry support (helpers)
  386. inline HRESULT CComModule::RegisterTypeLib()
  387. {
  388. return _AtlComModule.RegisterTypeLib();
  389. }
  390. inline HRESULT CComModule::RegisterTypeLib(LPCTSTR lpszIndex)
  391. {
  392. return _AtlComModule.RegisterTypeLib(lpszIndex);
  393. }
  394. inline HRESULT CComModule::UnRegisterTypeLib()
  395. {
  396. return _AtlComModule.UnRegisterTypeLib();
  397. }
  398. inline HRESULT CComModule::UnRegisterTypeLib(LPCTSTR lpszIndex)
  399. {
  400. return _AtlComModule.UnRegisterTypeLib(lpszIndex);
  401. }
  402. inline HRESULT CComModule::RegisterServer(BOOL bRegTypeLib /*= FALSE*/, const CLSID* pCLSID /*= NULL*/)
  403. {
  404. HRESULT hr = S_OK;
  405. _ATL_OBJMAP_ENTRY* pEntry = m_pObjMap;
  406. if (pEntry != NULL)
  407. {
  408. for (;pEntry->pclsid != NULL; pEntry++)
  409. {
  410. if (pCLSID != NULL)
  411. {
  412. if (!IsEqualGUID(*pCLSID, *pEntry->pclsid))
  413. continue;
  414. }
  415. hr = pEntry->pfnUpdateRegistry(TRUE);
  416. if (FAILED(hr))
  417. break;
  418. hr = AtlRegisterClassCategoriesHelper( *pEntry->pclsid,
  419. pEntry->pfnGetCategoryMap(), TRUE );
  420. if (FAILED(hr))
  421. break;
  422. }
  423. }
  424. if (SUCCEEDED(hr))
  425. hr = CAtlModuleT<CComModule>::RegisterServer(bRegTypeLib, pCLSID);
  426. return hr;
  427. }
  428. inline HRESULT CComModule::UnregisterServer(BOOL bUnRegTypeLib, const CLSID* pCLSID /*= NULL*/)
  429. {
  430. HRESULT hr = S_OK;
  431. _ATL_OBJMAP_ENTRY* pEntry = m_pObjMap;
  432. if (pEntry != NULL)
  433. {
  434. for (;pEntry->pclsid != NULL; pEntry++)
  435. {
  436. if (pCLSID != NULL)
  437. {
  438. if (!IsEqualGUID(*pCLSID, *pEntry->pclsid))
  439. continue;
  440. }
  441. hr = AtlRegisterClassCategoriesHelper( *pEntry->pclsid,
  442. pEntry->pfnGetCategoryMap(), FALSE );
  443. if (FAILED(hr))
  444. break;
  445. hr = pEntry->pfnUpdateRegistry(FALSE); //unregister
  446. if (FAILED(hr))
  447. break;
  448. }
  449. }
  450. if (SUCCEEDED(hr))
  451. hr = CAtlModuleT<CComModule>::UnregisterServer(bUnRegTypeLib, pCLSID);
  452. return hr;
  453. }
  454. inline HRESULT CComModule::UnregisterServer(const CLSID* pCLSID /*= NULL*/)
  455. {
  456. return UnregisterServer(FALSE, pCLSID);
  457. }
  458. inline void CComModule::AddCreateWndData(_AtlCreateWndData* pData, void* pObject)
  459. {
  460. _AtlWinModule.AddCreateWndData(pData, pObject);
  461. }
  462. inline void* CComModule::ExtractCreateWndData()
  463. {
  464. return _AtlWinModule.ExtractCreateWndData();
  465. }
  466. } // namespace ATL
  467. #endif // __ATLBASE_INL__