Leaked source code of windows server 2003
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.

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