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.

789 lines
22 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation
  3. Module Name:
  4. FusionHandle.h
  5. Abstract:
  6. Simple exception safe wrappers of Win32 "handle" types, defining "handle" loosely.
  7. CFusionFile
  8. CDynamicLinkLibrary
  9. CFindFile (should be named CFindFileHandle, see NVseeLibIo::CFindFile vs. NVseeLibIo::CFindFileHandle
  10. CFindFile includes a WIN32_FIND_DATA, CFindFileHandle does not.)
  11. CFileMapping
  12. CMappedViewOfFile
  13. CRegKey
  14. See also:
  15. NVseeLibReg::CRegKey
  16. NVseeLibIo::CFusionFile
  17. NVseeLibIo::CFileMapping
  18. NVseeLibIo::CMappedViewOfFile
  19. NVseeLibIo::CFindFullPath
  20. NVseeLibModule::CDynamicLinkLibrary
  21. etc.
  22. Author:
  23. Jay Krell (a-JayK, JayKrell) May 2000
  24. Revision History:
  25. --*/
  26. #pragma once
  27. #include "fusiontrace.h"
  28. #include "fusionbuffer.h"
  29. #include "wtypes.h"
  30. #include "wincrypt.h"
  31. template <void* const* invalidValue, typename Closer>
  32. class CHandleTemplate
  33. {
  34. public:
  35. // void* instead of HANDLE to fudge views
  36. // HANDLE is void*
  37. CHandleTemplate(const void* handle = *invalidValue);
  38. ~CHandleTemplate();
  39. BOOL Win32Close();
  40. void* Detach();
  41. void operator=(const void*);
  42. operator void*() const;
  43. operator const void*() const;
  44. // private
  45. class CSmartPointerPointerOrDumbPointerPointer
  46. {
  47. public:
  48. CSmartPointerPointerOrDumbPointerPointer(CHandleTemplate* p) : m(p) { }
  49. operator CHandleTemplate*() { return m; }
  50. operator void**() { /*assert((**m).m_handle == *invalidValue);*/ return &(*m).m_handle; }
  51. operator HKEY*() { /*assert((**m).m_handle == *invalidValue);*/
  52. //compiler bug? m->operator HKEY(); // only allow this to compile for CFusionRegKey
  53. //static_cast<HKEY>(*m);
  54. static_cast<CRegKey*>(m);
  55. return reinterpret_cast<HKEY*>(operator void**()); }
  56. operator HCRYPTHASH*() {
  57. static_cast<CCryptHash*>(m);
  58. return reinterpret_cast<HCRYPTHASH*>(operator void**()); }
  59. CHandleTemplate* m;
  60. };
  61. CSmartPointerPointerOrDumbPointerPointer operator&() { return CSmartPointerPointerOrDumbPointerPointer(this); }
  62. void* m_handle;
  63. static void* GetInvalidValue() { return *invalidValue; }
  64. bool IsValid() const { return m_handle != *invalidValue; }
  65. private:
  66. CHandleTemplate(const CHandleTemplate&); // deliberately not implemented
  67. void operator=(const CHandleTemplate&); // deliberately not implemented
  68. };
  69. __declspec(selectany) extern void* const hInvalidValue = INVALID_HANDLE_VALUE;
  70. __declspec(selectany) extern void* const hNull = NULL;
  71. /* This closes a Win32 event log handle for writing. */
  72. class COperatorDeregisterEventSource
  73. {
  74. public: BOOL operator()(void* handle) const;
  75. };
  76. /* This closes a Win32 event log handle for reading. */
  77. class COperatorCloseEventLog
  78. {
  79. public: BOOL operator()(void* handle) const;
  80. };
  81. /* This closes file, event, mutex, semaphore, etc. kernel objects */
  82. class COperatorCloseHandle
  83. {
  84. public: BOOL operator()(void* handle) const;
  85. };
  86. //
  87. // Closes HCRYPTHASH objects
  88. //
  89. class COperatorCloseCryptHash
  90. {
  91. public: BOOL operator()(void* handle) const;
  92. };
  93. /* this closes FindFirstFile/FindNextFile */
  94. class COperatorFindClose
  95. {
  96. public: BOOL operator()(void* handle) const;
  97. };
  98. /* this closes MapViewOfFile */
  99. class COperatorUnmapViewOfFile
  100. {
  101. public: BOOL operator()(void* handle) const;
  102. };
  103. /* this closes FreeLibrary */
  104. class COperatorFreeLibrary
  105. {
  106. public: BOOL operator()(void* handle) const;
  107. };
  108. /* this closes CreateActCtx/AddRefActCtx */
  109. class COperatorReleaseActCtx
  110. {
  111. public: BOOL operator()(void* handle) const;
  112. };
  113. #include "fusionreg.h"
  114. class CEvent : public CHandleTemplate<&hNull, COperatorCloseHandle>
  115. {
  116. private:
  117. typedef CHandleTemplate<&hNull, COperatorCloseHandle> Base;
  118. public:
  119. CEvent(void* handle = NULL) : Base(handle) { }
  120. BOOL Win32CreateEvent(BOOL ManualReset, BOOL InitialState, PCWSTR Name = NULL);
  121. void operator=(void* v) { Base::operator=(v); }
  122. private:
  123. CEvent(const CEvent &); // intentionally not implemented
  124. void operator =(const CEvent &); // intentionally not implemented
  125. };
  126. class CThread : public CHandleTemplate<&hNull, COperatorCloseHandle>
  127. {
  128. private:
  129. typedef CHandleTemplate<&hNull, COperatorCloseHandle> Base;
  130. public:
  131. CThread(void* handle = NULL) : Base(handle) { }
  132. BOOL Win32CreateThread(LPTHREAD_START_ROUTINE StartAddress, LPVOID Parameter, DWORD Flags = 0, LPDWORD ThreadId = NULL);
  133. void operator=(void* v) { Base::operator=(v); }
  134. private:
  135. CThread(const CThread &); // intentionally not implemented
  136. void operator =(const CThread &); // intentionally not implemented
  137. };
  138. class CFindFile : public CHandleTemplate<&hInvalidValue, COperatorFindClose>
  139. {
  140. private:
  141. typedef CHandleTemplate<&hInvalidValue, COperatorFindClose> Base;
  142. public:
  143. CFindFile(void* handle = INVALID_HANDLE_VALUE) : Base(handle) { }
  144. HRESULT HrFindFirstFile(PCSTR nameOrWildcard, WIN32_FIND_DATAA*);
  145. HRESULT HrFindFirstFile(PCWSTR nameOrWildcard, WIN32_FIND_DATAW*);
  146. BOOL Win32FindFirstFile( PCSTR nameOrWildcard, WIN32_FIND_DATAA*);
  147. BOOL Win32FindFirstFile(PCWSTR nameOrWildcard, WIN32_FIND_DATAW*);
  148. void operator=(void* v) { Base::operator=(v); }
  149. private:
  150. CFindFile(const CFindFile &); // intentionally not implemented
  151. void operator =(const CFindFile &); // intentionally not implemented
  152. };
  153. class CFusionFile : public CHandleTemplate<&hInvalidValue, COperatorCloseHandle>
  154. {
  155. private:
  156. typedef CHandleTemplate<&hInvalidValue, COperatorCloseHandle> Base;
  157. public:
  158. CFusionFile(void* handle = INVALID_HANDLE_VALUE) : Base(handle) { }
  159. HRESULT HrCreateFile( PCSTR name, DWORD access, DWORD share, DWORD openOrCreate);
  160. HRESULT HrCreateFile(PCWSTR name, DWORD access, DWORD share, DWORD openOrCreate);
  161. BOOL Win32CreateFile( PCSTR name, DWORD access, DWORD share, DWORD openOrCreate);
  162. BOOL Win32CreateFile(PCWSTR name, DWORD access, DWORD share, DWORD openOrCreate);
  163. BOOL Win32GetSize(ULONGLONG &rulSize) const;
  164. void operator=(void* v) { Base::operator=(v); }
  165. private:
  166. CFusionFile(const CFusionFile &); // intentionally not implemented
  167. void operator =(const CFusionFile &); // intentionally not implemented
  168. };
  169. class CFileMapping : public CHandleTemplate<&hNull, COperatorCloseHandle>
  170. {
  171. private:
  172. typedef CHandleTemplate<&hNull, COperatorCloseHandle> Base;
  173. public:
  174. CFileMapping(void* handle = NULL) : Base(handle) { }
  175. HRESULT HrCreateFileMapping(void* file, DWORD flProtect, ULONGLONG maximumSize=0, PCWSTR name=0);
  176. BOOL Win32CreateFileMapping(void* file, DWORD flProtect, ULONGLONG maximumSize=0, PCWSTR name=0);
  177. void operator=(void* v) { Base::operator=(v); }
  178. private:
  179. CFileMapping(const CFileMapping &); // intentionally not implemented
  180. void operator =(const CFileMapping &); // intentionally not implemented
  181. };
  182. class CCryptHash : public CHandleTemplate<&hNull, COperatorCloseCryptHash>
  183. {
  184. private:
  185. typedef CHandleTemplate<&hNull, COperatorCloseCryptHash> Base;
  186. public:
  187. CCryptHash( HCRYPTHASH hash = NULL ) : Base((void*)hash) { }
  188. operator HCRYPTHASH() { return (HCRYPTHASH)(Base::operator void*()); }
  189. void operator=(HCRYPTHASH hash) {
  190. ASSERT_NTC(!IsValid());
  191. m_handle = (void*)hash;
  192. }
  193. private:
  194. CCryptHash( const CCryptHash & );
  195. CCryptHash &operator=( const CCryptHash & );
  196. };
  197. class CMappedViewOfFile : public CHandleTemplate<&hNull, COperatorUnmapViewOfFile>
  198. {
  199. private:
  200. typedef CHandleTemplate<&hNull, COperatorUnmapViewOfFile> Base;
  201. public:
  202. CMappedViewOfFile(void* handle = NULL) : Base(handle) { }
  203. HRESULT HrMapViewOfFile(void* fileMapping, DWORD access, ULONGLONG offset=0, SIZE_T size=0);
  204. BOOL Win32MapViewOfFile(void* fileMapping, DWORD access, ULONGLONG offset=0, SIZE_T size=0);
  205. void operator=(void* v) { Base::operator=(v); }
  206. operator void*() { return Base::operator void*(); }
  207. private:
  208. CMappedViewOfFile(const CMappedViewOfFile &); // intentionally not implemented
  209. void operator =(const CMappedViewOfFile &); // intentionally not implemented
  210. operator void*() const; // intentionally not implemented
  211. };
  212. class CDynamicLinkLibrary : public CHandleTemplate<&hNull, COperatorFreeLibrary>
  213. {
  214. private:
  215. typedef CHandleTemplate<&hNull, COperatorFreeLibrary> Base;
  216. public:
  217. CDynamicLinkLibrary(void* handle = NULL) : Base(handle) { }
  218. BOOL Win32LoadLibrary(PCWSTR file, DWORD flags = 0);
  219. template <typename PointerToFunction>
  220. bool Win32GetProcAddress(PCSTR procName, PointerToFunction* ppfn)
  221. {
  222. return (*ppfn = reinterpret_cast<PointerToFunction>(::GetProcAddress(*this, procName))) != NULL;
  223. }
  224. operator HMODULE() { return reinterpret_cast<HMODULE>(operator void*()); }
  225. HMODULE Detach() { return reinterpret_cast<HMODULE>(Base::Detach()); }
  226. void operator=(void* v) { Base::operator=(v); }
  227. private:
  228. CDynamicLinkLibrary(const CDynamicLinkLibrary &); // intentionally not implemented
  229. void operator =(const CDynamicLinkLibrary &); // intentionally not implemented
  230. };
  231. class CFusionActCtxHandle : public CHandleTemplate<&hInvalidValue, COperatorReleaseActCtx>
  232. {
  233. private:
  234. typedef CHandleTemplate<&hInvalidValue, COperatorReleaseActCtx> Base;
  235. public:
  236. CFusionActCtxHandle(void* handle = INVALID_HANDLE_VALUE) : Base(handle) { }
  237. BOOL Win32Create(PCACTCTXW);
  238. void operator=(void* v) { Base::operator=(v); }
  239. private:
  240. CFusionActCtxHandle(const CFusionActCtxHandle &); // intentionally not implemented
  241. void operator =(const CFusionActCtxHandle &); // intentionally not implemented
  242. };
  243. class CFusionActCtxScope
  244. {
  245. protected:
  246. BOOL m_fSuccess;
  247. ULONG_PTR m_ulCookie;
  248. public:
  249. CFusionActCtxScope();
  250. ~CFusionActCtxScope();
  251. BOOL Win32Activate(HANDLE hActCtx);
  252. private:
  253. CFusionActCtxScope(const CFusionActCtxScope &); // intentionally not implemented
  254. void operator =(const CFusionActCtxScope &); // intentionally not implemented
  255. };
  256. /*--------------------------------------------------------------------------
  257. CFindFile
  258. --------------------------------------------------------------------------*/
  259. inline BOOL
  260. CFindFile::Win32FindFirstFile(
  261. PCSTR nameOrWildcard,
  262. WIN32_FIND_DATAA *data
  263. )
  264. {
  265. BOOL fSuccess = FALSE;
  266. FN_TRACE_WIN32(fSuccess);
  267. HANDLE hTemp = ::FindFirstFileA(nameOrWildcard, data);
  268. if (hTemp == INVALID_HANDLE_VALUE)
  269. {
  270. goto Exit;
  271. }
  272. (*this) = hTemp;
  273. fSuccess = TRUE;
  274. Exit:
  275. return fSuccess;
  276. }
  277. inline BOOL
  278. CFindFile::Win32FindFirstFile(
  279. PCWSTR nameOrWildcard,
  280. WIN32_FIND_DATAW *data
  281. )
  282. {
  283. BOOL fSuccess = FALSE;
  284. FN_TRACE_WIN32(fSuccess);
  285. HANDLE hTemp = ::FindFirstFileW(nameOrWildcard, data);
  286. if (hTemp == INVALID_HANDLE_VALUE)
  287. {
  288. goto Exit;
  289. }
  290. (*this) = hTemp;
  291. fSuccess = TRUE;
  292. Exit:
  293. return fSuccess;
  294. }
  295. inline
  296. HRESULT
  297. CFindFile::HrFindFirstFile(
  298. PCSTR nameOrWildcard,
  299. WIN32_FIND_DATAA *data
  300. )
  301. {
  302. HRESULT hr = HRESULT_FROM_WIN32(ERROR_INTERNAL_ERROR);
  303. FN_TRACE_HR(hr);
  304. IFW32FALSE_EXIT(this->Win32FindFirstFile(nameOrWildcard, data));
  305. hr = NOERROR;
  306. Exit:
  307. return hr;
  308. }
  309. inline
  310. HRESULT
  311. CFindFile::HrFindFirstFile(PCWSTR nameOrWildcard, WIN32_FIND_DATAW* data)
  312. {
  313. HRESULT hr = HRESULT_FROM_WIN32(ERROR_INTERNAL_ERROR);
  314. FN_TRACE_HR(hr);
  315. IFW32FALSE_EXIT(this->Win32FindFirstFile(nameOrWildcard, data));
  316. hr = NOERROR;
  317. Exit:
  318. return hr;
  319. }
  320. /*--------------------------------------------------------------------------
  321. CFusionFile
  322. --------------------------------------------------------------------------*/
  323. inline BOOL
  324. CFusionFile::Win32CreateFile(
  325. PCSTR name,
  326. DWORD access,
  327. DWORD share,
  328. DWORD openOrCreate
  329. )
  330. {
  331. FN_PROLOG_WIN32
  332. HANDLE hTemp;
  333. IFW32INVALIDHANDLE_ORIGINATE_AND_EXIT(
  334. hTemp = ::CreateFileA(name, access, share, NULL, openOrCreate, FILE_ATTRIBUTE_NORMAL, NULL));
  335. operator=(hTemp);
  336. FN_EPILOG
  337. }
  338. inline BOOL
  339. CFusionFile::Win32CreateFile(
  340. PCWSTR name,
  341. DWORD access,
  342. DWORD share,
  343. DWORD openOrCreate
  344. )
  345. {
  346. FN_PROLOG_WIN32
  347. HANDLE hTemp;
  348. IFW32INVALIDHANDLE_ORIGINATE_AND_EXIT(
  349. hTemp = ::CreateFileW(name, access, share, NULL, openOrCreate, FILE_ATTRIBUTE_NORMAL, NULL));
  350. operator=(hTemp);
  351. FN_EPILOG
  352. }
  353. inline HRESULT CFusionFile::HrCreateFile(PCSTR name, DWORD access, DWORD share, DWORD openOrCreate)
  354. {
  355. if (!this->Win32CreateFile(name, access, share, openOrCreate))
  356. return HRESULT_FROM_WIN32(::FusionpGetLastWin32Error());
  357. return NOERROR;
  358. }
  359. inline HRESULT CFusionFile::HrCreateFile(PCWSTR name, DWORD access, DWORD share, DWORD openOrCreate)
  360. {
  361. if (!this->Win32CreateFile(name, access, share, openOrCreate))
  362. return HRESULT_FROM_WIN32(::FusionpGetLastWin32Error());
  363. return NOERROR;
  364. }
  365. inline BOOL
  366. CFusionFile::Win32GetSize(ULONGLONG &rulSize) const
  367. {
  368. DWORD highPart = 0;
  369. DWORD lastError = NO_ERROR;
  370. DWORD lowPart = ::GetFileSize(m_handle, &highPart);
  371. if (lowPart == INVALID_FILE_SIZE && (lastError = ::FusionpGetLastWin32Error()) != NO_ERROR)
  372. {
  373. TRACE_WIN32_FAILURE_ORIGINATION(GetFileSize);
  374. return FALSE;
  375. }
  376. ULARGE_INTEGER liSize;
  377. liSize.LowPart = lowPart;
  378. liSize.HighPart = highPart;
  379. rulSize = liSize.QuadPart;
  380. return TRUE;
  381. }
  382. /*--------------------------------------------------------------------------
  383. CFileMapping
  384. --------------------------------------------------------------------------*/
  385. inline HRESULT
  386. CFileMapping::HrCreateFileMapping(void* file, DWORD flProtect, ULONGLONG maximumSize, PCWSTR name)
  387. {
  388. LARGE_INTEGER liMaximumSize;
  389. liMaximumSize.QuadPart = maximumSize;
  390. HANDLE hTemp = ::CreateFileMappingW(file, NULL, flProtect, liMaximumSize.HighPart, liMaximumSize.LowPart, name);
  391. if (hTemp == NULL)
  392. {
  393. TRACE_WIN32_FAILURE_ORIGINATION(CreateFileMapping);
  394. return HRESULT_FROM_WIN32(::FusionpGetLastWin32Error());
  395. }
  396. operator=(hTemp);
  397. return S_OK;
  398. }
  399. inline BOOL
  400. CFileMapping::Win32CreateFileMapping(
  401. void* file,
  402. DWORD flProtect,
  403. ULONGLONG maximumSize,
  404. PCWSTR name
  405. )
  406. {
  407. return SUCCEEDED(this->HrCreateFileMapping(file, flProtect, maximumSize, name));
  408. }
  409. /*--------------------------------------------------------------------------
  410. CMappedViewOfFile
  411. --------------------------------------------------------------------------*/
  412. inline HRESULT
  413. CMappedViewOfFile::HrMapViewOfFile(
  414. void* fileMapping,
  415. DWORD access,
  416. ULONGLONG offset,
  417. SIZE_T size
  418. )
  419. {
  420. ULARGE_INTEGER liOffset;
  421. liOffset.QuadPart = offset;
  422. void* pvTemp = ::MapViewOfFile(fileMapping, access, liOffset.HighPart, liOffset.LowPart, size);
  423. if (pvTemp == NULL)
  424. {
  425. TRACE_WIN32_FAILURE_ORIGINATION(MapViewOfFile);
  426. return HRESULT_FROM_WIN32(::FusionpGetLastWin32Error());
  427. }
  428. (*this) = pvTemp;
  429. return S_OK;
  430. }
  431. inline BOOL
  432. CMappedViewOfFile::Win32MapViewOfFile(void* fileMapping, DWORD access, ULONGLONG offset, SIZE_T size)
  433. {
  434. return SUCCEEDED(this->HrMapViewOfFile(fileMapping, access, offset, size));
  435. }
  436. /*--------------------------------------------------------------------------
  437. CEvent
  438. --------------------------------------------------------------------------*/
  439. inline BOOL CEvent::Win32CreateEvent(BOOL ManualReset, BOOL InitialState, PCWSTR Name)
  440. {
  441. HANDLE Temp = ::CreateEventW(NULL, ManualReset, InitialState, Name);
  442. if (Temp == NULL)
  443. {
  444. TRACE_WIN32_FAILURE_ORIGINATION(CreateEventW);
  445. return FALSE;
  446. }
  447. (*this) = Temp;
  448. return TRUE;
  449. }
  450. /*--------------------------------------------------------------------------
  451. CThread
  452. --------------------------------------------------------------------------*/
  453. inline BOOL
  454. CThread::Win32CreateThread(
  455. LPTHREAD_START_ROUTINE StartAddress,
  456. LPVOID Parameter,
  457. DWORD Flags,
  458. LPDWORD ThreadId
  459. )
  460. {
  461. DWORD LocalThreadId;
  462. if (ThreadId == NULL)
  463. ThreadId = &LocalThreadId;
  464. HANDLE Temp = ::CreateThread(NULL, 0, StartAddress, Parameter, Flags, ThreadId);
  465. if (Temp == NULL)
  466. {
  467. TRACE_WIN32_FAILURE_ORIGINATION(CreateThread);
  468. return FALSE;
  469. }
  470. (*this) = Temp;
  471. return TRUE;
  472. }
  473. /*--------------------------------------------------------------------------
  474. CDynamicLinkLibrary
  475. --------------------------------------------------------------------------*/
  476. inline BOOL
  477. CDynamicLinkLibrary::Win32LoadLibrary(
  478. PCWSTR file,
  479. DWORD flags
  480. )
  481. {
  482. void* temp = ::LoadLibraryExW(file, NULL, flags);
  483. if (temp == NULL)
  484. {
  485. TRACE_WIN32_FAILURE_ORIGINATION(LoadLibraryExW);
  486. return FALSE;
  487. }
  488. (*this) = temp;
  489. return TRUE;
  490. }
  491. /*--------------------------------------------------------------------------
  492. CFusionActCtxHandle
  493. --------------------------------------------------------------------------*/
  494. inline BOOL
  495. CFusionActCtxHandle::Win32Create(
  496. PCACTCTXW pActCtx
  497. )
  498. {
  499. typedef HANDLE (WINAPI* PFN)(PCACTCTXW pActCtx);
  500. static PFN pfn;
  501. if (pfn == NULL)
  502. {
  503. HMODULE hmodKernel32 = ::GetModuleHandleW(L"Kernel32.dll");
  504. if (hmodKernel32 != NULL)
  505. {
  506. pfn = reinterpret_cast<PFN>(::GetProcAddress(hmodKernel32, "CreateActCtxW"));
  507. if (pfn == NULL)
  508. TRACE_WIN32_FAILURE_ORIGINATION(GetProcAddress);
  509. }
  510. else
  511. TRACE_WIN32_FAILURE_ORIGINATION(GetModuleHandleW);
  512. if (pfn == NULL)
  513. return FALSE;
  514. }
  515. void* temp = (*pfn)(pActCtx);
  516. if (temp == INVALID_HANDLE_VALUE)
  517. {
  518. TRACE_WIN32_FAILURE_ORIGINATION(CreateActCtxW);
  519. return FALSE;
  520. }
  521. (*this) = temp;
  522. return TRUE;
  523. }
  524. inline BOOL COperatorReleaseActCtx::operator()(HANDLE hActCtx) const
  525. {
  526. typedef BOOL (WINAPI* PFN)(HANDLE);
  527. static PFN pfn;
  528. if (pfn == NULL)
  529. {
  530. HMODULE hmodKernel32 = ::GetModuleHandleW(L"Kernel32.dll");
  531. if (hmodKernel32 != NULL)
  532. {
  533. pfn = reinterpret_cast<PFN>(::GetProcAddress(hmodKernel32, "ReleaseActCtx"));
  534. if (pfn == NULL)
  535. TRACE_WIN32_FAILURE_ORIGINATION(GetProcAddress);
  536. }
  537. else
  538. {
  539. TRACE_WIN32_FAILURE_ORIGINATION(GetModuleHandleW);
  540. }
  541. if (pfn == NULL)
  542. return FALSE;
  543. }
  544. return pfn(hActCtx);
  545. }
  546. /*--------------------------------------------------------------------------
  547. CFusionActCtxScope
  548. --------------------------------------------------------------------------*/
  549. inline CFusionActCtxScope::CFusionActCtxScope() : m_fSuccess(FALSE) { }
  550. inline BOOL CFusionActCtxScope::Win32Activate(HANDLE hActCtx)
  551. {
  552. typedef BOOL (WINAPI* PFN)(HANDLE hActCtx, ULONG_PTR* lpCookie);
  553. static PFN pfn;
  554. if (pfn == NULL)
  555. {
  556. HMODULE hmodKernel32 = ::GetModuleHandleW(L"Kernel32.dll");
  557. if (hmodKernel32 != NULL)
  558. {
  559. pfn = reinterpret_cast<PFN>(GetProcAddress(hmodKernel32, "ActivateActCtx"));
  560. if (pfn == NULL)
  561. TRACE_WIN32_FAILURE_ORIGINATION(GetProcAddress);
  562. }
  563. else
  564. {
  565. TRACE_WIN32_FAILURE_ORIGINATION(GetModuleHandleW);
  566. }
  567. if (pfn == NULL)
  568. return FALSE;
  569. }
  570. return (m_fSuccess = pfn(hActCtx, &m_ulCookie));
  571. }
  572. inline CFusionActCtxScope::~CFusionActCtxScope()
  573. {
  574. if (m_fSuccess)
  575. {
  576. CSxsPreserveLastError ple;
  577. m_fSuccess = FALSE;
  578. typedef BOOL (WINAPI* PFN)(DWORD dwFlags, ULONG_PTR ulCookie);
  579. static PFN pfn;
  580. if (pfn == NULL)
  581. {
  582. HMODULE hmodKernel32 = GetModuleHandleW(L"Kernel32.dll");
  583. if (hmodKernel32 != NULL)
  584. {
  585. pfn = reinterpret_cast<PFN>(GetProcAddress(hmodKernel32, "DeactivateActCtx"));
  586. if (pfn == NULL)
  587. TRACE_WIN32_FAILURE_ORIGINATION(GetProcAddress);
  588. }
  589. else
  590. {
  591. TRACE_WIN32_FAILURE_ORIGINATION(GetModuleHandleW);
  592. }
  593. if (pfn == NULL)
  594. {
  595. ple.Restore();
  596. return;
  597. }
  598. }
  599. (*pfn)(0, m_ulCookie);
  600. ple.Restore();
  601. }
  602. }
  603. /*--------------------------------------------------------------------------
  604. COperator*
  605. --------------------------------------------------------------------------*/
  606. inline BOOL COperatorCloseHandle::operator()(void* handle) const { return ::CloseHandle(handle); }
  607. inline BOOL COperatorFindClose::operator()(void* handle) const { return ::FindClose(handle); }
  608. inline BOOL COperatorUnmapViewOfFile::operator()(void* handle) const { return ::UnmapViewOfFile(handle); }
  609. inline BOOL COperatorCloseEventLog::operator()(void* handle) const { return ::CloseEventLog(handle); }
  610. inline BOOL COperatorDeregisterEventSource::operator()(void* handle) const { return ::DeregisterEventSource(handle); }
  611. inline BOOL COperatorFreeLibrary::operator()(void* handle) const { return ::FreeLibrary(reinterpret_cast<HMODULE>(handle)); }
  612. inline BOOL COperatorCloseCryptHash::operator()(void* handle) const { return ::CryptDestroyHash(reinterpret_cast<HCRYPTHASH>(handle)); };
  613. /*--------------------------------------------------------------------------
  614. CHandleTemplate
  615. --------------------------------------------------------------------------*/
  616. template <void* const* invalidValue, typename Closer>
  617. CHandleTemplate<invalidValue, Closer>::CHandleTemplate(const void* handle)
  618. : m_handle(const_cast<void*>(handle))
  619. {
  620. }
  621. template <void* const* invalidValue, typename Closer>
  622. void* CHandleTemplate<invalidValue, Closer>::Detach()
  623. {
  624. void* handle = m_handle;
  625. m_handle = *invalidValue;
  626. return handle;
  627. }
  628. template <void* const* invalidValue, typename Closer>
  629. void CHandleTemplate<invalidValue, Closer>::operator=(const void* handle)
  630. {
  631. if (handle != m_handle)
  632. {
  633. void *SavedHandle = m_handle;
  634. m_handle = const_cast<void*>(handle);
  635. if (SavedHandle != *invalidValue)
  636. {
  637. Closer close;
  638. // a bug waiting to happen to customers
  639. VERIFY_NTC(close(SavedHandle));
  640. }
  641. }
  642. }
  643. template <void* const* invalidValue, typename Closer>
  644. BOOL CHandleTemplate<invalidValue, Closer>::Win32Close()
  645. {
  646. void* handle = Detach();
  647. if (handle != *invalidValue)
  648. {
  649. Closer close;
  650. return close(handle);
  651. }
  652. return TRUE;
  653. }
  654. template <void* const* invalidValue, typename Closer>
  655. CHandleTemplate<invalidValue, Closer>::~CHandleTemplate()
  656. {
  657. CSxsPreserveLastError ple;
  658. (void) this->Win32Close();
  659. ple.Restore();
  660. }
  661. template <void* const* invalidValue, typename Closer>
  662. CHandleTemplate<invalidValue, Closer>::operator void*() const
  663. {
  664. return m_handle;
  665. }
  666. template <void* const* invalidValue, typename Closer>
  667. CHandleTemplate<invalidValue, Closer>::operator const void*() const
  668. {
  669. return m_handle;
  670. }
  671. /*--------------------------------------------------------------------------
  672. end of file
  673. --------------------------------------------------------------------------*/