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.

251 lines
6.4 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1997 - 1998 **/
  4. /**********************************************************************/
  5. /*
  6. regkey.h
  7. FILE HISTORY:
  8. */
  9. #ifndef _TREGKEY_H
  10. #define _TREGKEY_H
  11. #if _MSC_VER >= 1000 // VC 5.0 or later
  12. #pragma once
  13. #endif
  14. /*---------------------------------------------------------------------------
  15. String length functions
  16. ---------------------------------------------------------------------------*/
  17. #define StrLen lstrlen
  18. #define StrLenA lstrlenA
  19. #define StrLenW lstrlenW
  20. #define StrLenOle StrLenW
  21. //
  22. // CbStrLenA() is inaccurate for DBCS strings! It will return the
  23. // incorrect number of bytes needed.
  24. //
  25. #define CbStrLenA(psz) ((StrLenA(psz)+1)*sizeof(char))
  26. #define CbStrLenW(psz) ((StrLenW(psz)+1)*sizeof(WCHAR))
  27. #ifdef _UNICODE
  28. #define CbStrLen(psz) CbStrLenW(psz)
  29. #else
  30. #define CbStrLen(psz) CbStrLenA(psz)
  31. #endif
  32. //
  33. // Maximum size of a Registry class name
  34. //
  35. #define CREGKEY_MAX_CLASS_NAME MAX_PATH
  36. #ifndef MaxCchFromCb
  37. // Given a cb (count of bytes) this is the maximal number of characters
  38. // that can be in this string
  39. #define MaxCchFromCb(cb) ((cb) / sizeof(TCHAR))
  40. #endif
  41. #ifndef MinTCharNeededForCch
  42. // that needs to be allocated to hold the string
  43. #define MinTCharNeededForCch(ch) ((ch) * (2/sizeof(TCHAR)))
  44. #endif
  45. #ifndef MinCbNeededForCch
  46. #define MinCbNeededForCch(ch) (sizeof(TCHAR)*MinTCharNeededForCch(ch))
  47. #endif
  48. #ifndef TFS_EXPORT_CLASS
  49. #define TFS_EXPORT_CLASS
  50. #endif
  51. // Convert a CStringList to the REG_MULTI_SZ format
  52. DWORD StrList2MULTI_SZ(CStringList & strList, DWORD * pcbSize, BYTE ** ppbData);
  53. // Convert a REG_MULTI_SZ format to the CStringList
  54. DWORD MULTI_SZ2StrList(LPCTSTR pbMulti_Sz, CStringList & strList);
  55. //
  56. // Wrapper for a Registry key handle.
  57. //
  58. class TFS_EXPORT_CLASS RegKey
  59. {
  60. public:
  61. //
  62. // Key information return structure
  63. //
  64. typedef struct
  65. {
  66. TCHAR chBuff [CREGKEY_MAX_CLASS_NAME] ;
  67. DWORD dwClassNameSize, // size of the class string
  68. dwNumSubKeys, // number of subkeys
  69. dwMaxSubKey, // longest subkey name length
  70. dwMaxClass, // longest class string length
  71. dwMaxValues, // number of value entries
  72. dwMaxValueName, // longest value name length
  73. dwMaxValueData, // longest value data length
  74. dwSecDesc ; // security descriptor length
  75. FILETIME ftKey ;
  76. } CREGKEY_KEY_INFO ;
  77. // Standard constructor
  78. // To get at keys, you will have to open/create the key
  79. RegKey();
  80. ~RegKey ();
  81. DWORD Create(HKEY hKeyParent,
  82. LPCTSTR lpszKeyName,
  83. DWORD dwOptions = REG_OPTION_NON_VOLATILE,
  84. REGSAM samDesired = KEY_ALL_ACCESS,
  85. LPSECURITY_ATTRIBUTES lpSecAttr = NULL,
  86. LPCTSTR pszServerName = NULL);
  87. DWORD Open(HKEY hKeyParent,
  88. LPCTSTR pszSubKey,
  89. REGSAM samDesired = KEY_ALL_ACCESS,
  90. LPCTSTR pszServerName = NULL);
  91. DWORD Close();
  92. HKEY Detach();
  93. void Attach(HKEY hKey);
  94. DWORD DeleteSubKey(LPCTSTR lpszSubKey);
  95. DWORD RecurseDeleteKey(LPCTSTR lpszKey);
  96. DWORD DeleteValue(LPCTSTR lpszValue);
  97. // Deletes the subkeys of the current key (does NOT delete the
  98. // current key).
  99. DWORD RecurseDeleteSubKeys();
  100. //
  101. // Allow a RegKey to be used anywhere an HKEY is required.
  102. //
  103. operator HKEY () const
  104. {
  105. return m_hKey ;
  106. }
  107. //
  108. // Fill a key information structure
  109. //
  110. DWORD QueryKeyInfo ( CREGKEY_KEY_INFO * pRegKeyInfo ) ;
  111. DWORD QueryTypeAndSize(LPCTSTR pszValueName, DWORD *pdwType, DWORD *pdwSize);
  112. //
  113. // Overloaded value query members; each returns ERROR_INVALID_PARAMETER
  114. // if data exists but not in correct form to deliver into result object.
  115. //
  116. DWORD QueryValue ( LPCTSTR pchValueName, ::CString & strResult ) ;
  117. DWORD QueryValue ( LPCTSTR pchValueName, CStringList & strList ) ;
  118. DWORD QueryValue ( LPCTSTR pchValueName, DWORD & dwResult ) ;
  119. DWORD QueryValue ( LPCTSTR pchValueName, CByteArray & abResult ) ;
  120. DWORD QueryValue ( LPCTSTR pchValueName, void * pvResult, DWORD cbSize );
  121. DWORD QueryValue ( LPCTSTR pchValueName, LPTSTR pszDestBuffer, DWORD cbSize, BOOL fExpandSz);
  122. DWORD QueryValueExplicit(LPCTSTR pchValueName,
  123. DWORD *pdwType,
  124. DWORD *pdwSize,
  125. LPBYTE *ppbData);
  126. // Overloaded value setting members.
  127. DWORD SetValue ( LPCTSTR pchValueName, LPCTSTR pszValue,
  128. BOOL fRegExpand = FALSE) ;
  129. DWORD SetValue ( LPCTSTR pchValueName, CStringList & strList ) ;
  130. DWORD SetValue ( LPCTSTR pchValueName, DWORD & dwResult ) ;
  131. DWORD SetValue ( LPCTSTR pchValueName, CByteArray & abResult ) ;
  132. DWORD SetValue ( LPCTSTR pchValueName, void * pvResult, DWORD cbSize );
  133. DWORD SetValueExplicit(LPCTSTR pchValueName,
  134. DWORD dwType,
  135. DWORD dwSize,
  136. LPBYTE pbData);
  137. protected:
  138. HKEY m_hKey;
  139. // Prepare to read a value by finding the value's size.
  140. DWORD PrepareValue (
  141. LPCTSTR pchValueName,
  142. DWORD * pdwType,
  143. DWORD * pcbSize,
  144. BYTE ** ppbData
  145. );
  146. // Convert a CStringList to the REG_MULTI_SZ format
  147. static DWORD FlattenValue (
  148. CStringList & strList,
  149. DWORD * pcbSize,
  150. BYTE ** ppbData
  151. );
  152. // Convert a CByteArray to a REG_BINARY block
  153. static DWORD FlattenValue (
  154. CByteArray & abData,
  155. DWORD * pcbSize,
  156. BYTE ** ppbData
  157. );
  158. };
  159. //
  160. // Iterate the values of a key, return the name and type
  161. // of each.
  162. //
  163. class TFS_EXPORT_CLASS RegValueIterator
  164. {
  165. protected:
  166. RegKey * m_pRegKey;
  167. DWORD m_dwIndex ;
  168. TCHAR * m_pszBuffer ;
  169. DWORD m_cbBuffer ;
  170. public:
  171. RegValueIterator();
  172. ~ RegValueIterator () ;
  173. HRESULT Init(RegKey *pRegKey);
  174. //
  175. // Get the name (and optional last write time) of the next key.
  176. //
  177. HRESULT Next ( ::CString * pstName, DWORD * pdwType ) ;
  178. //
  179. // Reset the iterator
  180. //
  181. void Reset ()
  182. {
  183. m_dwIndex = 0 ;
  184. }
  185. };
  186. //
  187. // Iterate the sub-key names of a key.
  188. //
  189. class TFS_EXPORT_CLASS RegKeyIterator
  190. {
  191. public:
  192. RegKeyIterator();
  193. ~RegKeyIterator();
  194. HRESULT Init(RegKey *pRegKey);
  195. HRESULT Next(::CString *pstName, CTime *pTime = NULL);
  196. HRESULT Reset();
  197. protected:
  198. RegKey * m_pregkey;
  199. DWORD m_dwIndex;
  200. TCHAR * m_pszBuffer;
  201. DWORD m_cbBuffer;
  202. };
  203. #endif _TREGKEY_H