michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // All Rights Reserved. michael@0: michael@0: #include "base/registry.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #pragma comment(lib, "shlwapi.lib") // for SHDeleteKey michael@0: michael@0: // local types (see the same declarations in the header file) michael@0: #define tchar TCHAR michael@0: #define CTP const tchar* michael@0: #define tstr std::basic_string michael@0: michael@0: // michael@0: // RegistryValueIterator michael@0: // michael@0: michael@0: RegistryValueIterator::RegistryValueIterator(HKEY root_key, michael@0: LPCTSTR folder_key) { michael@0: LONG result = RegOpenKeyEx(root_key, folder_key, 0, KEY_READ, &key_); michael@0: if (result != ERROR_SUCCESS) { michael@0: key_ = NULL; michael@0: } else { michael@0: DWORD count = 0; michael@0: result = ::RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, NULL, &count, michael@0: NULL, NULL, NULL, NULL); michael@0: michael@0: if (result != ERROR_SUCCESS) { michael@0: ::RegCloseKey(key_); michael@0: key_ = NULL; michael@0: } else { michael@0: index_ = count - 1; michael@0: } michael@0: } michael@0: michael@0: Read(); michael@0: } michael@0: michael@0: RegistryValueIterator::~RegistryValueIterator() { michael@0: if (key_) michael@0: ::RegCloseKey(key_); michael@0: } michael@0: michael@0: bool RegistryValueIterator::Valid() const { michael@0: // true while the iterator is valid michael@0: return key_ != NULL && index_ >= 0; michael@0: } michael@0: michael@0: void RegistryValueIterator::operator ++ () { michael@0: // advance to the next entry in the folder michael@0: --index_; michael@0: Read(); michael@0: } michael@0: michael@0: bool RegistryValueIterator::Read() { michael@0: if (Valid()) { michael@0: DWORD ncount = sizeof(name_)/sizeof(*name_); michael@0: value_size_ = sizeof(value_); michael@0: LRESULT r = ::RegEnumValue(key_, index_, name_, &ncount, NULL, &type_, michael@0: reinterpret_cast(value_), &value_size_); michael@0: if (ERROR_SUCCESS == r) michael@0: return true; michael@0: } michael@0: michael@0: name_[0] = '\0'; michael@0: value_[0] = '\0'; michael@0: value_size_ = 0; michael@0: return false; michael@0: } michael@0: michael@0: DWORD RegistryValueIterator::ValueCount() const { michael@0: michael@0: DWORD count = 0; michael@0: HRESULT result = ::RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, NULL, michael@0: &count, NULL, NULL, NULL, NULL); michael@0: michael@0: if (result != ERROR_SUCCESS) michael@0: return 0; michael@0: michael@0: return count; michael@0: } michael@0: michael@0: // michael@0: // RegistryKeyIterator michael@0: // michael@0: michael@0: RegistryKeyIterator::RegistryKeyIterator(HKEY root_key, michael@0: LPCTSTR folder_key) { michael@0: LONG result = RegOpenKeyEx(root_key, folder_key, 0, KEY_READ, &key_); michael@0: if (result != ERROR_SUCCESS) { michael@0: key_ = NULL; michael@0: } else { michael@0: DWORD count = 0; michael@0: HRESULT result = ::RegQueryInfoKey(key_, NULL, 0, NULL, &count, NULL, NULL, michael@0: NULL, NULL, NULL, NULL, NULL); michael@0: michael@0: if (result != ERROR_SUCCESS) { michael@0: ::RegCloseKey(key_); michael@0: key_ = NULL; michael@0: } else { michael@0: index_ = count - 1; michael@0: } michael@0: } michael@0: michael@0: Read(); michael@0: } michael@0: michael@0: RegistryKeyIterator::~RegistryKeyIterator() { michael@0: if (key_) michael@0: ::RegCloseKey(key_); michael@0: } michael@0: michael@0: bool RegistryKeyIterator::Valid() const { michael@0: // true while the iterator is valid michael@0: return key_ != NULL && index_ >= 0; michael@0: } michael@0: michael@0: void RegistryKeyIterator::operator ++ () { michael@0: // advance to the next entry in the folder michael@0: --index_; michael@0: Read(); michael@0: } michael@0: michael@0: bool RegistryKeyIterator::Read() { michael@0: if (Valid()) { michael@0: DWORD ncount = sizeof(name_)/sizeof(*name_); michael@0: FILETIME written; michael@0: LRESULT r = ::RegEnumKeyEx(key_, index_, name_, &ncount, NULL, NULL, michael@0: NULL, &written); michael@0: if (ERROR_SUCCESS == r) michael@0: return true; michael@0: } michael@0: michael@0: name_[0] = '\0'; michael@0: return false; michael@0: } michael@0: michael@0: DWORD RegistryKeyIterator::SubkeyCount() const { michael@0: michael@0: DWORD count = 0; michael@0: HRESULT result = ::RegQueryInfoKey(key_, NULL, 0, NULL, &count, NULL, NULL, michael@0: NULL, NULL, NULL, NULL, NULL); michael@0: michael@0: if (result != ERROR_SUCCESS) michael@0: return 0; michael@0: michael@0: return count; michael@0: } michael@0: michael@0: // michael@0: // RegKey michael@0: // michael@0: michael@0: RegKey::RegKey(HKEY rootkey, const tchar* subkey, REGSAM access) michael@0: : key_(NULL), watch_event_(0) { michael@0: if (rootkey) { michael@0: if (access & (KEY_SET_VALUE | KEY_CREATE_SUB_KEY | KEY_CREATE_LINK)) michael@0: this->Create(rootkey, subkey, access); michael@0: else michael@0: this->Open(rootkey, subkey, access); michael@0: } michael@0: else assert(!subkey); michael@0: } michael@0: michael@0: void RegKey::Close() { michael@0: StopWatching(); michael@0: if (key_) { michael@0: ::RegCloseKey(key_); michael@0: key_ = NULL; michael@0: } michael@0: } michael@0: michael@0: bool RegKey::Create(HKEY rootkey, const tchar* subkey, REGSAM access) { michael@0: DWORD disposition_value; michael@0: return CreateWithDisposition(rootkey, subkey, &disposition_value, access); michael@0: } michael@0: michael@0: bool RegKey::CreateWithDisposition(HKEY rootkey, const tchar* subkey, michael@0: DWORD* disposition, REGSAM access) { michael@0: assert(rootkey && subkey && access && disposition); michael@0: this->Close(); michael@0: michael@0: LONG const result = RegCreateKeyEx(rootkey, michael@0: subkey, michael@0: 0, michael@0: NULL, michael@0: REG_OPTION_NON_VOLATILE, michael@0: access, michael@0: NULL, michael@0: &key_, michael@0: disposition ); michael@0: if (result != ERROR_SUCCESS) { michael@0: key_ = NULL; michael@0: return false; michael@0: } michael@0: else return true; michael@0: } michael@0: michael@0: bool RegKey::Open(HKEY rootkey, const tchar* subkey, REGSAM access) { michael@0: assert(rootkey && subkey && access); michael@0: this->Close(); michael@0: michael@0: LONG const result = RegOpenKeyEx(rootkey, subkey, 0, michael@0: access, &key_ ); michael@0: if (result != ERROR_SUCCESS) { michael@0: key_ = NULL; michael@0: return false; michael@0: } michael@0: else return true; michael@0: } michael@0: michael@0: bool RegKey::CreateKey(const tchar* name, REGSAM access) { michael@0: assert(name && access); michael@0: michael@0: HKEY subkey = NULL; michael@0: LONG const result = RegCreateKeyEx(key_, name, 0, NULL, michael@0: REG_OPTION_NON_VOLATILE, michael@0: access, NULL, &subkey, NULL); michael@0: this->Close(); michael@0: michael@0: key_ = subkey; michael@0: return (result == ERROR_SUCCESS); michael@0: } michael@0: michael@0: bool RegKey::OpenKey(const tchar* name, REGSAM access) { michael@0: assert(name && access); michael@0: michael@0: HKEY subkey = NULL; michael@0: LONG const result = RegOpenKeyEx(key_, name, 0, access, &subkey); michael@0: michael@0: this->Close(); michael@0: michael@0: key_ = subkey; michael@0: return (result == ERROR_SUCCESS); michael@0: } michael@0: michael@0: DWORD RegKey::ValueCount() { michael@0: DWORD count = 0; michael@0: HRESULT const result = ::RegQueryInfoKey(key_, NULL, 0, NULL, NULL, NULL, michael@0: NULL, &count, NULL, NULL, NULL, NULL); michael@0: return (result != ERROR_SUCCESS) ? 0 : count; michael@0: } michael@0: michael@0: bool RegKey::ReadName(int index, tstr* name) { michael@0: tchar buf[256]; michael@0: DWORD bufsize = sizeof(buf)/sizeof(*buf); michael@0: LRESULT r = ::RegEnumValue(key_, index, buf, &bufsize, NULL, NULL, michael@0: NULL, NULL); michael@0: if (r != ERROR_SUCCESS) michael@0: return false; michael@0: if (name) michael@0: *name = buf; michael@0: return true; michael@0: } michael@0: michael@0: bool RegKey::ValueExists(const tchar* name) { michael@0: if (!key_) return false; michael@0: const HRESULT result = RegQueryValueEx(key_, name, 0, NULL, NULL, NULL); michael@0: return (result == ERROR_SUCCESS); michael@0: } michael@0: michael@0: bool RegKey::ReadValue(const tchar* name, void* data, michael@0: DWORD* dsize, DWORD* dtype) { michael@0: if (!key_) return false; michael@0: HRESULT const result = RegQueryValueEx(key_, name, 0, dtype, michael@0: reinterpret_cast(data), michael@0: dsize); michael@0: return (result == ERROR_SUCCESS); michael@0: } michael@0: michael@0: bool RegKey::ReadValue(const tchar* name, tstr * value) { michael@0: assert(value); michael@0: static const size_t kMaxStringLength = 1024; // This is after expansion. michael@0: // Use the one of the other forms of ReadValue if 1024 is too small for you. michael@0: TCHAR raw_value[kMaxStringLength]; michael@0: DWORD type = REG_SZ, size = sizeof(raw_value); michael@0: if (this->ReadValue(name, raw_value, &size, &type)) { michael@0: if (type == REG_SZ) { michael@0: *value = raw_value; michael@0: } else if (type == REG_EXPAND_SZ) { michael@0: TCHAR expanded[kMaxStringLength]; michael@0: size = ExpandEnvironmentStrings(raw_value, expanded, kMaxStringLength); michael@0: // Success: returns the number of TCHARs copied michael@0: // Fail: buffer too small, returns the size required michael@0: // Fail: other, returns 0 michael@0: if (size == 0 || size > kMaxStringLength) michael@0: return false; michael@0: *value = expanded; michael@0: } else { michael@0: // Not a string. Oops. michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: else return false; michael@0: } michael@0: michael@0: bool RegKey::ReadValueDW(const tchar* name, DWORD * value) { michael@0: assert(value); michael@0: DWORD type = REG_DWORD, size = sizeof(DWORD), result = 0; michael@0: if (this->ReadValue(name, &result, &size, &type) michael@0: && (type == REG_DWORD || type == REG_BINARY) michael@0: && size == sizeof(DWORD)) { michael@0: *value = result; michael@0: return true; michael@0: } michael@0: else return false; michael@0: } michael@0: michael@0: bool RegKey::WriteValue(const tchar* name, michael@0: const void * data, michael@0: DWORD dsize, michael@0: DWORD dtype) { michael@0: assert(data); michael@0: if (!key_) return false; michael@0: HRESULT const result = RegSetValueEx( michael@0: key_, michael@0: name, michael@0: 0, michael@0: dtype, michael@0: reinterpret_cast(const_cast(data)), michael@0: dsize); michael@0: return (result == ERROR_SUCCESS); michael@0: } michael@0: michael@0: bool RegKey::WriteValue(const tchar * name, const tchar * value) { michael@0: return this->WriteValue(name, value, michael@0: static_cast(sizeof(*value) * (_tcslen(value) + 1)), REG_SZ); michael@0: } michael@0: michael@0: bool RegKey::WriteValue(const tchar * name, DWORD value) { michael@0: return this->WriteValue(name, &value, michael@0: static_cast(sizeof(value)), REG_DWORD); michael@0: } michael@0: michael@0: bool RegKey::DeleteKey(const tchar * name) { michael@0: if (!key_) return false; michael@0: return (ERROR_SUCCESS == SHDeleteKey(key_, name)); michael@0: } michael@0: michael@0: michael@0: bool RegKey::DeleteValue(const tchar * value_name) { michael@0: assert(value_name); michael@0: HRESULT const result = RegDeleteValue(key_, value_name); michael@0: return (result == ERROR_SUCCESS); michael@0: } michael@0: michael@0: bool RegKey::StartWatching() { michael@0: if (!watch_event_) michael@0: watch_event_ = CreateEvent(NULL, TRUE, FALSE, NULL); michael@0: michael@0: DWORD filter = REG_NOTIFY_CHANGE_NAME | michael@0: REG_NOTIFY_CHANGE_ATTRIBUTES | michael@0: REG_NOTIFY_CHANGE_LAST_SET | michael@0: REG_NOTIFY_CHANGE_SECURITY; michael@0: michael@0: // Watch the registry key for a change of value. michael@0: HRESULT result = RegNotifyChangeKeyValue(key_, TRUE, filter, michael@0: watch_event_, TRUE); michael@0: if (SUCCEEDED(result)) { michael@0: return true; michael@0: } else { michael@0: CloseHandle(watch_event_); michael@0: watch_event_ = 0; michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: bool RegKey::StopWatching() { michael@0: if (watch_event_) { michael@0: CloseHandle(watch_event_); michael@0: watch_event_ = 0; michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool RegKey::HasChanged() { michael@0: if (watch_event_) { michael@0: if (WaitForSingleObject(watch_event_, 0) == WAIT_OBJECT_0) { michael@0: StartWatching(); michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: // Register a COM object with the most usual properties. michael@0: bool RegisterCOMServer(const tchar* guid, michael@0: const tchar* name, michael@0: const tchar* path) { michael@0: RegKey key(HKEY_CLASSES_ROOT, _T("CLSID"), KEY_WRITE); michael@0: key.CreateKey(guid, KEY_WRITE); michael@0: key.WriteValue(NULL, name); michael@0: key.CreateKey(_T("InprocServer32"), KEY_WRITE); michael@0: key.WriteValue(NULL, path); michael@0: key.WriteValue(_T("ThreadingModel"), _T("Apartment")); michael@0: return true; michael@0: } michael@0: michael@0: bool RegisterCOMServer(const tchar* guid, const tchar* name, HINSTANCE module) { michael@0: tchar module_path[MAX_PATH]; michael@0: ::GetModuleFileName(module, module_path, MAX_PATH); michael@0: _tcslwr_s(module_path, MAX_PATH); michael@0: return RegisterCOMServer(guid, name, module_path); michael@0: } michael@0: michael@0: bool UnregisterCOMServer(const tchar* guid) { michael@0: RegKey key(HKEY_CLASSES_ROOT, _T("CLSID"), KEY_WRITE); michael@0: key.DeleteKey(guid); michael@0: return true; michael@0: }