3 * Copyright 2003-2007, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 // Registry configuration wrappers class
30 // Offers static functions for convenient
31 // fast access for individual values
33 // Also provides a wrapper class for efficient
34 // batch operations on values of a given registry key.
37 #ifndef TALK_BASE_WIN32REGKEY_H_
38 #define TALK_BASE_WIN32REGKEY_H_
43 #include "talk/base/basictypes.h"
44 #include "talk/base/win32.h"
48 // maximum sizes registry key and value names
49 const int kMaxKeyNameChars = 255 + 1;
50 const int kMaxValueNameChars = 16383 + 1;
61 HRESULT Create(HKEY parent_key, const wchar_t* key_name);
63 HRESULT Create(HKEY parent_key,
64 const wchar_t* key_name,
68 LPSECURITY_ATTRIBUTES lp_sec_attr,
69 LPDWORD lp_disposition);
71 // open an existing reg key
72 HRESULT Open(HKEY parent_key, const wchar_t* key_name);
74 HRESULT Open(HKEY parent_key, const wchar_t* key_name, REGSAM sam_desired);
79 // check if the key has a specified value
80 bool HasValue(const wchar_t* value_name) const;
82 // get the number of values for this key
83 uint32 GetValueCount();
85 // Called to get the value name for the given value name index
86 // Use GetValueCount() to get the total value_name count for this key
87 // Returns failure if no key at the specified index
88 // If you modify the key while enumerating, the indexes will be out of order.
89 // Since the index order is not guaranteed, you need to reset your counting
91 // 'type' refers to REG_DWORD, REG_QWORD, etc..
92 // 'type' can be NULL if not interested in the value type
93 HRESULT GetValueNameAt(int index, std::wstring* value_name, DWORD* type);
95 // check if the current key has the specified subkey
96 bool HasSubkey(const wchar_t* key_name) const;
98 // get the number of subkeys for this key
99 uint32 GetSubkeyCount();
101 // Called to get the key name for the given key index
102 // Use GetSubkeyCount() to get the total count for this key
103 // Returns failure if no key at the specified index
104 // If you modify the key while enumerating, the indexes will be out of order.
105 // Since the index order is not guaranteed, you need to reset your counting
107 HRESULT GetSubkeyNameAt(int index, std::wstring* key_name);
111 // set an int32 value - use when reading multiple values from a key
112 HRESULT SetValue(const wchar_t* value_name, DWORD value) const;
114 // set an int64 value
115 HRESULT SetValue(const wchar_t* value_name, DWORD64 value) const;
117 // set a string value
118 HRESULT SetValue(const wchar_t* value_name, const wchar_t* value) const;
121 HRESULT SetValue(const wchar_t* value_name,
123 DWORD byte_count) const;
125 // set raw data, including type
126 HRESULT SetValue(const wchar_t* value_name,
133 // get an int32 value
134 HRESULT GetValue(const wchar_t* value_name, DWORD* value) const;
136 // get an int64 value
137 HRESULT GetValue(const wchar_t* value_name, DWORD64* value) const;
139 // get a string value - the caller must free the return buffer
140 HRESULT GetValue(const wchar_t* value_name, wchar_t** value) const;
142 // get a string value
143 HRESULT GetValue(const wchar_t* value_name, std::wstring* value) const;
145 // get a std::vector<std::wstring> value from REG_MULTI_SZ type
146 HRESULT GetValue(const wchar_t* value_name,
147 std::vector<std::wstring>* value) const;
149 // get binary data - the caller must free the return buffer
150 HRESULT GetValue(const wchar_t* value_name,
152 DWORD* byte_count) const;
154 // get raw data, including type - the caller must free the return buffer
155 HRESULT GetValue(const wchar_t* value_name,
163 static HRESULT FlushKey(const wchar_t* full_key_name);
165 // check if a key exists
166 static bool HasKey(const wchar_t* full_key_name);
168 // check if the key has a specified value
169 static bool HasValue(const wchar_t* full_key_name, const wchar_t* value_name);
174 static HRESULT SetValue(const wchar_t* full_key_name,
175 const wchar_t* value_name,
179 static HRESULT SetValue(const wchar_t* full_key_name,
180 const wchar_t* value_name,
184 static HRESULT SetValue(const wchar_t* full_key_name,
185 const wchar_t* value_name,
189 static HRESULT SetValue(const wchar_t* full_key_name,
190 const wchar_t* value_name,
194 static HRESULT SetValue(const wchar_t* full_key_name,
195 const wchar_t* value_name,
196 const wchar_t* value);
198 // STATIC binary data set
199 static HRESULT SetValue(const wchar_t* full_key_name,
200 const wchar_t* value_name,
204 // STATIC multi-string set
205 static HRESULT SetValueMultiSZ(const wchar_t* full_key_name,
206 const TCHAR* value_name,
213 static HRESULT GetValue(const wchar_t* full_key_name,
214 const wchar_t* value_name,
219 // Note: if you are using time64 you should
220 // likely use GetLimitedTimeValue (util.h) instead of this method.
221 static HRESULT GetValue(const wchar_t* full_key_name,
222 const wchar_t* value_name,
226 static HRESULT GetValue(const wchar_t* full_key_name,
227 const wchar_t* value_name,
231 static HRESULT GetValue(const wchar_t* full_key_name,
232 const wchar_t* value_name,
236 // Note: the caller must free the return buffer for wchar_t* version
237 static HRESULT GetValue(const wchar_t* full_key_name,
238 const wchar_t* value_name,
240 static HRESULT GetValue(const wchar_t* full_key_name,
241 const wchar_t* value_name,
242 std::wstring* value);
244 // STATIC REG_MULTI_SZ get
245 static HRESULT GetValue(const wchar_t* full_key_name,
246 const wchar_t* value_name,
247 std::vector<std::wstring>* value);
249 // STATIC get binary data - the caller must free the return buffer
250 static HRESULT GetValue(const wchar_t* full_key_name,
251 const wchar_t* value_name,
255 // Get type of a registry value
256 static HRESULT GetValueType(const wchar_t* full_key_name,
257 const wchar_t* value_name,
260 // delete a subkey of the current key (with no subkeys)
261 HRESULT DeleteSubKey(const wchar_t* key_name);
263 // recursively delete a sub key of the current key (and all its subkeys)
264 HRESULT RecurseDeleteSubKey(const wchar_t* key_name);
266 // STATIC version of delete key - handles nested keys also
267 // delete a key and all its sub-keys recursively
268 // Returns S_FALSE if key didn't exist, S_OK if deletion was successful,
269 // and failure otherwise.
270 static HRESULT DeleteKey(const wchar_t* full_key_name);
272 // STATIC version of delete key
273 // delete a key recursively or non-recursively
274 // Returns S_FALSE if key didn't exist, S_OK if deletion was successful,
275 // and failure otherwise.
276 static HRESULT DeleteKey(const wchar_t* full_key_name, bool recursive);
278 // delete the specified value
279 HRESULT DeleteValue(const wchar_t* value_name);
281 // STATIC version of delete value
282 // Returns S_FALSE if key didn't exist, S_OK if deletion was successful,
283 // and failure otherwise.
284 static HRESULT DeleteValue(const wchar_t* full_key_name,
285 const wchar_t* value_name);
287 // Peek inside (use a RegKey as a smart wrapper around a registry handle)
288 HKEY key() { return h_key_; }
290 // helper function to get the HKEY and the root key from a string
291 // modifies the argument in place and returns the key name
292 // e.g. HKLM\\Software\\Google\... returns HKLM, "Software\\Google\..."
293 // Necessary for the static versions that use the full name of the reg key
294 static HKEY GetRootKeyInfo(std::wstring* full_key_name);
296 // Returns true if this key name is 'safe' for deletion (doesn't specify a key
298 static bool SafeKeyNameForDeletion(const wchar_t* key_name);
300 // save the key and all of its subkeys and values to a file
301 static HRESULT Save(const wchar_t* full_key_name, const wchar_t* file_name);
303 // restore the key and all of its subkeys and values which are saved into a
305 static HRESULT Restore(const wchar_t* full_key_name,
306 const wchar_t* file_name);
308 // Is the key empty: having no sub-keys and values
309 static bool IsKeyEmpty(const wchar_t* full_key_name);
313 // helper function to get any value from the registry
314 // used when the size of the data is unknown
315 HRESULT GetValueHelper(const wchar_t* value_name,
316 DWORD* type, uint8** value,
317 DWORD* byte_count) const;
319 // helper function to get the parent key name and the subkey from a string
320 // modifies the argument in place and returns the key name
321 // Necessary for the static versions that use the full name of the reg key
322 static std::wstring GetParentKeyInfo(std::wstring* key_name);
324 // common SET Helper for the static case
325 static HRESULT SetValueStaticHelper(const wchar_t* full_key_name,
326 const wchar_t* value_name,
329 DWORD byte_count = 0);
331 // common GET Helper for the static case
332 static HRESULT GetValueStaticHelper(const wchar_t* full_key_name,
333 const wchar_t* value_name,
336 DWORD* byte_count = NULL);
338 // convert REG_MULTI_SZ bytes to string array
339 static HRESULT MultiSZBytesToStringArray(const uint8* buffer,
341 std::vector<std::wstring>* value);
343 // the HKEY for the current key
347 friend void RegKeyHelperFunctionsTest();
349 DISALLOW_EVIL_CONSTRUCTORS(RegKey);
352 } // namespace talk_base
354 #endif // TALK_BASE_WIN32REGKEY_H_