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.

1045 lines
20 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992-1998
  5. //
  6. // File: smart.hxx
  7. //
  8. // Contents: Macro for simple smart pointer
  9. //
  10. // History: 25-Aug-94 KyleP Created
  11. // 24-Oct-94 BartoszM Added template
  12. //
  13. //---------------------------------------------------------------------------
  14. #pragma once
  15. //+---------------------------------------------------------------------------
  16. //
  17. // Class: XPtr<class CItem>
  18. //
  19. // Purpose: Smart Pointer template
  20. //
  21. // History: 24-Oct-94 BartoszM Created
  22. //
  23. // Notes: Usage
  24. //
  25. // XPtr<CWidget> xWidget(pWidget);
  26. // xWidget->WidgetMethod(...);
  27. // CWidget pW = xWidget.Acquire();
  28. //
  29. //----------------------------------------------------------------------------
  30. template<class CItem> class XPtr
  31. {
  32. public:
  33. XPtr(CItem* p = 0) : _p( p )
  34. {
  35. }
  36. XPtr ( XPtr<CItem> & x )
  37. {
  38. _p = x.Acquire();
  39. }
  40. ~XPtr() { delete _p; }
  41. CItem* operator->() { return _p; }
  42. CItem const * operator->() const { return _p; }
  43. BOOL IsNull() const { return (0 == _p); }
  44. void Set ( CItem* p )
  45. {
  46. Win4Assert (0 == _p);
  47. _p = p;
  48. }
  49. CItem * Acquire()
  50. {
  51. CItem * pTemp = _p;
  52. _p = 0;
  53. return pTemp;
  54. }
  55. CItem & GetReference() const
  56. {
  57. Win4Assert( 0 != _p );
  58. return *_p;
  59. }
  60. CItem * GetPointer() const { return _p; }
  61. void Free() { delete Acquire(); }
  62. private:
  63. XPtr<CItem>& operator=( const XPtr<CItem> & x );
  64. CItem * _p;
  65. };
  66. //+---------------------------------------------------------------------------
  67. //
  68. // Class: XPtrST<class CItem>
  69. //
  70. // Purpose: Smart Pointer template for Simple Types
  71. //
  72. // History: 12-Mar-96 dlee Created
  73. //
  74. //----------------------------------------------------------------------------
  75. template<class CItem> class XPtrST
  76. {
  77. public:
  78. XPtrST(CItem* p = 0) : _p( p )
  79. {
  80. }
  81. ~XPtrST() { delete _p; }
  82. BOOL IsNull() const { return ( 0 == _p ); }
  83. void Set ( CItem* p )
  84. {
  85. Win4Assert( 0 == _p );
  86. _p = p;
  87. }
  88. CItem * Acquire()
  89. {
  90. CItem * pTemp = _p;
  91. _p = 0;
  92. return pTemp;
  93. }
  94. CItem & GetReference() const
  95. {
  96. Win4Assert( 0 != _p );
  97. return *_p;
  98. }
  99. CItem * GetPointer() const { return _p ; }
  100. void Free() { delete Acquire(); }
  101. private:
  102. XPtrST (const XPtrST<CItem> & x);
  103. XPtrST<CItem> & operator=( const XPtrST<CItem> & x);
  104. CItem * _p;
  105. };
  106. //+---------------------------------------------------------------------------
  107. //
  108. // Class: XBStr
  109. //
  110. // Purpose: Smart BSTR class
  111. //
  112. // History: 05-May-97 KrishnaN Created
  113. //
  114. //----------------------------------------------------------------------------
  115. class XBStr
  116. {
  117. public:
  118. XBStr(BSTR p = 0) : _p( p ) {}
  119. XBStr ( XBStr & x ): _p( x.Acquire() ) {}
  120. ~XBStr() { SysFreeString(_p); }
  121. BOOL IsNull() const { return (0 == _p); }
  122. BSTR SetText ( WCHAR const * pOleStr )
  123. {
  124. Win4Assert (0 == _p && pOleStr);
  125. _p = SysAllocString(pOleStr);
  126. if (0 == _p)
  127. THROW( CException(E_OUTOFMEMORY) );
  128. return _p;
  129. }
  130. void Set ( BSTR pOleStr )
  131. {
  132. Win4Assert (0 == _p);
  133. _p = pOleStr;
  134. }
  135. BSTR Acquire()
  136. {
  137. BSTR pTemp = _p;
  138. _p = 0;
  139. return pTemp;
  140. }
  141. BSTR GetPointer() const { return _p; }
  142. void Free() { SysFreeString(Acquire()); }
  143. private:
  144. BSTR _p;
  145. };
  146. //+---------------------------------------------------------------------------
  147. //
  148. // Class: XPipeImpersonation
  149. //
  150. // Purpose: Smart Pointer for named pipe impersonation and revert
  151. //
  152. // History: 4-Oct-96 dlee Created
  153. //
  154. //----------------------------------------------------------------------------
  155. class XPipeImpersonation
  156. {
  157. public:
  158. XPipeImpersonation() : _fImpersonated( FALSE )
  159. {
  160. }
  161. XPipeImpersonation( HANDLE hPipe ) : _fImpersonated( FALSE )
  162. {
  163. Impersonate( hPipe );
  164. }
  165. void Impersonate( HANDLE hPipe )
  166. {
  167. Win4Assert( INVALID_HANDLE_VALUE != hPipe );
  168. Win4Assert( !_fImpersonated );
  169. if ( ! ImpersonateNamedPipeClient( hPipe ) )
  170. THROW( CException() );
  171. _fImpersonated = TRUE;
  172. }
  173. ~XPipeImpersonation()
  174. {
  175. Revert();
  176. }
  177. void Revert()
  178. {
  179. if ( _fImpersonated )
  180. {
  181. RevertToSelf();
  182. _fImpersonated = FALSE;
  183. }
  184. }
  185. private:
  186. BOOL _fImpersonated;
  187. };
  188. //+---------------------------------------------------------------------------
  189. //
  190. // Class: XInterface<class CItem>
  191. //
  192. // Purpose: Smart Pointer template for items Release()'ed, not ~'ed
  193. //
  194. // History: 22-Mar-95 dlee Created
  195. //
  196. //----------------------------------------------------------------------------
  197. template<class CItem> class XInterface
  198. {
  199. public:
  200. XInterface(CItem* p = 0) : _p( p )
  201. {
  202. }
  203. ~XInterface() { if (0 != _p) _p->Release(); }
  204. CItem* operator->() { return _p; }
  205. CItem const * operator->() const { return _p; }
  206. BOOL IsNull() const { return (0 == _p); }
  207. void Set ( CItem* p )
  208. {
  209. Win4Assert (0 == _p);
  210. _p = p;
  211. }
  212. CItem * Acquire()
  213. {
  214. CItem * pTemp = _p;
  215. _p = 0;
  216. return pTemp;
  217. }
  218. CItem & GetReference() const
  219. {
  220. Win4Assert( 0 != _p );
  221. return *_p;
  222. }
  223. CItem * GetPointer() const { return _p; }
  224. void ** GetQIPointer() { return (void **)&_p; }
  225. CItem ** GetPPointer() { return &_p; }
  226. IUnknown ** GetIUPointer() { return (IUnknown **) &_p; }
  227. void Free()
  228. {
  229. CItem *p = Acquire();
  230. if ( 0 != p )
  231. p->Release();
  232. }
  233. private:
  234. //
  235. // Do not define this. We don't want people
  236. // doing copies.
  237. //
  238. XInterface (const XInterface<CItem> & x);
  239. XInterface<CItem>& operator=(const XInterface<CItem> & x);
  240. CItem * _p;
  241. };
  242. //+---------------------------------------------------------------------------
  243. //
  244. // Class: XRtlHeapMem
  245. //
  246. // Purpose: A smart pointer for memory allocated from the process heap.
  247. //
  248. // History: 7-15-96 srikants Created
  249. //
  250. //----------------------------------------------------------------------------
  251. class XRtlHeapMem
  252. {
  253. public:
  254. XRtlHeapMem( void * pv ) : _pv(pv)
  255. {
  256. }
  257. ~XRtlHeapMem()
  258. {
  259. _FreeMem();
  260. }
  261. void Set( void * pv )
  262. {
  263. _FreeMem();
  264. _pv = pv;
  265. }
  266. void * Get() { return _pv; }
  267. private:
  268. void _FreeMem()
  269. {
  270. if ( _pv )
  271. {
  272. RtlFreeHeap( RtlProcessHeap(), 0, _pv );
  273. _pv = 0;
  274. }
  275. }
  276. void * _pv; // Memory pointer to free
  277. };
  278. //+---------------------------------------------------------------------------
  279. //
  280. // Class: XGlobalAllocMem
  281. //
  282. // Purpose: A smart pointer for GlobalAlloc
  283. //
  284. // History: 2-19-97 mohamedn Created
  285. //
  286. //----------------------------------------------------------------------------
  287. class XGlobalAllocMem
  288. {
  289. public:
  290. XGlobalAllocMem( HGLOBAL pv = 0 ) : _pv( pv )
  291. {
  292. }
  293. ~XGlobalAllocMem() { Free(); }
  294. void Set( HGLOBAL pv )
  295. {
  296. Win4Assert( 0 == _pv );
  297. _pv = pv;
  298. }
  299. HGLOBAL Get() { return _pv; }
  300. HGLOBAL Acquire() { HGLOBAL p = _pv; _pv = 0; return p; }
  301. void Free()
  302. {
  303. if ( 0 != _pv )
  304. {
  305. GlobalFree( _pv );
  306. _pv = 0;
  307. }
  308. }
  309. private:
  310. HGLOBAL _pv;
  311. }; //XGlobalAllocMem
  312. //+---------------------------------------------------------------------------
  313. //
  314. // Class: XLocalAllocMem
  315. //
  316. // Purpose: A smart pointer for LocalAlloc
  317. //
  318. // History: 10-25-96 dlee Created
  319. //
  320. //----------------------------------------------------------------------------
  321. class XLocalAllocMem
  322. {
  323. public:
  324. XLocalAllocMem( void * pv = 0 ) : _pv( pv )
  325. {
  326. }
  327. ~XLocalAllocMem() { Free(); }
  328. void Set( void * pv )
  329. {
  330. Win4Assert( 0 == _pv );
  331. _pv = pv;
  332. }
  333. void * Get() { return _pv; }
  334. void * Acquire() { void *p = _pv; _pv = 0; return p; }
  335. void Free()
  336. {
  337. if ( 0 != _pv )
  338. {
  339. LocalFree( (HLOCAL) _pv );
  340. _pv = 0;
  341. }
  342. }
  343. private:
  344. void * _pv;
  345. }; //XLocalAllocMem
  346. //+---------------------------------------------------------------------------
  347. //
  348. // Class: XSafeArray
  349. //
  350. // Purpose: A smart pointer for SafeArray
  351. //
  352. // History: 05-01-97 emilyb created
  353. //
  354. //----------------------------------------------------------------------------
  355. class XSafeArray
  356. {
  357. public:
  358. XSafeArray( SAFEARRAY * psa = 0 ) : _psa( psa )
  359. {
  360. }
  361. ~XSafeArray() { if ( 0 != _psa )
  362. SafeArrayDestroy(_psa);
  363. }
  364. void Set( SAFEARRAY * psa )
  365. {
  366. Win4Assert( 0 == _psa );
  367. _psa = psa;
  368. }
  369. SAFEARRAY * Get() { return _psa; }
  370. SAFEARRAY * Acquire() { SAFEARRAY * psa = _psa; _psa = 0; return psa; }
  371. void Destroy()
  372. {
  373. if ( 0 != _psa )
  374. {
  375. SafeArrayDestroy(_psa);
  376. _psa = 0;
  377. }
  378. }
  379. private:
  380. SAFEARRAY * _psa;
  381. }; //XSafeArray
  382. //+-------------------------------------------------------------------------
  383. //
  384. // Template: renewx, function
  385. //
  386. // Synopsis: Re-allocate memory in a type-safe way
  387. //
  388. // Arguments: [ptOld] -- ole block of memory, may be 0
  389. // [cOld] -- old count of items
  390. // [cNew] -- new count of items
  391. //
  392. // Returns: T * pointer to the newly allocated memory
  393. //
  394. //--------------------------------------------------------------------------
  395. template<class T> T * renewx( T * ptOld, ULONG cOld, ULONG cNew )
  396. {
  397. if ( cOld == cNew )
  398. {
  399. return ptOld;
  400. }
  401. else
  402. {
  403. T *ptNew = new T [cNew];
  404. ULONG cMin = __min(cOld,cNew);
  405. RtlCopyMemory( ptNew, ptOld, cMin * sizeof T );
  406. delete [] ptOld;
  407. return ptNew;
  408. }
  409. } //renewx
  410. //+---------------------------------------------------------------------------
  411. //
  412. // Class: XFindFirstFile
  413. //
  414. // Purpose: A smart pointer for FindFirstFile
  415. //
  416. // History: 10-25-96 dlee Created
  417. //
  418. //----------------------------------------------------------------------------
  419. class XFindFirstFile
  420. {
  421. public:
  422. XFindFirstFile( WCHAR const * pwcPath,
  423. WIN32_FIND_DATA * pFindData )
  424. {
  425. _h = FindFirstFile( pwcPath, pFindData );
  426. }
  427. XFindFirstFile( HANDLE h = INVALID_HANDLE_VALUE ) : _h( h ) {}
  428. ~XFindFirstFile()
  429. {
  430. Free();
  431. }
  432. BOOL IsOK() { return INVALID_HANDLE_VALUE != _h; }
  433. void Set( HANDLE h )
  434. {
  435. Win4Assert( INVALID_HANDLE_VALUE == _h );
  436. _h = h;
  437. }
  438. HANDLE Get() { return _h; }
  439. HANDLE Acquire() { HANDLE h = _h; _h = INVALID_HANDLE_VALUE; return h; }
  440. void Free()
  441. {
  442. if ( INVALID_HANDLE_VALUE != _h )
  443. {
  444. FindClose( _h );
  445. _h = INVALID_HANDLE_VALUE;
  446. }
  447. }
  448. private:
  449. HANDLE _h;
  450. }; //XFindFirstFile
  451. //+---------------------------------------------------------------------------
  452. //
  453. // Class: SHandle
  454. //
  455. // Purpose: Smart handle
  456. //
  457. // History: 17-Jul-95 DwightKr Created
  458. //
  459. // Notes: Usage
  460. //
  461. // SHandle xHandle( hFile )
  462. // HANDLE handle = xHandle.Acquire();
  463. //
  464. //----------------------------------------------------------------------------
  465. class SHandle
  466. {
  467. public :
  468. SHandle(HANDLE handle = INVALID_HANDLE_VALUE) :
  469. _handle(handle) { }
  470. ~SHandle()
  471. {
  472. Free();
  473. }
  474. void Free()
  475. {
  476. if ( INVALID_HANDLE_VALUE != _handle )
  477. {
  478. NtClose(_handle);
  479. _handle = INVALID_HANDLE_VALUE;
  480. }
  481. }
  482. HANDLE Acquire()
  483. {
  484. HANDLE handle = _handle;
  485. _handle = INVALID_HANDLE_VALUE;
  486. return handle;
  487. }
  488. void Set( HANDLE handle )
  489. {
  490. Win4Assert( INVALID_HANDLE_VALUE == _handle );
  491. _handle = handle;
  492. }
  493. HANDLE Get() const { return _handle; }
  494. private :
  495. HANDLE _handle;
  496. };
  497. //+---------------------------------------------------------------------------
  498. //
  499. // Class: SRegKey
  500. //
  501. // Purpose: Smart registy key
  502. //
  503. // History: 12/29/97 dlee created header, author unknown
  504. //
  505. //----------------------------------------------------------------------------
  506. class SRegKey
  507. {
  508. public:
  509. SRegKey( HKEY key ) : _key( key ) {}
  510. SRegKey() : _key( 0 ) {}
  511. ~SRegKey() { Free(); }
  512. void Set( HKEY key ) { Win4Assert( 0 == _key ); _key = key; }
  513. HKEY * GetPointer() { Win4Assert( 0 == _key ); return &_key; }
  514. HKEY Get() { return _key; }
  515. BOOL IsNull() { return ( 0 == _key ); }
  516. HKEY Acquire() { HKEY tmp = _key; _key = 0; return tmp; }
  517. void Free() { if ( 0 != _key ) { RegCloseKey( _key ); _key = 0; } }
  518. private:
  519. HKEY _key;
  520. };
  521. //+---------------------------------------------------------------------------
  522. //
  523. // Class: SPropVariant
  524. //
  525. // Purpose: Smart propVariant
  526. //
  527. // History: 01-Jul-96 DwightKr Created
  528. //
  529. // Notes: Usage
  530. //
  531. // SPropVariant xPropVariant(pPropVariant);
  532. // pPropVariant = xPropVariant.Acquire();
  533. //
  534. //----------------------------------------------------------------------------
  535. class SPropVariant
  536. {
  537. public :
  538. SPropVariant(PROPVARIANT * pPropVariant = 0) :
  539. _pPropVariant(pPropVariant)
  540. {
  541. }
  542. ~SPropVariant()
  543. {
  544. if ( 0 != _pPropVariant )
  545. PropVariantClear( _pPropVariant );
  546. _pPropVariant = 0;
  547. }
  548. PROPVARIANT * Acquire()
  549. {
  550. PROPVARIANT * pPropVariant = _pPropVariant;
  551. _pPropVariant = 0;
  552. return pPropVariant;
  553. }
  554. void Set( PROPVARIANT * pPropVariant )
  555. {
  556. Win4Assert( 0 == _pPropVariant );
  557. _pPropVariant = pPropVariant;
  558. }
  559. BOOL IsNull() const { return 0 == _pPropVariant; }
  560. PROPVARIANT * Get() const { return _pPropVariant; }
  561. private :
  562. PROPVARIANT * _pPropVariant;
  563. };
  564. //+-------------------------------------------------------------------------
  565. //
  566. // Class: SWin32Handle
  567. //
  568. // Purpose: Smart pointer to a Win32 HANDLE; insures handle is closed
  569. //
  570. // History: 07-Jun-94 DwightKr Created
  571. //
  572. //--------------------------------------------------------------------------
  573. class SWin32Handle
  574. {
  575. public:
  576. SWin32Handle( HANDLE handle = INVALID_HANDLE_VALUE ) : _handle(handle) {}
  577. ~SWin32Handle()
  578. {
  579. Free();
  580. }
  581. void Free()
  582. {
  583. if ( INVALID_HANDLE_VALUE != _handle )
  584. {
  585. CloseHandle( _handle );
  586. _handle = INVALID_HANDLE_VALUE;
  587. }
  588. }
  589. void Set( HANDLE h )
  590. {
  591. Win4Assert( INVALID_HANDLE_VALUE == _handle );
  592. _handle = h;
  593. }
  594. HANDLE Get()
  595. {
  596. return _handle;
  597. }
  598. HANDLE Acquire()
  599. {
  600. HANDLE h = _handle;
  601. _handle = INVALID_HANDLE_VALUE;
  602. return h;
  603. }
  604. BOOL IsValid()
  605. {
  606. return INVALID_HANDLE_VALUE != _handle;
  607. }
  608. operator HANDLE () { return _handle; };
  609. private:
  610. HANDLE _handle;
  611. };
  612. //+-------------------------------------------------------------------------
  613. //
  614. // Class: SDacl
  615. //
  616. // Purpose: Smart pointer to a Win32 TOKEN_DEFAULT_DACL; releases storage
  617. //
  618. // History: 07-Jun-94 DwightKr Created
  619. //
  620. //--------------------------------------------------------------------------
  621. class SDacl
  622. {
  623. public:
  624. SDacl( TOKEN_DEFAULT_DACL *pDacl ) : _pDacl(pDacl) {}
  625. ~SDacl() { delete _pDacl; }
  626. private:
  627. TOKEN_DEFAULT_DACL *_pDacl;
  628. };
  629. //+-------------------------------------------------------------------------
  630. //
  631. // Class: CSid
  632. //
  633. // Purpose: Pointer to a Win32 Sid; release the Sid
  634. //
  635. // History: 07-Jun-94 DwightKr Created
  636. //
  637. //--------------------------------------------------------------------------
  638. class CSid
  639. {
  640. public:
  641. CSid( SID_IDENTIFIER_AUTHORITY & NtAuthority, DWORD dwSubauthority )
  642. {
  643. if ( !AllocateAndInitializeSid( &NtAuthority,
  644. 1,
  645. dwSubauthority,
  646. 0,0,0,0,0,0,0,
  647. &_pSid ) )
  648. {
  649. THROW( CException() );
  650. }
  651. }
  652. ~CSid() { FreeSid( _pSid ); }
  653. PSID Get() { return _pSid; }
  654. operator PSID () { return _pSid; }
  655. private:
  656. PSID _pSid;
  657. };
  658. //+---------------------------------------------------------------------------
  659. //
  660. // Class: XCoMem<class CItem>
  661. //
  662. // Purpose: Smart Pointer template
  663. //
  664. // History: 24-Oct-94 BartoszM Created
  665. //
  666. // Notes: Usage
  667. //
  668. // XCoMem<CWidget> xWidget(pWidget);
  669. // xWidget->WidgetMethod(...);
  670. // CWidget pW = xWidget.Acquire();
  671. //
  672. //----------------------------------------------------------------------------
  673. template<class CItem> class XCoMem
  674. {
  675. public:
  676. XCoMem(CItem* p = 0) : _p(p)
  677. {
  678. }
  679. XCoMem(unsigned cItems) : _p(0)
  680. {
  681. Init( cItems );
  682. }
  683. ~XCoMem() { if ( 0 != _p ) CoTaskMemFree( _p ); }
  684. BOOL IsNull() const { return (0 == _p); }
  685. CItem & operator[]( unsigned i ) { return _p[i]; }
  686. void Set ( CItem* p )
  687. {
  688. Win4Assert (0 == _p);
  689. _p = p;
  690. }
  691. CItem * Acquire()
  692. {
  693. CItem * pTemp = _p;
  694. _p = 0;
  695. return pTemp;
  696. }
  697. CItem & GetReference() const
  698. {
  699. Win4Assert( 0 != _p );
  700. return *_p;
  701. }
  702. CItem * GetPointer()
  703. {
  704. return _p;
  705. }
  706. void Init( unsigned cItems )
  707. {
  708. Win4Assert( 0 == _p );
  709. _p = (CItem *) CoTaskMemAlloc( cItems * sizeof CItem );
  710. if ( 0 == _p )
  711. THROW ( CException( E_OUTOFMEMORY ) );
  712. }
  713. void InitNoThrow( unsigned cItems )
  714. {
  715. Win4Assert( 0 == _p );
  716. _p = (CItem *) CoTaskMemAlloc( cItems * sizeof CItem );
  717. }
  718. void Free( void )
  719. {
  720. if ( 0 != _p ) {
  721. CoTaskMemFree( _p );
  722. _p = 0;
  723. }
  724. }
  725. protected:
  726. CItem * _p;
  727. private:
  728. XCoMem (const XCoMem<CItem> & x);
  729. XCoMem<CItem> & operator=( const XCoMem<CItem> & x);
  730. };
  731. //+---------------------------------------------------------------------------
  732. //
  733. // Class: XLibHandle
  734. //
  735. // Purpose: Smart pointer for an HINSTANCE
  736. //
  737. // History: 4-22-97 KrishnaN Created
  738. //
  739. //----------------------------------------------------------------------------
  740. class XLibHandle
  741. {
  742. public:
  743. XLibHandle( HINSTANCE hLib = NULL ) : _hLibHandle( hLib )
  744. {
  745. }
  746. void Set( HINSTANCE hLib )
  747. {
  748. Win4Assert( NULL == _hLibHandle );
  749. _hLibHandle = hLib;
  750. }
  751. ~XLibHandle()
  752. {
  753. if ( NULL != _hLibHandle )
  754. {
  755. FreeLibrary( _hLibHandle );
  756. }
  757. }
  758. HINSTANCE Acquire()
  759. {
  760. HINSTANCE h = _hLibHandle;
  761. _hLibHandle = 0;
  762. return h;
  763. }
  764. HINSTANCE Get()
  765. {
  766. return _hLibHandle;
  767. }
  768. private:
  769. HINSTANCE _hLibHandle;
  770. };
  771. //+---------------------------------------------------------------------------
  772. //
  773. // Class: XBitmapHandle
  774. //
  775. // Purpose: Smart pointer for an HBITMAP
  776. //
  777. // History: 4-22-97 KrishnaN Created
  778. //
  779. //----------------------------------------------------------------------------
  780. class XBitmapHandle
  781. {
  782. public:
  783. XBitmapHandle( HBITMAP hBmp = NULL ) : _hBmpHandle( hBmp )
  784. {
  785. }
  786. void Set( HBITMAP hBmp )
  787. {
  788. Win4Assert( NULL == _hBmpHandle );
  789. _hBmpHandle = hBmp;
  790. }
  791. ~XBitmapHandle()
  792. {
  793. if (_hBmpHandle)
  794. DeleteObject( _hBmpHandle );
  795. }
  796. HBITMAP Acquire()
  797. {
  798. HBITMAP h = _hBmpHandle;
  799. _hBmpHandle = 0;
  800. return h;
  801. }
  802. HBITMAP Get()
  803. {
  804. return _hBmpHandle;
  805. }
  806. void Free()
  807. {
  808. HBITMAP h = Acquire();
  809. if (h)
  810. DeleteObject(h);
  811. }
  812. private:
  813. HBITMAP _hBmpHandle;
  814. };
  815. #define DECLARE_SMARTP( T ) typedef XPtr< C##T > X##T;
  816. //+---------------------------------------------------------------------------
  817. //
  818. // Class: CNoErrorMode
  819. //
  820. // Purpose: Turns off popups from the system
  821. //
  822. // History: 4-6-00 dlee Created
  823. //
  824. //----------------------------------------------------------------------------
  825. class CNoErrorMode
  826. {
  827. public:
  828. CNoErrorMode()
  829. {
  830. _uiOldMode = SetErrorMode( SEM_NOOPENFILEERRORBOX |
  831. SEM_FAILCRITICALERRORS );
  832. }
  833. ~CNoErrorMode()
  834. {
  835. SetErrorMode( _uiOldMode );
  836. }
  837. private:
  838. UINT _uiOldMode;
  839. };