diff options
| author | Robert Schumacher <roschuma@microsoft.com> | 2017-03-10 15:43:54 -0800 |
|---|---|---|
| committer | Robert Schumacher <roschuma@microsoft.com> | 2017-03-10 16:12:04 -0800 |
| commit | 4d0abd5841b9639c20126a5a25e2b0d4a9eb98d2 (patch) | |
| tree | 9eb9f240cbdaf5e80150c1938f66b8fdd74549b1 /toolsrc/src/metrics.cpp | |
| parent | bfa5812a6bd9dd108317491111bbdb68d4d2c9a8 (diff) | |
| download | vcpkg-4d0abd5841b9639c20126a5a25e2b0d4a9eb98d2.tar.gz vcpkg-4d0abd5841b9639c20126a5a25e2b0d4a9eb98d2.zip | |
[vcpkg] Refactor RAII registry key manipulation
Diffstat (limited to 'toolsrc/src/metrics.cpp')
| -rw-r--r-- | toolsrc/src/metrics.cpp | 34 |
1 files changed, 4 insertions, 30 deletions
diff --git a/toolsrc/src/metrics.cpp b/toolsrc/src/metrics.cpp index 4ac43c5c6..b8e469f6e 100644 --- a/toolsrc/src/metrics.cpp +++ b/toolsrc/src/metrics.cpp @@ -227,38 +227,12 @@ true std::wstring GetSQMUser() { - LONG err; + auto hkcu_sqmclient = System::get_registry_string(HKEY_CURRENT_USER, LR"(Software\Microsoft\SQMClient)", L"UserId"); - struct RAII_HKEY - { - HKEY hkey = nullptr; - - ~RAII_HKEY() - { - if (hkey != nullptr) - RegCloseKey(hkey); - } - } HKCU_SQMClient; - - err = RegOpenKeyExW(HKEY_CURRENT_USER, LR"(Software\Microsoft\SQMClient)", NULL, KEY_READ, &HKCU_SQMClient.hkey); - if (err != ERROR_SUCCESS) - { + if (hkcu_sqmclient) + return std::move(*hkcu_sqmclient); + else return L"{}"; - } - - std::array<wchar_t, 128> buffer; - DWORD lType = 0; - DWORD dwBufferSize = static_cast<DWORD>(buffer.size() * sizeof(wchar_t)); - err = RegQueryValueExW(HKCU_SQMClient.hkey, L"UserId", nullptr, &lType, reinterpret_cast<LPBYTE>(buffer.data()), &dwBufferSize); - if (err == ERROR_SUCCESS && lType == REG_SZ && dwBufferSize >= sizeof(wchar_t)) - { - size_t sz = dwBufferSize / sizeof(wchar_t); - if (buffer[sz - 1] == '\0') - --sz; - return std::wstring(buffer.begin(), buffer.begin() + sz); - } - - return L"{}"; } void SetUserInformation(const std::string& user_id, const std::string& first_use_time) |
