registry.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_WIN_REGISTRY_H_
  5. #define BASE_WIN_REGISTRY_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/base_export.h"
  11. #include "base/callback.h"
  12. #include "base/macros.h"
  13. #include "base/win/object_watcher.h"
  14. #include "base/win/scoped_handle.h"
  15. #include "base/win/windows_types.h"
  16. namespace base {
  17. namespace win {
  18. // Utility class to read, write and manipulate the Windows Registry.
  19. // Registry vocabulary primer: a "key" is like a folder, in which there
  20. // are "values", which are <name, data> pairs, with an associated data type.
  21. //
  22. // Note:
  23. // * ReadValue family of functions guarantee that the out-parameter
  24. // is not touched in case of failure.
  25. // * Functions returning LONG indicate success as ERROR_SUCCESS or an
  26. // error as a (non-zero) win32 error code.
  27. class BASE_EXPORT RegKey {
  28. public:
  29. // Called from the MessageLoop when the key changes.
  30. using ChangeCallback = OnceCallback<void()>;
  31. RegKey();
  32. explicit RegKey(HKEY key);
  33. RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access);
  34. RegKey(RegKey&& other) noexcept;
  35. RegKey& operator=(RegKey&& other);
  36. ~RegKey();
  37. LONG Create(HKEY rootkey, const wchar_t* subkey, REGSAM access);
  38. LONG CreateWithDisposition(HKEY rootkey,
  39. const wchar_t* subkey,
  40. DWORD* disposition,
  41. REGSAM access);
  42. // Creates a subkey or open it if it already exists.
  43. LONG CreateKey(const wchar_t* name, REGSAM access);
  44. // Opens an existing reg key.
  45. LONG Open(HKEY rootkey, const wchar_t* subkey, REGSAM access);
  46. // Opens an existing reg key, given the relative key name.
  47. LONG OpenKey(const wchar_t* relative_key_name, REGSAM access);
  48. // Closes this reg key.
  49. void Close();
  50. // Replaces the handle of the registry key and takes ownership of the handle.
  51. void Set(HKEY key);
  52. // Transfers ownership away from this object.
  53. HKEY Take();
  54. // Returns false if this key does not have the specified value, or if an error
  55. // occurrs while attempting to access it.
  56. bool HasValue(const wchar_t* value_name) const;
  57. // Returns the number of values for this key, or 0 if the number cannot be
  58. // determined.
  59. DWORD GetValueCount() const;
  60. // Determines the nth value's name.
  61. LONG GetValueNameAt(int index, std::wstring* name) const;
  62. // True while the key is valid.
  63. bool Valid() const { return key_ != nullptr; }
  64. // Kills a key and everything that lives below it; please be careful when
  65. // using it.
  66. LONG DeleteKey(const wchar_t* name);
  67. // Deletes an empty subkey. If the subkey has subkeys or values then this
  68. // will fail.
  69. LONG DeleteEmptyKey(const wchar_t* name);
  70. // Deletes a single value within the key.
  71. LONG DeleteValue(const wchar_t* name);
  72. // Getters:
  73. // Reads a REG_DWORD (uint32_t) into |out_value|. If |name| is null or empty,
  74. // reads the key's default value, if any.
  75. LONG ReadValueDW(const wchar_t* name, DWORD* out_value) const;
  76. // Reads a REG_QWORD (int64_t) into |out_value|. If |name| is null or empty,
  77. // reads the key's default value, if any.
  78. LONG ReadInt64(const wchar_t* name, int64_t* out_value) const;
  79. // Reads a string into |out_value|. If |name| is null or empty, reads
  80. // the key's default value, if any.
  81. LONG ReadValue(const wchar_t* name, std::wstring* out_value) const;
  82. // Reads a REG_MULTI_SZ registry field into a vector of strings. Clears
  83. // |values| initially and adds further strings to the list. Returns
  84. // ERROR_CANTREAD if type is not REG_MULTI_SZ.
  85. LONG ReadValues(const wchar_t* name, std::vector<std::wstring>* values);
  86. // Reads raw data into |data|. If |name| is null or empty, reads the key's
  87. // default value, if any.
  88. LONG ReadValue(const wchar_t* name,
  89. void* data,
  90. DWORD* dsize,
  91. DWORD* dtype) const;
  92. // Setters:
  93. // Sets an int32_t value.
  94. LONG WriteValue(const wchar_t* name, DWORD in_value);
  95. // Sets a string value.
  96. LONG WriteValue(const wchar_t* name, const wchar_t* in_value);
  97. // Sets raw data, including type.
  98. LONG WriteValue(const wchar_t* name,
  99. const void* data,
  100. DWORD dsize,
  101. DWORD dtype);
  102. // Starts watching the key to see if any of its values have changed.
  103. // The key must have been opened with the KEY_NOTIFY access privilege.
  104. // Returns true on success.
  105. // To stop watching, delete this RegKey object. To continue watching the
  106. // object after the callback is invoked, call StartWatching again.
  107. bool StartWatching(ChangeCallback callback);
  108. HKEY Handle() const { return key_; }
  109. private:
  110. class Watcher;
  111. // Recursively deletes a key and all of its subkeys.
  112. static LONG RegDelRecurse(HKEY root_key, const wchar_t* name, REGSAM access);
  113. HKEY key_ = nullptr; // The registry key being iterated.
  114. REGSAM wow64access_ = 0;
  115. std::unique_ptr<Watcher> key_watcher_;
  116. DISALLOW_COPY_AND_ASSIGN(RegKey);
  117. };
  118. // Iterates the entries found in a particular folder on the registry.
  119. class BASE_EXPORT RegistryValueIterator {
  120. public:
  121. // Constructs a Registry Value Iterator with default WOW64 access.
  122. RegistryValueIterator(HKEY root_key, const wchar_t* folder_key);
  123. // Constructs a Registry Key Iterator with specific WOW64 access, one of
  124. // KEY_WOW64_32KEY or KEY_WOW64_64KEY, or 0.
  125. // Note: |wow64access| should be the same access used to open |root_key|
  126. // previously, or a predefined key (e.g. HKEY_LOCAL_MACHINE).
  127. // See http://msdn.microsoft.com/en-us/library/windows/desktop/aa384129.aspx.
  128. RegistryValueIterator(HKEY root_key,
  129. const wchar_t* folder_key,
  130. REGSAM wow64access);
  131. ~RegistryValueIterator();
  132. DWORD ValueCount() const;
  133. // True while the iterator is valid.
  134. bool Valid() const;
  135. // Advances to the next registry entry.
  136. void operator++();
  137. const wchar_t* Name() const { return name_.c_str(); }
  138. const wchar_t* Value() const { return value_.data(); }
  139. // ValueSize() is in bytes.
  140. DWORD ValueSize() const { return value_size_; }
  141. DWORD Type() const { return type_; }
  142. int Index() const { return index_; }
  143. private:
  144. // Reads in the current values.
  145. bool Read();
  146. void Initialize(HKEY root_key, const wchar_t* folder_key, REGSAM wow64access);
  147. // The registry key being iterated.
  148. HKEY key_;
  149. // Current index of the iteration.
  150. int index_;
  151. // Current values.
  152. std::wstring name_;
  153. std::vector<wchar_t> value_;
  154. DWORD value_size_;
  155. DWORD type_;
  156. DISALLOW_COPY_AND_ASSIGN(RegistryValueIterator);
  157. };
  158. class BASE_EXPORT RegistryKeyIterator {
  159. public:
  160. // Constructs a Registry Key Iterator with default WOW64 access.
  161. RegistryKeyIterator(HKEY root_key, const wchar_t* folder_key);
  162. // Constructs a Registry Value Iterator with specific WOW64 access, one of
  163. // KEY_WOW64_32KEY or KEY_WOW64_64KEY, or 0.
  164. // Note: |wow64access| should be the same access used to open |root_key|
  165. // previously, or a predefined key (e.g. HKEY_LOCAL_MACHINE).
  166. // See http://msdn.microsoft.com/en-us/library/windows/desktop/aa384129.aspx.
  167. RegistryKeyIterator(HKEY root_key,
  168. const wchar_t* folder_key,
  169. REGSAM wow64access);
  170. ~RegistryKeyIterator();
  171. DWORD SubkeyCount() const;
  172. // True while the iterator is valid.
  173. bool Valid() const;
  174. // Advances to the next entry in the folder.
  175. void operator++();
  176. const wchar_t* Name() const { return name_; }
  177. int Index() const { return index_; }
  178. private:
  179. // Reads in the current values.
  180. bool Read();
  181. void Initialize(HKEY root_key, const wchar_t* folder_key, REGSAM wow64access);
  182. // The registry key being iterated.
  183. HKEY key_;
  184. // Current index of the iteration.
  185. int index_;
  186. wchar_t name_[MAX_PATH];
  187. DISALLOW_COPY_AND_ASSIGN(RegistryKeyIterator);
  188. };
  189. } // namespace win
  190. } // namespace base
  191. #endif // BASE_WIN_REGISTRY_H_