| 137 | }; |
| 138 | |
| 139 | KeyHandler KeyHandler::OpenKey(cm::string_view rootKey, cm::string_view subKey, |
| 140 | View view) |
| 141 | { |
| 142 | if (view == View::Reg64 && !Is64BitWindows()) { |
| 143 | throw registry_error("No 64bit registry on Windows32."); |
| 144 | } |
| 145 | |
| 146 | HKEY hRootKey; |
| 147 | if (rootKey == "HKCU"_s || rootKey == "HKEY_CURRENT_USER"_s) { |
| 148 | hRootKey = HKEY_CURRENT_USER; |
| 149 | } else if (rootKey == "HKLM"_s || rootKey == "HKEY_LOCAL_MACHINE"_s) { |
| 150 | hRootKey = HKEY_LOCAL_MACHINE; |
| 151 | } else if (rootKey == "HKCR"_s || rootKey == "HKEY_CLASSES_ROOT"_s) { |
| 152 | hRootKey = HKEY_CLASSES_ROOT; |
| 153 | } else if (rootKey == "HKCC"_s || rootKey == "HKEY_CURRENT_CONFIG"_s) { |
| 154 | hRootKey = HKEY_CURRENT_CONFIG; |
| 155 | } else if (rootKey == "HKU"_s || rootKey == "HKEY_USERS"_s) { |
| 156 | hRootKey = HKEY_USERS; |
| 157 | } else { |
| 158 | throw registry_error(cmStrCat(rootKey, ": invalid root key.")); |
| 159 | } |
| 160 | // Update path format |
| 161 | auto key = ToWide(subKey); |
| 162 | std::replace(key.begin(), key.end(), L'/', L'\\'); |
| 163 | |
| 164 | REGSAM options = KEY_READ; |
| 165 | if (Is64BitWindows()) { |
| 166 | options |= view == View::Reg64 ? KEY_WOW64_64KEY : KEY_WOW64_32KEY; |
| 167 | } |
| 168 | |
| 169 | HKEY hKey; |
| 170 | LSTATUS status; |
| 171 | if ((status = RegOpenKeyExW(hRootKey, key.c_str(), 0, options, &hKey)) != |
| 172 | ERROR_SUCCESS) { |
| 173 | throw registry_error(FormatSystemError(status)); |
| 174 | } |
| 175 | |
| 176 | return KeyHandler(hKey); |
| 177 | } |
| 178 | |
| 179 | KeyHandler KeyHandler::OpenKey(cm::string_view key, View view) |
| 180 | { |
nothing calls this directly
no test coverage detected