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.

354 lines
13 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2003-2007, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  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.
  15. *
  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.
  26. */
  27. // Registry configuration wrappers class
  28. //
  29. // Offers static functions for convenient
  30. // fast access for individual values
  31. //
  32. // Also provides a wrapper class for efficient
  33. // batch operations on values of a given registry key.
  34. //
  35. #ifndef TALK_BASE_WIN32REGKEY_H_
  36. #define TALK_BASE_WIN32REGKEY_H_
  37. #include <string>
  38. #include <vector>
  39. #include "talk/base/basictypes.h"
  40. #include "talk/base/win32.h"
  41. namespace talk_base {
  42. // maximum sizes registry key and value names
  43. const int kMaxKeyNameChars = 255 + 1;
  44. const int kMaxValueNameChars = 16383 + 1;
  45. class RegKey {
  46. public:
  47. // constructor
  48. RegKey();
  49. // destructor
  50. ~RegKey();
  51. // create a reg key
  52. HRESULT Create(HKEY parent_key, const wchar_t* key_name);
  53. HRESULT Create(HKEY parent_key,
  54. const wchar_t* key_name,
  55. wchar_t* reg_class,
  56. DWORD options,
  57. REGSAM sam_desired,
  58. LPSECURITY_ATTRIBUTES lp_sec_attr,
  59. LPDWORD lp_disposition);
  60. // open an existing reg key
  61. HRESULT Open(HKEY parent_key, const wchar_t* key_name);
  62. HRESULT Open(HKEY parent_key, const wchar_t* key_name, REGSAM sam_desired);
  63. // close this reg key
  64. HRESULT Close();
  65. // check if the key has a specified value
  66. bool HasValue(const wchar_t* value_name) const;
  67. // get the number of values for this key
  68. uint32 GetValueCount();
  69. // Called to get the value name for the given value name index
  70. // Use GetValueCount() to get the total value_name count for this key
  71. // Returns failure if no key at the specified index
  72. // If you modify the key while enumerating, the indexes will be out of order.
  73. // Since the index order is not guaranteed, you need to reset your counting
  74. // loop.
  75. // 'type' refers to REG_DWORD, REG_QWORD, etc..
  76. // 'type' can be NULL if not interested in the value type
  77. HRESULT GetValueNameAt(int index, std::wstring* value_name, DWORD* type);
  78. // check if the current key has the specified subkey
  79. bool HasSubkey(const wchar_t* key_name) const;
  80. // get the number of subkeys for this key
  81. uint32 GetSubkeyCount();
  82. // Called to get the key name for the given key index
  83. // Use GetSubkeyCount() to get the total count for this key
  84. // Returns failure if no key at the specified index
  85. // If you modify the key while enumerating, the indexes will be out of order.
  86. // Since the index order is not guaranteed, you need to reset your counting
  87. // loop.
  88. HRESULT GetSubkeyNameAt(int index, std::wstring* key_name);
  89. // SETTERS
  90. // set an int32 value - use when reading multiple values from a key
  91. HRESULT SetValue(const wchar_t* value_name, DWORD value) const;
  92. // set an int64 value
  93. HRESULT SetValue(const wchar_t* value_name, DWORD64 value) const;
  94. // set a string value
  95. HRESULT SetValue(const wchar_t* value_name, const wchar_t* value) const;
  96. // set binary data
  97. HRESULT SetValue(const wchar_t* value_name,
  98. const uint8* value,
  99. DWORD byte_count) const;
  100. // set raw data, including type
  101. HRESULT SetValue(const wchar_t* value_name,
  102. const uint8* value,
  103. DWORD byte_count,
  104. DWORD type) const;
  105. // GETTERS
  106. // get an int32 value
  107. HRESULT GetValue(const wchar_t* value_name, DWORD* value) const;
  108. // get an int64 value
  109. HRESULT GetValue(const wchar_t* value_name, DWORD64* value) const;
  110. // get a string value - the caller must free the return buffer
  111. HRESULT GetValue(const wchar_t* value_name, wchar_t** value) const;
  112. // get a string value
  113. HRESULT GetValue(const wchar_t* value_name, std::wstring* value) const;
  114. // get a std::vector<std::wstring> value from REG_MULTI_SZ type
  115. HRESULT GetValue(const wchar_t* value_name,
  116. std::vector<std::wstring>* value) const;
  117. // get binary data - the caller must free the return buffer
  118. HRESULT GetValue(const wchar_t* value_name,
  119. uint8** value,
  120. DWORD* byte_count) const;
  121. // get raw data, including type - the caller must free the return buffer
  122. HRESULT GetValue(const wchar_t* value_name,
  123. uint8** value,
  124. DWORD* byte_count,
  125. DWORD* type) const;
  126. // STATIC VERSIONS
  127. // flush
  128. static HRESULT FlushKey(const wchar_t* full_key_name);
  129. // check if a key exists
  130. static bool HasKey(const wchar_t* full_key_name);
  131. // check if the key has a specified value
  132. static bool HasValue(const wchar_t* full_key_name, const wchar_t* value_name);
  133. // SETTERS
  134. // STATIC int32 set
  135. static HRESULT SetValue(const wchar_t* full_key_name,
  136. const wchar_t* value_name,
  137. DWORD value);
  138. // STATIC int64 set
  139. static HRESULT SetValue(const wchar_t* full_key_name,
  140. const wchar_t* value_name,
  141. DWORD64 value);
  142. // STATIC float set
  143. static HRESULT SetValue(const wchar_t* full_key_name,
  144. const wchar_t* value_name,
  145. float value);
  146. // STATIC double set
  147. static HRESULT SetValue(const wchar_t* full_key_name,
  148. const wchar_t* value_name,
  149. double value);
  150. // STATIC string set
  151. static HRESULT SetValue(const wchar_t* full_key_name,
  152. const wchar_t* value_name,
  153. const wchar_t* value);
  154. // STATIC binary data set
  155. static HRESULT SetValue(const wchar_t* full_key_name,
  156. const wchar_t* value_name,
  157. const uint8* value,
  158. DWORD byte_count);
  159. // STATIC multi-string set
  160. static HRESULT SetValueMultiSZ(const wchar_t* full_key_name,
  161. const TCHAR* value_name,
  162. const uint8* value,
  163. DWORD byte_count);
  164. // GETTERS
  165. // STATIC int32 get
  166. static HRESULT GetValue(const wchar_t* full_key_name,
  167. const wchar_t* value_name,
  168. DWORD* value);
  169. // STATIC int64 get
  170. //
  171. // Note: if you are using time64 you should
  172. // likely use GetLimitedTimeValue (util.h) instead of this method.
  173. static HRESULT GetValue(const wchar_t* full_key_name,
  174. const wchar_t* value_name,
  175. DWORD64* value);
  176. // STATIC float get
  177. static HRESULT GetValue(const wchar_t* full_key_name,
  178. const wchar_t* value_name,
  179. float* value);
  180. // STATIC double get
  181. static HRESULT GetValue(const wchar_t* full_key_name,
  182. const wchar_t* value_name,
  183. double* value);
  184. // STATIC string get
  185. // Note: the caller must free the return buffer for wchar_t* version
  186. static HRESULT GetValue(const wchar_t* full_key_name,
  187. const wchar_t* value_name,
  188. wchar_t** value);
  189. static HRESULT GetValue(const wchar_t* full_key_name,
  190. const wchar_t* value_name,
  191. std::wstring* value);
  192. // STATIC REG_MULTI_SZ get
  193. static HRESULT GetValue(const wchar_t* full_key_name,
  194. const wchar_t* value_name,
  195. std::vector<std::wstring>* value);
  196. // STATIC get binary data - the caller must free the return buffer
  197. static HRESULT GetValue(const wchar_t* full_key_name,
  198. const wchar_t* value_name,
  199. uint8** value,
  200. DWORD* byte_count);
  201. // Get type of a registry value
  202. static HRESULT GetValueType(const wchar_t* full_key_name,
  203. const wchar_t* value_name,
  204. DWORD* value_type);
  205. // delete a subkey of the current key (with no subkeys)
  206. HRESULT DeleteSubKey(const wchar_t* key_name);
  207. // recursively delete a sub key of the current key (and all its subkeys)
  208. HRESULT RecurseDeleteSubKey(const wchar_t* key_name);
  209. // STATIC version of delete key - handles nested keys also
  210. // delete a key and all its sub-keys recursively
  211. // Returns S_FALSE if key didn't exist, S_OK if deletion was successful,
  212. // and failure otherwise.
  213. static HRESULT DeleteKey(const wchar_t* full_key_name);
  214. // STATIC version of delete key
  215. // delete a key recursively or non-recursively
  216. // Returns S_FALSE if key didn't exist, S_OK if deletion was successful,
  217. // and failure otherwise.
  218. static HRESULT DeleteKey(const wchar_t* full_key_name, bool recursive);
  219. // delete the specified value
  220. HRESULT DeleteValue(const wchar_t* value_name);
  221. // STATIC version of delete value
  222. // Returns S_FALSE if key didn't exist, S_OK if deletion was successful,
  223. // and failure otherwise.
  224. static HRESULT DeleteValue(const wchar_t* full_key_name,
  225. const wchar_t* value_name);
  226. // Peek inside (use a RegKey as a smart wrapper around a registry handle)
  227. HKEY key() { return h_key_; }
  228. // helper function to get the HKEY and the root key from a string
  229. // modifies the argument in place and returns the key name
  230. // e.g. HKLM\\Software\\Google\... returns HKLM, "Software\\Google\..."
  231. // Necessary for the static versions that use the full name of the reg key
  232. static HKEY GetRootKeyInfo(std::wstring* full_key_name);
  233. // Returns true if this key name is 'safe' for deletion (doesn't specify a key
  234. // root)
  235. static bool SafeKeyNameForDeletion(const wchar_t* key_name);
  236. // save the key and all of its subkeys and values to a file
  237. static HRESULT Save(const wchar_t* full_key_name, const wchar_t* file_name);
  238. // restore the key and all of its subkeys and values which are saved into a
  239. // file
  240. static HRESULT Restore(const wchar_t* full_key_name,
  241. const wchar_t* file_name);
  242. // Is the key empty: having no sub-keys and values
  243. static bool IsKeyEmpty(const wchar_t* full_key_name);
  244. private:
  245. // helper function to get any value from the registry
  246. // used when the size of the data is unknown
  247. HRESULT GetValueHelper(const wchar_t* value_name,
  248. DWORD* type, uint8** value,
  249. DWORD* byte_count) const;
  250. // helper function to get the parent key name and the subkey from a string
  251. // modifies the argument in place and returns the key name
  252. // Necessary for the static versions that use the full name of the reg key
  253. static std::wstring GetParentKeyInfo(std::wstring* key_name);
  254. // common SET Helper for the static case
  255. static HRESULT SetValueStaticHelper(const wchar_t* full_key_name,
  256. const wchar_t* value_name,
  257. DWORD type,
  258. LPVOID value,
  259. DWORD byte_count = 0);
  260. // common GET Helper for the static case
  261. static HRESULT GetValueStaticHelper(const wchar_t* full_key_name,
  262. const wchar_t* value_name,
  263. DWORD type,
  264. LPVOID value,
  265. DWORD* byte_count = NULL);
  266. // convert REG_MULTI_SZ bytes to string array
  267. static HRESULT MultiSZBytesToStringArray(const uint8* buffer,
  268. DWORD byte_count,
  269. std::vector<std::wstring>* value);
  270. // the HKEY for the current key
  271. HKEY h_key_;
  272. // for unittest
  273. friend void RegKeyHelperFunctionsTest();
  274. DISALLOW_EVIL_CONSTRUCTORS(RegKey);
  275. };
  276. } // namespace talk_base
  277. #endif // TALK_BASE_WIN32REGKEY_H_