Index: src/common/TRegistry.cpp =================================================================== diff -u -N -re6380ed876ab438bb19585713b36bc1d400f5d95 -r7e67996922421c8b79c7c0d20d4d68c7722c4ad8 --- src/common/TRegistry.cpp (.../TRegistry.cpp) (revision e6380ed876ab438bb19585713b36bc1d400f5d95) +++ src/common/TRegistry.cpp (.../TRegistry.cpp) (revision 7e67996922421c8b79c7c0d20d4d68c7722c4ad8) @@ -21,34 +21,39 @@ #include #include -TRegistry::TRegistry(HKEY key, const wchar_t* pszKey, bool bReadOnly) +TRegistry::TRegistry(HKEY key, const wchar_t* pszKey, bool bReadOnly, bool bFailIfNotFound) { - ReOpen(key, pszKey, bReadOnly); + ReOpen(key, pszKey, bReadOnly, bFailIfNotFound); } TRegistry::~TRegistry() { - if(m_hKey) + if(IsOpen()) RegCloseKey(m_hKey); } -void TRegistry::ReOpen(HKEY key, const wchar_t* pszKey, bool bReadOnly /*= true*/) +void TRegistry::ReOpen(HKEY key, const wchar_t* pszKey, bool bReadOnly, bool bFailIfNotFound) { if (!pszKey) throw std::invalid_argument("pszKey"); - if (m_hKey) + if (IsOpen()) RegCloseKey(m_hKey); LSTATUS lStatus = RegOpenKeyEx(key, pszKey, 0, bReadOnly ? KEY_QUERY_VALUE : KEY_ALL_ACCESS, &m_hKey); if (lStatus != ERROR_SUCCESS || m_hKey == nullptr) - throw std::runtime_error("Cannot open registry key"); + { + if(bFailIfNotFound || lStatus != ERROR_FILE_NOT_FOUND) + throw std::runtime_error("Cannot open registry key"); + } } void TRegistry::CreateSubKey(const wchar_t* pszKey) { if (!pszKey) throw std::invalid_argument("pszKey"); + if(!IsOpen()) + throw std::runtime_error("Registry key not open"); HKEY newKey = nullptr; LSTATUS status = RegCreateKeyEx(m_hKey, pszKey, 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY, nullptr, &newKey, nullptr); @@ -60,6 +65,8 @@ { if (!pszKey) throw std::invalid_argument("pszKey"); + if(!IsOpen()) + throw std::runtime_error("Registry key not open"); LSTATUS status = SHDeleteKey(m_hKey, pszKey); if (status != ERROR_SUCCESS && status != ERROR_FILE_NOT_FOUND) @@ -70,6 +77,8 @@ { if (!pszValueKey) throw std::invalid_argument("pszValueKey"); + if(!IsOpen()) + throw std::runtime_error("Registry key not open"); LSTATUS status = RegDeleteValue(m_hKey, pszValueKey); if (status != ERROR_SUCCESS && status != ERROR_FILE_NOT_FOUND) @@ -80,6 +89,8 @@ { if (!pszValueKey) throw std::invalid_argument("pszValueKey"); + if(!IsOpen()) + throw std::runtime_error("Registry key not open"); DWORD dwType = REG_SZ; const DWORD stMaxBuffer = 1024; @@ -100,6 +111,8 @@ { if (!pszValueKey) throw std::invalid_argument("pszValueKey"); + if(!IsOpen()) + throw std::runtime_error("Registry key not open"); DWORD dwType = REG_DWORD; DWORD dwCount = sizeof(DWORD); @@ -119,6 +132,8 @@ throw std::invalid_argument("pszValueKey"); if (!pszValue) throw std::invalid_argument("pszValue"); + if(!IsOpen()) + throw std::runtime_error("Registry key not open"); size_t stValueLen = wcslen(pszValue) + 1; LSTATUS status = RegSetValueEx(m_hKey, pszValueKey, 0, REG_SZ, (const BYTE*)pszValue, boost::numeric_cast(stValueLen * sizeof(wchar_t)));