bool KeyExists(WXHKEY hRootKey, const wxString& szKey, wxRegKey::WOW64ViewMode viewMode) { // don't close this key itself for the case of empty szKey! if ( szKey.empty() ) return true; HKEY hkeyDummy; if ( ::RegOpenKeyEx ( (HKEY)hRootKey, szKey.t_str(), RESERVED, // we might not have enough rights for rw access GetMSWAccessFlags(wxRegKey::Read, viewMode), &hkeyDummy ) == ERROR_SUCCESS ) { ::RegCloseKey(hkeyDummy); return true; } return false; }
// opens key (it's not an error to call Open() on an already opened key) bool wxRegKey::Open(AccessMode mode) { if ( IsOpened() ) { if ( mode <= m_mode ) return true; // we had been opened in read mode but now must be reopened in write Close(); } HKEY tmpKey; m_dwLastError = ::RegOpenKeyEx ( (HKEY) m_hRootKey, m_strKey.t_str(), RESERVED, GetMSWAccessFlags(mode, m_viewMode), &tmpKey ); if ( m_dwLastError != ERROR_SUCCESS ) { wxLogSysError(m_dwLastError, _("Can't open registry key '%s'"), GetName().c_str()); return false; } m_hKey = (WXHKEY) tmpKey; m_mode = mode; return true; }
// creates key, failing if it exists and !bOkIfExists bool wxRegKey::Create(bool bOkIfExists) { // check for existence only if asked (i.e. order is important!) if ( !bOkIfExists && Exists() ) return false; if ( IsOpened() ) return true; HKEY tmpKey; DWORD disposition; // Minimum supported OS for RegCreateKeyEx: Win 95, Win NT 3.1, Win CE 1.0 m_dwLastError = RegCreateKeyEx((HKEY) m_hRootKey, m_strKey.t_str(), 0, // reserved and must be 0 NULL, // The user-defined class type of this key. REG_OPTION_NON_VOLATILE, // supports other values as well; see MS docs GetMSWAccessFlags(wxRegKey::Write, m_viewMode), NULL, // pointer to a SECURITY_ATTRIBUTES structure &tmpKey, &disposition); if ( m_dwLastError != ERROR_SUCCESS ) { wxLogSysError(m_dwLastError, _("Can't create registry key '%s'"), GetName().c_str()); return false; } else { m_hKey = (WXHKEY) tmpKey; return true; } }