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.

208 lines
4.2 KiB

  1. /*
  2. ****************************************************************************
  3. | Copyright (C) 2002 Microsoft Corporation
  4. |
  5. | Component / Subcomponent
  6. | IIS 6.0 / IIS Migration Wizard
  7. |
  8. | Based on:
  9. | http://iis6/Specs/IIS%20Migration6.0_Final.doc
  10. |
  11. | Abstract:
  12. | Wrapper classes
  13. |
  14. | Author:
  15. | ivelinj
  16. |
  17. | Revision History:
  18. | V1.00 March 2002
  19. |
  20. ****************************************************************************
  21. */
  22. #pragma once
  23. // Class for auto managing handles ( smart handle )
  24. ////////////////////////////////////////////////////////////////////////////
  25. template <typename T, BOOL (__stdcall *PFN_FREE)( T ), T InvalidVal = NULL>
  26. class THandle
  27. {
  28. public:
  29. THandle()
  30. {
  31. m_Handle = InvalidVal;
  32. }
  33. explicit THandle( const T& handle )
  34. {
  35. m_Handle = handle;
  36. }
  37. THandle( const THandle<T, PFN_FREE, InvalidVal>& src )
  38. {
  39. m_Handle = src.Relase();
  40. }
  41. ~THandle()
  42. {
  43. Close();
  44. }
  45. void Attach( const T& NewVal )
  46. {
  47. Close();
  48. m_Handle = NewVal;
  49. }
  50. const T Detach()
  51. {
  52. T Result = m_Handle;
  53. m_Handle = InvalidVal;
  54. return Result;
  55. }
  56. void Close()
  57. {
  58. if ( m_Handle != InvalidVal )
  59. {
  60. VERIFY( PFN_FREE( m_Handle ) );
  61. m_Handle = InvalidVal;
  62. }
  63. }
  64. bool IsValid()const{ return m_Handle != InvalidVal; }
  65. T* operator &(){ Close(); return &m_Handle; }
  66. const T get()const { return m_Handle;}
  67. void operator= ( const T& RVal )
  68. {
  69. Close();
  70. m_Handle = RVal;
  71. }
  72. THandle<T, PFN_FREE, InvalidVal>& operator=( const THandle<T, PFN_FREE, InvalidVal>& RVal )
  73. {
  74. Close();
  75. m_Handle = RVal.Relase();
  76. return *this;
  77. }
  78. private:
  79. const T Relase()const
  80. {
  81. T Result = m_Handle;
  82. m_Handle = InvalidVal;
  83. return Result;
  84. }
  85. private:
  86. mutable T m_Handle;
  87. };
  88. // Adaptor for WINAPI functions that accept second DWORD param, which is always 0
  89. template<typename T, BOOL (__stdcall *PFN_FREE)( T, DWORD ) >
  90. inline BOOL __stdcall Adapt2nd( T hCtx )
  91. {
  92. return PFN_FREE( hCtx, 0 );
  93. }
  94. // Adaptor for WINAPI functions that do not retun result
  95. template<typename T, void (__stdcall *PFN_FREE)( T ) >
  96. inline BOOL __stdcall AdaptNoRet( T hCtx )
  97. {
  98. PFN_FREE( hCtx );
  99. return TRUE;
  100. }
  101. // Predefined wrappers
  102. /////////////////////////////////////////////////////////////////////////////////////////
  103. // General
  104. typedef THandle<HANDLE, ::CloseHandle, INVALID_HANDLE_VALUE> TFileHandle; // Win32 Files
  105. typedef THandle<HANDLE, ::CloseHandle> TStdHandle; // Win32 HANDLEs
  106. typedef THandle<HANDLE, ::FindClose, INVALID_HANDLE_VALUE> TSearchHandle; // FindFirstFile handles
  107. typedef THandle<HMODULE, ::FreeLibrary> TLibHandle; // DLL module handle
  108. // Crypt
  109. #ifdef __WINCRYPT_H__
  110. typedef THandle<HCRYPTPROV,
  111. Adapt2nd<HCRYPTPROV, ::CryptReleaseContext> > TCryptProvHandle; // Crypt provider
  112. typedef THandle<HCRYPTHASH, ::CryptDestroyHash> TCryptHashHandle; // Crypt hash
  113. typedef THandle<HCRYPTKEY, ::CryptDestroyKey> TCryptKeyHandle; // Crypt key
  114. typedef THandle<HCERTSTORE,
  115. Adapt2nd<HCERTSTORE, ::CertCloseStore> > TCertStoreHandle; // Cert store
  116. typedef THandle<PCCERT_CONTEXT, ::CertFreeCertificateContext> TCertContextHandle; // Cert context*/
  117. typedef THandle<PCCERT_CHAIN_CONTEXT,
  118. AdaptNoRet<PCCERT_CHAIN_CONTEXT, ::CertFreeCertificateChain> > TCertChainHandle; // Cert chain
  119. #endif
  120. // Used instead of auto_ptr as auto_ptr cannot be used with STL containers
  121. // See CInPackage class for usage details
  122. /////////////////////////////////////////////////////////////////////////////////////////
  123. class _sid_ptr
  124. {
  125. public:
  126. explicit _sid_ptr( PSID pSID )
  127. {
  128. CopyFrom( pSID );
  129. }
  130. _sid_ptr( const _sid_ptr& s )
  131. {
  132. CopyFrom( s.m_pSid );
  133. }
  134. ~_sid_ptr()
  135. {
  136. delete m_pSid;
  137. }
  138. PSID get()const{ return m_pSid; }
  139. private:
  140. PSID m_pSid;
  141. private:
  142. void operator=( const _sid_ptr& );
  143. void operator==( const _sid_ptr& );
  144. void CopyFrom( PSID pSID )
  145. {
  146. _ASSERT( ::IsValidSid( pSID ) );
  147. m_pSid = new BYTE[ ::GetLengthSid( pSID ) ];
  148. VERIFY( ::CopySid( ::GetLengthSid( pSID ), m_pSid, pSID ) );
  149. }
  150. };