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.
5 #ifndef BASE_WIN_REGISTRY_H_
6 #define BASE_WIN_REGISTRY_H_
12 #include "base/base_export.h"
13 #include "base/basictypes.h"
14 #include "base/stl_util.h"
15 #include "base/win/object_watcher.h"
16 #include "base/win/scoped_handle.h"
21 // Utility class to read, write and manipulate the Windows Registry.
22 // Registry vocabulary primer: a "key" is like a folder, in which there
23 // are "values", which are <name, data> pairs, with an associated data type.
26 // ReadValue family of functions guarantee that the return arguments
27 // are not touched in case of failure.
28 class BASE_EXPORT RegKey
{
30 // Called from the MessageLoop when the key changes.
31 typedef base::Callback
<void()> ChangeCallback
;
34 explicit RegKey(HKEY key
);
35 RegKey(HKEY rootkey
, const wchar_t* subkey
, REGSAM access
);
38 LONG
Create(HKEY rootkey
, const wchar_t* subkey
, REGSAM access
);
40 LONG
CreateWithDisposition(HKEY rootkey
, const wchar_t* subkey
,
41 DWORD
* disposition
, REGSAM access
);
43 // Creates a subkey or open it if it already exists.
44 LONG
CreateKey(const wchar_t* name
, REGSAM access
);
46 // Opens an existing reg key.
47 LONG
Open(HKEY rootkey
, const wchar_t* subkey
, REGSAM access
);
49 // Opens an existing reg key, given the relative key name.
50 LONG
OpenKey(const wchar_t* relative_key_name
, REGSAM access
);
52 // Closes this reg key.
55 // Replaces the handle of the registry key and takes ownership of the handle.
58 // Transfers ownership away from this object.
61 // Returns false if this key does not have the specified value, or if an error
62 // occurrs while attempting to access it.
63 bool HasValue(const wchar_t* value_name
) const;
65 // Returns the number of values for this key, or 0 if the number cannot be
67 DWORD
GetValueCount() const;
69 // Determine the nth value's name.
70 LONG
GetValueNameAt(int index
, std::wstring
* name
) const;
72 // True while the key is valid.
73 bool Valid() const { return key_
!= NULL
; }
75 // Kill a key and everything that live below it; please be careful when using
77 LONG
DeleteKey(const wchar_t* name
);
79 // Deletes an empty subkey. If the subkey has subkeys or values then this
81 LONG
DeleteEmptyKey(const wchar_t* name
);
83 // Deletes a single value within the key.
84 LONG
DeleteValue(const wchar_t* name
);
88 // Returns an int32 value. If |name| is NULL or empty, returns the default
90 LONG
ReadValueDW(const wchar_t* name
, DWORD
* out_value
) const;
92 // Returns an int64 value. If |name| is NULL or empty, returns the default
94 LONG
ReadInt64(const wchar_t* name
, int64
* out_value
) const;
96 // Returns a string value. If |name| is NULL or empty, returns the default
98 LONG
ReadValue(const wchar_t* name
, std::wstring
* out_value
) const;
100 // Reads a REG_MULTI_SZ registry field into a vector of strings. Clears
101 // |values| initially and adds further strings to the list. Returns
102 // ERROR_CANTREAD if type is not REG_MULTI_SZ.
103 LONG
ReadValues(const wchar_t* name
, std::vector
<std::wstring
>* values
);
105 // Returns raw data. If |name| is NULL or empty, returns the default
107 LONG
ReadValue(const wchar_t* name
,
114 // Sets an int32 value.
115 LONG
WriteValue(const wchar_t* name
, DWORD in_value
);
117 // Sets a string value.
118 LONG
WriteValue(const wchar_t* name
, const wchar_t* in_value
);
120 // Sets raw data, including type.
121 LONG
WriteValue(const wchar_t* name
,
126 // Starts watching the key to see if any of its values have changed.
127 // The key must have been opened with the KEY_NOTIFY access privilege.
128 // Returns true on success.
129 // To stop watching, delete this RegKey object. To continue watching the
130 // object after the callback is invoked, call StartWatching again.
131 bool StartWatching(const ChangeCallback
& callback
);
133 HKEY
Handle() const { return key_
; }
138 // Calls RegDeleteKeyEx on supported platforms, alternatively falls back to
140 static LONG
RegDeleteKeyExWrapper(HKEY hKey
,
141 const wchar_t* lpSubKey
,
145 // Recursively deletes a key and all of its subkeys.
146 static LONG
RegDelRecurse(HKEY root_key
,
147 const std::wstring
& name
,
150 HKEY key_
; // The registry key being iterated.
152 scoped_ptr
<Watcher
> key_watcher_
;
154 DISALLOW_COPY_AND_ASSIGN(RegKey
);
157 // Iterates the entries found in a particular folder on the registry.
158 class BASE_EXPORT RegistryValueIterator
{
160 // Construct a Registry Value Iterator with default WOW64 access.
161 RegistryValueIterator(HKEY root_key
, const wchar_t* folder_key
);
163 // Construct a Registry Key Iterator with specific WOW64 access, one of
164 // KEY_WOW64_32KEY or KEY_WOW64_64KEY, or 0.
165 // Note: |wow64access| should be the same access used to open |root_key|
166 // previously, or a predefined key (e.g. HKEY_LOCAL_MACHINE).
167 // See http://msdn.microsoft.com/en-us/library/windows/desktop/aa384129.aspx.
168 RegistryValueIterator(HKEY root_key
,
169 const wchar_t* folder_key
,
172 ~RegistryValueIterator();
174 DWORD
ValueCount() const;
176 // True while the iterator is valid.
179 // Advances to the next registry entry.
182 const wchar_t* Name() const { return name_
.c_str(); }
183 const wchar_t* Value() const { return vector_as_array(&value_
); }
184 // ValueSize() is in bytes.
185 DWORD
ValueSize() const { return value_size_
; }
186 DWORD
Type() const { return type_
; }
188 int Index() const { return index_
; }
191 // Read in the current values.
194 void Initialize(HKEY root_key
, const wchar_t* folder_key
, REGSAM wow64access
);
196 // The registry key being iterated.
199 // Current index of the iteration.
204 std::vector
<wchar_t> value_
;
208 DISALLOW_COPY_AND_ASSIGN(RegistryValueIterator
);
211 class BASE_EXPORT RegistryKeyIterator
{
213 // Construct a Registry Key Iterator with default WOW64 access.
214 RegistryKeyIterator(HKEY root_key
, const wchar_t* folder_key
);
216 // Construct a Registry Value Iterator with specific WOW64 access, one of
217 // KEY_WOW64_32KEY or KEY_WOW64_64KEY, or 0.
218 // Note: |wow64access| should be the same access used to open |root_key|
219 // previously, or a predefined key (e.g. HKEY_LOCAL_MACHINE).
220 // See http://msdn.microsoft.com/en-us/library/windows/desktop/aa384129.aspx.
221 RegistryKeyIterator(HKEY root_key
,
222 const wchar_t* folder_key
,
225 ~RegistryKeyIterator();
227 DWORD
SubkeyCount() const;
229 // True while the iterator is valid.
232 // Advances to the next entry in the folder.
235 const wchar_t* Name() const { return name_
; }
237 int Index() const { return index_
; }
240 // Read in the current values.
243 void Initialize(HKEY root_key
, const wchar_t* folder_key
, REGSAM wow64access
);
245 // The registry key being iterated.
248 // Current index of the iteration.
251 wchar_t name_
[MAX_PATH
];
253 DISALLOW_COPY_AND_ASSIGN(RegistryKeyIterator
);
259 #endif // BASE_WIN_REGISTRY_H_