/* **************************************************************************** | Copyright (C) 2002 Microsoft Corporation | | Component / Subcomponent | IIS 6.0 / IIS Migration Wizard | | Based on: | http://iis6/Specs/IIS%20Migration6.0_Final.doc | | Abstract: | Wrapper classes | | Author: | ivelinj | | Revision History: | V1.00 March 2002 | **************************************************************************** */ #pragma once // Class for auto managing handles ( smart handle ) //////////////////////////////////////////////////////////////////////////// template class THandle { public: THandle() { m_Handle = InvalidVal; } explicit THandle( const T& handle ) { m_Handle = handle; } THandle( const THandle& src ) { m_Handle = src.Relase(); } ~THandle() { Close(); } void Attach( const T& NewVal ) { Close(); m_Handle = NewVal; } const T Detach() { T Result = m_Handle; m_Handle = InvalidVal; return Result; } void Close() { if ( m_Handle != InvalidVal ) { VERIFY( PFN_FREE( m_Handle ) ); m_Handle = InvalidVal; } } bool IsValid()const{ return m_Handle != InvalidVal; } T* operator &(){ Close(); return &m_Handle; } const T get()const { return m_Handle;} void operator= ( const T& RVal ) { Close(); m_Handle = RVal; } THandle& operator=( const THandle& RVal ) { Close(); m_Handle = RVal.Relase(); return *this; } private: const T Relase()const { T Result = m_Handle; m_Handle = InvalidVal; return Result; } private: mutable T m_Handle; }; // Adaptor for WINAPI functions that accept second DWORD param, which is always 0 template inline BOOL __stdcall Adapt2nd( T hCtx ) { return PFN_FREE( hCtx, 0 ); } // Adaptor for WINAPI functions that do not retun result template inline BOOL __stdcall AdaptNoRet( T hCtx ) { PFN_FREE( hCtx ); return TRUE; } // Predefined wrappers ///////////////////////////////////////////////////////////////////////////////////////// // General typedef THandle TFileHandle; // Win32 Files typedef THandle TStdHandle; // Win32 HANDLEs typedef THandle TSearchHandle; // FindFirstFile handles typedef THandle TLibHandle; // DLL module handle // Crypt #ifdef __WINCRYPT_H__ typedef THandle > TCryptProvHandle; // Crypt provider typedef THandle TCryptHashHandle; // Crypt hash typedef THandle TCryptKeyHandle; // Crypt key typedef THandle > TCertStoreHandle; // Cert store typedef THandle TCertContextHandle; // Cert context*/ typedef THandle > TCertChainHandle; // Cert chain #endif // Used instead of auto_ptr as auto_ptr cannot be used with STL containers // See CInPackage class for usage details ///////////////////////////////////////////////////////////////////////////////////////// class _sid_ptr { public: explicit _sid_ptr( PSID pSID ) { CopyFrom( pSID ); } _sid_ptr( const _sid_ptr& s ) { CopyFrom( s.m_pSid ); } ~_sid_ptr() { delete m_pSid; } PSID get()const{ return m_pSid; } private: PSID m_pSid; private: void operator=( const _sid_ptr& ); void operator==( const _sid_ptr& ); void CopyFrom( PSID pSID ) { _ASSERT( ::IsValidSid( pSID ) ); m_pSid = new BYTE[ ::GetLengthSid( pSID ) ]; VERIFY( ::CopySid( ::GetLengthSid( pSID ), m_pSid, pSID ) ); } };