registry.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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. #include "base/win/registry.h"
  5. #include <stddef.h>
  6. #include <algorithm>
  7. #include <iterator>
  8. #include <memory>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include "base/callback.h"
  13. #include "base/check_op.h"
  14. #include "base/notreached.h"
  15. #include "base/strings/string_util.h"
  16. #include "base/strings/string_util_win.h"
  17. #include "base/threading/thread_restrictions.h"
  18. #include "base/win/object_watcher.h"
  19. #include "base/win/scoped_handle.h"
  20. #include "base/win/shlwapi.h"
  21. #include "base/win/windows_version.h"
  22. namespace base {
  23. namespace win {
  24. namespace {
  25. // RegEnumValue() reports the number of characters from the name that were
  26. // written to the buffer, not how many there are. This constant is the maximum
  27. // name size, such that a buffer with this size should read any name.
  28. constexpr DWORD MAX_REGISTRY_NAME_SIZE = 16384;
  29. // Registry values are read as BYTE* but can have wchar_t* data whose last
  30. // wchar_t is truncated. This function converts the reported |byte_size| to
  31. // a size in wchar_t that can store a truncated wchar_t if necessary.
  32. inline DWORD to_wchar_size(DWORD byte_size) {
  33. return (byte_size + sizeof(wchar_t) - 1) / sizeof(wchar_t);
  34. }
  35. // Mask to pull WOW64 access flags out of REGSAM access.
  36. constexpr REGSAM kWow64AccessMask = KEY_WOW64_32KEY | KEY_WOW64_64KEY;
  37. constexpr DWORD kInvalidIterValue = static_cast<DWORD>(-1);
  38. } // namespace
  39. // Watches for modifications to a key.
  40. class RegKey::Watcher : public ObjectWatcher::Delegate {
  41. public:
  42. Watcher() = default;
  43. Watcher(const Watcher&) = delete;
  44. Watcher& operator=(const Watcher&) = delete;
  45. ~Watcher() override = default;
  46. bool StartWatching(HKEY key, ChangeCallback callback);
  47. // ObjectWatcher::Delegate:
  48. void OnObjectSignaled(HANDLE object) override {
  49. DCHECK(watch_event_.is_valid());
  50. DCHECK_EQ(watch_event_.get(), object);
  51. std::move(callback_).Run();
  52. }
  53. private:
  54. ScopedHandle watch_event_;
  55. ObjectWatcher object_watcher_;
  56. ChangeCallback callback_;
  57. };
  58. bool RegKey::Watcher::StartWatching(HKEY key, ChangeCallback callback) {
  59. DCHECK(key);
  60. DCHECK(callback_.is_null());
  61. if (!watch_event_.is_valid())
  62. watch_event_.Set(CreateEvent(nullptr, TRUE, FALSE, nullptr));
  63. if (!watch_event_.is_valid())
  64. return false;
  65. DWORD filter = REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_ATTRIBUTES |
  66. REG_NOTIFY_CHANGE_LAST_SET | REG_NOTIFY_CHANGE_SECURITY;
  67. if (base::win::GetVersion() >= base::win::Version::WIN8)
  68. filter |= REG_NOTIFY_THREAD_AGNOSTIC;
  69. // Watch the registry key for a change of value.
  70. LONG result =
  71. RegNotifyChangeKeyValue(key, /*bWatchSubtree=*/TRUE, filter,
  72. watch_event_.get(), /*fAsynchronous=*/TRUE);
  73. if (result != ERROR_SUCCESS) {
  74. watch_event_.Close();
  75. return false;
  76. }
  77. callback_ = std::move(callback);
  78. return object_watcher_.StartWatchingOnce(watch_event_.get(), this);
  79. }
  80. // RegKey ----------------------------------------------------------------------
  81. RegKey::RegKey() = default;
  82. RegKey::RegKey(HKEY key) : key_(key) {}
  83. RegKey::RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access) {
  84. if (rootkey) {
  85. if (access & (KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_CREATE_LINK))
  86. Create(rootkey, subkey, access);
  87. else
  88. Open(rootkey, subkey, access);
  89. } else {
  90. DCHECK(!subkey);
  91. wow64access_ = access & kWow64AccessMask;
  92. }
  93. }
  94. RegKey::RegKey(RegKey&& other) noexcept
  95. : key_(other.key_),
  96. wow64access_(other.wow64access_),
  97. key_watcher_(std::move(other.key_watcher_)) {
  98. other.key_ = nullptr;
  99. other.wow64access_ = 0;
  100. }
  101. RegKey& RegKey::operator=(RegKey&& other) {
  102. Close();
  103. std::swap(key_, other.key_);
  104. std::swap(wow64access_, other.wow64access_);
  105. key_watcher_ = std::move(other.key_watcher_);
  106. return *this;
  107. }
  108. RegKey::~RegKey() {
  109. Close();
  110. }
  111. LONG RegKey::Create(HKEY rootkey, const wchar_t* subkey, REGSAM access) {
  112. DWORD disposition_value;
  113. return CreateWithDisposition(rootkey, subkey, &disposition_value, access);
  114. }
  115. LONG RegKey::CreateWithDisposition(HKEY rootkey,
  116. const wchar_t* subkey,
  117. DWORD* disposition,
  118. REGSAM access) {
  119. DCHECK(rootkey && subkey && access && disposition);
  120. HKEY subhkey = nullptr;
  121. LONG result =
  122. RegCreateKeyEx(rootkey, subkey, 0, nullptr, REG_OPTION_NON_VOLATILE,
  123. access, nullptr, &subhkey, disposition);
  124. if (result == ERROR_SUCCESS) {
  125. Close();
  126. key_ = subhkey;
  127. wow64access_ = access & kWow64AccessMask;
  128. }
  129. return result;
  130. }
  131. LONG RegKey::CreateKey(const wchar_t* name, REGSAM access) {
  132. DCHECK(name && access);
  133. // After the application has accessed an alternate registry view using one of
  134. // the [KEY_WOW64_32KEY / KEY_WOW64_64KEY] flags, all subsequent operations
  135. // (create, delete, or open) on child registry keys must explicitly use the
  136. // same flag. Otherwise, there can be unexpected behavior.
  137. // http://msdn.microsoft.com/en-us/library/windows/desktop/aa384129.aspx.
  138. if ((access & kWow64AccessMask) != wow64access_) {
  139. NOTREACHED();
  140. return ERROR_INVALID_PARAMETER;
  141. }
  142. HKEY subkey = nullptr;
  143. LONG result = RegCreateKeyEx(key_, name, 0, nullptr, REG_OPTION_NON_VOLATILE,
  144. access, nullptr, &subkey, nullptr);
  145. if (result == ERROR_SUCCESS) {
  146. Close();
  147. key_ = subkey;
  148. wow64access_ = access & kWow64AccessMask;
  149. }
  150. return result;
  151. }
  152. LONG RegKey::Open(HKEY rootkey, const wchar_t* subkey, REGSAM access) {
  153. DCHECK(rootkey && subkey && access);
  154. HKEY subhkey = nullptr;
  155. LONG result = RegOpenKeyEx(rootkey, subkey, 0, access, &subhkey);
  156. if (result == ERROR_SUCCESS) {
  157. Close();
  158. key_ = subhkey;
  159. wow64access_ = access & kWow64AccessMask;
  160. }
  161. return result;
  162. }
  163. LONG RegKey::OpenKey(const wchar_t* relative_key_name, REGSAM access) {
  164. DCHECK(relative_key_name && access);
  165. // After the application has accessed an alternate registry view using one of
  166. // the [KEY_WOW64_32KEY / KEY_WOW64_64KEY] flags, all subsequent operations
  167. // (create, delete, or open) on child registry keys must explicitly use the
  168. // same flag. Otherwise, there can be unexpected behavior.
  169. // http://msdn.microsoft.com/en-us/library/windows/desktop/aa384129.aspx.
  170. if ((access & kWow64AccessMask) != wow64access_) {
  171. NOTREACHED();
  172. return ERROR_INVALID_PARAMETER;
  173. }
  174. HKEY subkey = nullptr;
  175. LONG result = RegOpenKeyEx(key_, relative_key_name, 0, access, &subkey);
  176. // We have to close the current opened key before replacing it with the new
  177. // one.
  178. if (result == ERROR_SUCCESS) {
  179. Close();
  180. key_ = subkey;
  181. wow64access_ = access & kWow64AccessMask;
  182. }
  183. return result;
  184. }
  185. void RegKey::Close() {
  186. if (key_) {
  187. ::RegCloseKey(key_);
  188. key_ = nullptr;
  189. wow64access_ = 0;
  190. }
  191. }
  192. // TODO(wfh): Remove this and other unsafe methods. See http://crbug.com/375400
  193. void RegKey::Set(HKEY key) {
  194. if (key_ != key) {
  195. Close();
  196. key_ = key;
  197. }
  198. }
  199. HKEY RegKey::Take() {
  200. DCHECK_EQ(wow64access_, 0u);
  201. HKEY key = key_;
  202. key_ = nullptr;
  203. return key;
  204. }
  205. bool RegKey::HasValue(const wchar_t* name) const {
  206. return RegQueryValueEx(key_, name, nullptr, nullptr, nullptr, nullptr) ==
  207. ERROR_SUCCESS;
  208. }
  209. DWORD RegKey::GetValueCount() const {
  210. DWORD count = 0;
  211. LONG result =
  212. RegQueryInfoKey(key_, nullptr, nullptr, nullptr, nullptr, nullptr,
  213. nullptr, &count, nullptr, nullptr, nullptr, nullptr);
  214. return (result == ERROR_SUCCESS) ? count : 0;
  215. }
  216. FILETIME RegKey::GetLastWriteTime() const {
  217. FILETIME last_write_time;
  218. LONG result = RegQueryInfoKey(key_, nullptr, nullptr, nullptr, nullptr,
  219. nullptr, nullptr, nullptr, nullptr, nullptr,
  220. nullptr, &last_write_time);
  221. return (result == ERROR_SUCCESS) ? last_write_time : FILETIME{};
  222. }
  223. LONG RegKey::GetValueNameAt(DWORD index, std::wstring* name) const {
  224. wchar_t buf[256];
  225. DWORD bufsize = std::size(buf);
  226. LONG r = ::RegEnumValue(key_, index, buf, &bufsize, nullptr, nullptr, nullptr,
  227. nullptr);
  228. if (r == ERROR_SUCCESS)
  229. name->assign(buf, bufsize);
  230. return r;
  231. }
  232. LONG RegKey::DeleteKey(const wchar_t* name) {
  233. DCHECK(key_);
  234. DCHECK(name);
  235. HKEY subkey = nullptr;
  236. // Verify the key exists before attempting delete to replicate previous
  237. // behavior.
  238. LONG result =
  239. RegOpenKeyEx(key_, name, 0, READ_CONTROL | wow64access_, &subkey);
  240. if (result != ERROR_SUCCESS)
  241. return result;
  242. RegCloseKey(subkey);
  243. return RegDelRecurse(key_, name, wow64access_);
  244. }
  245. LONG RegKey::DeleteEmptyKey(const wchar_t* name) {
  246. DCHECK(key_);
  247. DCHECK(name);
  248. HKEY target_key = nullptr;
  249. LONG result =
  250. RegOpenKeyEx(key_, name, 0, KEY_READ | wow64access_, &target_key);
  251. if (result != ERROR_SUCCESS)
  252. return result;
  253. DWORD count = 0;
  254. result =
  255. RegQueryInfoKey(target_key, nullptr, nullptr, nullptr, nullptr, nullptr,
  256. nullptr, &count, nullptr, nullptr, nullptr, nullptr);
  257. RegCloseKey(target_key);
  258. if (result != ERROR_SUCCESS)
  259. return result;
  260. if (count == 0)
  261. return RegDeleteKeyEx(key_, name, wow64access_, 0);
  262. return ERROR_DIR_NOT_EMPTY;
  263. }
  264. LONG RegKey::DeleteValue(const wchar_t* value_name) {
  265. DCHECK(key_);
  266. LONG result = RegDeleteValue(key_, value_name);
  267. return result;
  268. }
  269. LONG RegKey::ReadValueDW(const wchar_t* name, DWORD* out_value) const {
  270. DCHECK(out_value);
  271. DWORD type = REG_DWORD;
  272. DWORD size = sizeof(DWORD);
  273. DWORD local_value = 0;
  274. LONG result = ReadValue(name, &local_value, &size, &type);
  275. if (result == ERROR_SUCCESS) {
  276. if ((type == REG_DWORD || type == REG_BINARY) && size == sizeof(DWORD))
  277. *out_value = local_value;
  278. else
  279. result = ERROR_CANTREAD;
  280. }
  281. return result;
  282. }
  283. LONG RegKey::ReadInt64(const wchar_t* name, int64_t* out_value) const {
  284. DCHECK(out_value);
  285. DWORD type = REG_QWORD;
  286. int64_t local_value = 0;
  287. DWORD size = sizeof(local_value);
  288. LONG result = ReadValue(name, &local_value, &size, &type);
  289. if (result == ERROR_SUCCESS) {
  290. if ((type == REG_QWORD || type == REG_BINARY) &&
  291. size == sizeof(local_value))
  292. *out_value = local_value;
  293. else
  294. result = ERROR_CANTREAD;
  295. }
  296. return result;
  297. }
  298. LONG RegKey::ReadValue(const wchar_t* name, std::wstring* out_value) const {
  299. DCHECK(out_value);
  300. const size_t kMaxStringLength = 1024; // This is after expansion.
  301. // Use the one of the other forms of ReadValue if 1024 is too small for you.
  302. wchar_t raw_value[kMaxStringLength];
  303. DWORD type = REG_SZ, size = sizeof(raw_value);
  304. LONG result = ReadValue(name, raw_value, &size, &type);
  305. if (result == ERROR_SUCCESS) {
  306. if (type == REG_SZ) {
  307. *out_value = raw_value;
  308. } else if (type == REG_EXPAND_SZ) {
  309. wchar_t expanded[kMaxStringLength];
  310. size = ExpandEnvironmentStrings(raw_value, expanded, kMaxStringLength);
  311. // Success: returns the number of wchar_t's copied
  312. // Fail: buffer too small, returns the size required
  313. // Fail: other, returns 0
  314. if (size == 0 || size > kMaxStringLength) {
  315. result = ERROR_MORE_DATA;
  316. } else {
  317. *out_value = expanded;
  318. }
  319. } else {
  320. // Not a string. Oops.
  321. result = ERROR_CANTREAD;
  322. }
  323. }
  324. return result;
  325. }
  326. LONG RegKey::ReadValue(const wchar_t* name,
  327. void* data,
  328. DWORD* dsize,
  329. DWORD* dtype) const {
  330. LONG result = RegQueryValueEx(key_, name, nullptr, dtype,
  331. reinterpret_cast<LPBYTE>(data), dsize);
  332. return result;
  333. }
  334. LONG RegKey::ReadValues(const wchar_t* name,
  335. std::vector<std::wstring>* values) {
  336. values->clear();
  337. DWORD type = REG_MULTI_SZ;
  338. DWORD size = 0;
  339. LONG result = ReadValue(name, nullptr, &size, &type);
  340. if (result != ERROR_SUCCESS || size == 0)
  341. return result;
  342. if (type != REG_MULTI_SZ)
  343. return ERROR_CANTREAD;
  344. std::vector<wchar_t> buffer(size / sizeof(wchar_t));
  345. result = ReadValue(name, buffer.data(), &size, nullptr);
  346. if (result != ERROR_SUCCESS || size == 0)
  347. return result;
  348. // Parse the double-null-terminated list of strings.
  349. // Note: This code is paranoid to not read outside of |buf|, in the case where
  350. // it may not be properly terminated.
  351. auto entry = buffer.cbegin();
  352. auto buffer_end = buffer.cend();
  353. while (entry < buffer_end && *entry != '\0') {
  354. auto entry_end = std::find(entry, buffer_end, '\0');
  355. values->emplace_back(entry, entry_end);
  356. entry = entry_end + 1;
  357. }
  358. return 0;
  359. }
  360. LONG RegKey::WriteValue(const wchar_t* name, DWORD in_value) {
  361. return WriteValue(name, &in_value, static_cast<DWORD>(sizeof(in_value)),
  362. REG_DWORD);
  363. }
  364. LONG RegKey::WriteValue(const wchar_t* name, const wchar_t* in_value) {
  365. return WriteValue(
  366. name, in_value,
  367. static_cast<DWORD>(sizeof(*in_value) *
  368. (std::char_traits<wchar_t>::length(in_value) + 1)),
  369. REG_SZ);
  370. }
  371. LONG RegKey::WriteValue(const wchar_t* name,
  372. const void* data,
  373. DWORD dsize,
  374. DWORD dtype) {
  375. DCHECK(data || !dsize);
  376. LONG result =
  377. RegSetValueEx(key_, name, 0, dtype,
  378. reinterpret_cast<LPBYTE>(const_cast<void*>(data)), dsize);
  379. return result;
  380. }
  381. bool RegKey::StartWatching(ChangeCallback callback) {
  382. if (!key_watcher_)
  383. key_watcher_ = std::make_unique<Watcher>();
  384. if (!key_watcher_->StartWatching(key_, std::move(callback)))
  385. return false;
  386. return true;
  387. }
  388. // static
  389. LONG RegKey::RegDelRecurse(HKEY root_key, const wchar_t* name, REGSAM access) {
  390. // First, see if the key can be deleted without having to recurse.
  391. LONG result = RegDeleteKeyEx(root_key, name, access, 0);
  392. if (result == ERROR_SUCCESS)
  393. return result;
  394. HKEY target_key = nullptr;
  395. result = RegOpenKeyEx(root_key, name, 0, KEY_ENUMERATE_SUB_KEYS | access,
  396. &target_key);
  397. if (result == ERROR_FILE_NOT_FOUND)
  398. return ERROR_SUCCESS;
  399. if (result != ERROR_SUCCESS)
  400. return result;
  401. std::wstring subkey_name(name);
  402. // Check for an ending slash and add one if it is missing.
  403. if (!subkey_name.empty() && subkey_name.back() != '\\')
  404. subkey_name.push_back('\\');
  405. // Enumerate the keys
  406. result = ERROR_SUCCESS;
  407. const DWORD kMaxKeyNameLength = MAX_PATH;
  408. const size_t base_key_length = subkey_name.length();
  409. std::wstring key_name;
  410. while (result == ERROR_SUCCESS) {
  411. DWORD key_size = kMaxKeyNameLength;
  412. result =
  413. RegEnumKeyEx(target_key, 0, WriteInto(&key_name, kMaxKeyNameLength),
  414. &key_size, nullptr, nullptr, nullptr, nullptr);
  415. if (result != ERROR_SUCCESS)
  416. break;
  417. key_name.resize(key_size);
  418. subkey_name.resize(base_key_length);
  419. subkey_name += key_name;
  420. if (RegDelRecurse(root_key, subkey_name.c_str(), access) != ERROR_SUCCESS)
  421. break;
  422. }
  423. RegCloseKey(target_key);
  424. // Try again to delete the key.
  425. result = RegDeleteKeyEx(root_key, name, access, 0);
  426. return result;
  427. }
  428. // RegistryValueIterator ------------------------------------------------------
  429. RegistryValueIterator::RegistryValueIterator(HKEY root_key,
  430. const wchar_t* folder_key,
  431. REGSAM wow64access)
  432. : name_(MAX_PATH, '\0'), value_(MAX_PATH, '\0') {
  433. Initialize(root_key, folder_key, wow64access);
  434. }
  435. RegistryValueIterator::RegistryValueIterator(HKEY root_key,
  436. const wchar_t* folder_key)
  437. : name_(MAX_PATH, '\0'), value_(MAX_PATH, '\0') {
  438. Initialize(root_key, folder_key, 0);
  439. }
  440. void RegistryValueIterator::Initialize(HKEY root_key,
  441. const wchar_t* folder_key,
  442. REGSAM wow64access) {
  443. DCHECK_EQ(wow64access & ~kWow64AccessMask, static_cast<REGSAM>(0));
  444. LONG result =
  445. RegOpenKeyEx(root_key, folder_key, 0, KEY_READ | wow64access, &key_);
  446. if (result != ERROR_SUCCESS) {
  447. key_ = nullptr;
  448. } else {
  449. DWORD count = 0;
  450. result =
  451. ::RegQueryInfoKey(key_, nullptr, nullptr, nullptr, nullptr, nullptr,
  452. nullptr, &count, nullptr, nullptr, nullptr, nullptr);
  453. if (result != ERROR_SUCCESS) {
  454. ::RegCloseKey(key_);
  455. key_ = nullptr;
  456. } else {
  457. index_ = count - 1;
  458. }
  459. }
  460. Read();
  461. }
  462. RegistryValueIterator::~RegistryValueIterator() {
  463. if (key_)
  464. ::RegCloseKey(key_);
  465. }
  466. DWORD RegistryValueIterator::ValueCount() const {
  467. DWORD count = 0;
  468. LONG result =
  469. ::RegQueryInfoKey(key_, nullptr, nullptr, nullptr, nullptr, nullptr,
  470. nullptr, &count, nullptr, nullptr, nullptr, nullptr);
  471. if (result != ERROR_SUCCESS)
  472. return 0;
  473. return count;
  474. }
  475. bool RegistryValueIterator::Valid() const {
  476. return key_ != nullptr && index_ != kInvalidIterValue;
  477. }
  478. void RegistryValueIterator::operator++() {
  479. if (index_ != kInvalidIterValue)
  480. --index_;
  481. Read();
  482. }
  483. bool RegistryValueIterator::Read() {
  484. if (Valid()) {
  485. DWORD capacity = static_cast<DWORD>(name_.capacity());
  486. DWORD name_size = capacity;
  487. // |value_size_| is in bytes. Reserve the last character for a NUL.
  488. value_size_ = static_cast<DWORD>((value_.size() - 1) * sizeof(wchar_t));
  489. LONG result = ::RegEnumValue(
  490. key_, index_, WriteInto(&name_, name_size), &name_size, nullptr, &type_,
  491. reinterpret_cast<BYTE*>(value_.data()), &value_size_);
  492. if (result == ERROR_MORE_DATA) {
  493. // Registry key names are limited to 255 characters and fit within
  494. // MAX_PATH (which is 260) but registry value names can use up to 16,383
  495. // characters and the value itself is not limited
  496. // (from http://msdn.microsoft.com/en-us/library/windows/desktop/
  497. // ms724872(v=vs.85).aspx).
  498. // Resize the buffers and retry if their size caused the failure.
  499. DWORD value_size_in_wchars = to_wchar_size(value_size_);
  500. if (value_size_in_wchars + 1 > value_.size())
  501. value_.resize(value_size_in_wchars + 1, '\0');
  502. value_size_ = static_cast<DWORD>((value_.size() - 1) * sizeof(wchar_t));
  503. name_size = name_size == capacity ? MAX_REGISTRY_NAME_SIZE : capacity;
  504. result = ::RegEnumValue(
  505. key_, index_, WriteInto(&name_, name_size), &name_size, nullptr,
  506. &type_, reinterpret_cast<BYTE*>(value_.data()), &value_size_);
  507. }
  508. if (result == ERROR_SUCCESS) {
  509. DCHECK_LT(to_wchar_size(value_size_), value_.size());
  510. value_[to_wchar_size(value_size_)] = '\0';
  511. return true;
  512. }
  513. }
  514. name_[0] = '\0';
  515. value_[0] = '\0';
  516. value_size_ = 0;
  517. return false;
  518. }
  519. // RegistryKeyIterator --------------------------------------------------------
  520. RegistryKeyIterator::RegistryKeyIterator(HKEY root_key,
  521. const wchar_t* folder_key) {
  522. Initialize(root_key, folder_key, 0);
  523. }
  524. RegistryKeyIterator::RegistryKeyIterator(HKEY root_key,
  525. const wchar_t* folder_key,
  526. REGSAM wow64access) {
  527. Initialize(root_key, folder_key, wow64access);
  528. }
  529. RegistryKeyIterator::~RegistryKeyIterator() {
  530. if (key_)
  531. ::RegCloseKey(key_);
  532. }
  533. DWORD RegistryKeyIterator::SubkeyCount() const {
  534. DWORD count = 0;
  535. LONG result =
  536. ::RegQueryInfoKey(key_, nullptr, nullptr, nullptr, &count, nullptr,
  537. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
  538. if (result != ERROR_SUCCESS)
  539. return 0;
  540. return count;
  541. }
  542. bool RegistryKeyIterator::Valid() const {
  543. return key_ != nullptr && index_ != kInvalidIterValue;
  544. }
  545. void RegistryKeyIterator::operator++() {
  546. if (index_ != kInvalidIterValue)
  547. --index_;
  548. Read();
  549. }
  550. bool RegistryKeyIterator::Read() {
  551. if (Valid()) {
  552. DWORD ncount = static_cast<DWORD>(std::size(name_));
  553. FILETIME written;
  554. LONG r = ::RegEnumKeyEx(key_, index_, name_, &ncount, nullptr, nullptr,
  555. nullptr, &written);
  556. if (ERROR_SUCCESS == r)
  557. return true;
  558. }
  559. name_[0] = '\0';
  560. return false;
  561. }
  562. void RegistryKeyIterator::Initialize(HKEY root_key,
  563. const wchar_t* folder_key,
  564. REGSAM wow64access) {
  565. DCHECK_EQ(wow64access & ~kWow64AccessMask, static_cast<REGSAM>(0));
  566. LONG result =
  567. RegOpenKeyEx(root_key, folder_key, 0, KEY_READ | wow64access, &key_);
  568. if (result != ERROR_SUCCESS) {
  569. key_ = nullptr;
  570. } else {
  571. DWORD count = 0;
  572. result =
  573. ::RegQueryInfoKey(key_, nullptr, nullptr, nullptr, &count, nullptr,
  574. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
  575. if (result != ERROR_SUCCESS) {
  576. ::RegCloseKey(key_);
  577. key_ = nullptr;
  578. } else {
  579. index_ = count - 1;
  580. }
  581. }
  582. Read();
  583. }
  584. } // namespace win
  585. } // namespace base