Registry::Registry(StringRange path, bool writable) : _handle(nullptr) { assert("Empty path" && !path.empty()); int rootKeyNameEnd = String::refer(path).indexOf(L'\\'); StringRange subKeyName = L""; if (rootKeyNameEnd == -1) { rootKeyNameEnd = path.length(); } else { subKeyName = StringRange(path.c_str() + rootKeyNameEnd + 1); } if (!rootKeyNameEnd) { assert("Invalid Registry root key name" && false); } else if (!String::compare(path, 0, L"HKEY_CLASSES_ROOT", 0, rootKeyNameEnd)) { *this = Registry::classesRoot(); } else if (!String::compare(path, 0, L"HKEY_CURRENT_CONFIG", 0, rootKeyNameEnd)) { *this = Registry::currentConfig(); } else if (!String::compare(path, 0, L"HKEY_CURRENT_USER", 0, rootKeyNameEnd)) { *this = Registry::currentUser(); } else if (!String::compare(path, 0, L"HKEY_LOCAL_MACHINE", 0, rootKeyNameEnd)) { *this = Registry::localMachine(); } else if (!String::compare(path, 0, L"HKEY_USERS", 0, rootKeyNameEnd)) { *this = Registry::users(); } else { assert("Invalid Registry root key name" && false); } if (!subKeyName.empty()) { *this = openKey(subKeyName, writable); } }
Registry Registry::openKey(StringRange keyName, bool writable) const { assert("Empty Registry" && *this); assert("Empty kKeyName" && !keyName.empty()); Registry key; checkResult(RegOpenKeyExW(_handle, keyName.c_str(), 0 , writable ? KEY_ALL_ACCESS : KEY_READ, &key._handle)); return key; }
Registry Registry::createKey(StringRange keyName, bool writable) { assert("Empty Registry" && *this); assert("Empty keyName" && !keyName.empty()); Registry key; checkResult(RegCreateKeyExW(_handle, keyName.c_str(), 0, nullptr, REG_OPTION_NON_VOLATILE , writable ? KEY_ALL_ACCESS : KEY_READ, nullptr, &key._handle, nullptr)); return key; }
void Registry::removeKey(StringRange keyName, bool recursive) { assert("Empty Registry" && *this); assert("Empty keyName" && !keyName.empty()); if (recursive) { Registry key = openKey(keyName, true); if (key) { auto i = NamesIterator(key, true); while (i) { key.removeKey(*i, true); i.refresh(); } } } checkResult(RegDeleteKeyW(_handle, keyName.c_str())); }