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.

747 lines
21 KiB

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