X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/de564874d60233c977c93496da1aa406245c37c2..3e2656801d825761f55ea9b0b766802c6cd32f1d:/src/msw/registry.cpp diff --git a/src/msw/registry.cpp b/src/msw/registry.cpp index c18f1de8d4..175242651c 100644 --- a/src/msw/registry.cpp +++ b/src/msw/registry.cpp @@ -12,7 +12,7 @@ // - add high level functions (RegisterOleServer, ...) /////////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ +#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) #pragma implementation "registry.h" #endif @@ -23,13 +23,11 @@ #pragma hdrstop #endif -// other wxWindows headers +// other wxWidgets headers #include "wx/string.h" #include "wx/intl.h" #include "wx/log.h" -#ifndef __WIN16__ - // Windows headers /* #define STRICT @@ -57,6 +55,7 @@ // some registry functions don't like signed chars typedef unsigned char *RegString; +typedef BYTE* RegBinary; // ---------------------------------------------------------------------------- // constants @@ -321,8 +320,6 @@ bool wxRegKey::GetKeyInfo(size_t *pnSubKeys, size_t *pnValues, size_t *pnMaxValueLen) const { -#if defined(__WIN32__) - // old gcc headers incorrectly prototype RegQueryInfoKey() #if defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__) #define REG_PARAM (size_t *) @@ -362,11 +359,6 @@ bool wxRegKey::GetKeyInfo(size_t *pnSubKeys, } return TRUE; -#else // Win16 - wxFAIL_MSG("GetKeyInfo() not implemented"); - - return FALSE; -#endif } // ---------------------------------------------------------------------------- @@ -374,7 +366,7 @@ bool wxRegKey::GetKeyInfo(size_t *pnSubKeys, // ---------------------------------------------------------------------------- // opens key (it's not an error to call Open() on an already opened key) -bool wxRegKey::Open() +bool wxRegKey::Open(AccessMode mode) { if ( IsOpened() ) return TRUE; @@ -385,7 +377,7 @@ bool wxRegKey::Open() (HKEY) m_hRootKey, m_strKey, RESERVED, - KEY_ALL_ACCESS, + mode == Read ? KEY_READ : KEY_ALL_ACCESS, &tmpKey ); @@ -500,14 +492,19 @@ bool wxRegKey::CopyValue(const wxChar *szValue, keyDst.SetValue(szValueNew, dwVal); } +#ifdef __WIN32__ + case Type_Binary: + { + wxMemoryBuffer buf; + return QueryValue(szValue,buf) && + keyDst.SetValue(szValueNew,buf); + } // these types are unsupported because I am not sure about how // exactly they should be copied and because they shouldn't // occur among the application keys (supposedly created with // this class) -#ifdef __WIN32__ case Type_None: case Type_Expand_String: - case Type_Binary: case Type_Dword_big_endian: case Type_Link: case Type_Multi_String: @@ -610,6 +607,10 @@ bool wxRegKey::Copy(wxRegKey& keyDst) if ( ok ) bCont = GetNextKey(strKey, lIndex); + else + wxLogError(_("Failed to copy the registry subkey '%s' to '%s'."), + GetFullName(&key), keyName.c_str()); + } // copy all values @@ -629,7 +630,8 @@ bool wxRegKey::Copy(wxRegKey& keyDst) } if ( !ok ) { - wxLogError(_("Failed to copy the contents of registry key '%s' to '%s'."), GetFullName(this), GetFullName(&keyDst)); + wxLogError(_("Failed to copy the contents of registry key '%s' to '%s'."), + GetFullName(this), GetFullName(&keyDst)); } return ok; @@ -654,7 +656,8 @@ bool wxRegKey::DeleteSelf() if ( m_strKey.IsEmpty() || ((m_hRootKey != (WXHKEY) aStdKeys[HKCR].hkey) && (m_strKey.Find(REG_SEPARATOR) == wxNOT_FOUND)) ) { - wxLogError(_("Registry key '%s' is needed for normal system operation,\ndeleting it will leave your system in unusable state:\noperation aborted."), GetFullName(this)); + wxLogError(_("Registry key '%s' is needed for normal system operation,\ndeleting it will leave your system in unusable state:\noperation aborted."), + GetFullName(this)); return FALSE; } @@ -683,7 +686,9 @@ bool wxRegKey::DeleteSelf() Close(); m_dwLastError = RegDeleteKey((HKEY) m_hRootKey, m_strKey); - if ( m_dwLastError != ERROR_SUCCESS ) { + // deleting a key which doesn't exist is not considered an error + if ( m_dwLastError != ERROR_SUCCESS && + m_dwLastError != ERROR_FILE_NOT_FOUND ) { wxLogSysError(m_dwLastError, _("Can't delete key '%s'"), GetName().c_str()); return FALSE; @@ -706,7 +711,6 @@ bool wxRegKey::DeleteValue(const wxChar *szValue) if ( !Open() ) return FALSE; -#if defined(__WIN32__) m_dwLastError = RegDeleteValue((HKEY) m_hKey, WXSTRINGCAST szValue); // deleting a value which doesn't exist is not considered an error @@ -716,18 +720,6 @@ bool wxRegKey::DeleteValue(const wxChar *szValue) szValue, GetName().c_str()); return FALSE; } -#else //WIN16 - // named registry values don't exist in Win16 world - wxASSERT( IsEmpty(szValue) ); - - // just set the (default and unique) value of the key to "" - m_dwLastError = RegSetValue((HKEY) m_hKey, NULL, REG_SZ, "", RESERVED); - if ( m_dwLastError != ERROR_SUCCESS ) { - wxLogSysError(m_dwLastError, _("Can't delete value of key '%s'"), - GetName().c_str()); - return FALSE; - } -#endif //WIN16/32 return TRUE; } @@ -742,8 +734,7 @@ bool wxRegKey::HasValue(const wxChar *szValue) const // this function should be silent, so suppress possible messages from Open() wxLogNull nolog; - #ifdef __WIN32__ - if ( !CONST_CAST Open() ) + if ( !CONST_CAST Open(Read) ) return FALSE; LONG dwRet = ::RegQueryValueEx((HKEY) m_hKey, @@ -751,10 +742,6 @@ bool wxRegKey::HasValue(const wxChar *szValue) const RESERVED, NULL, NULL, NULL); return dwRet == ERROR_SUCCESS; - #else // WIN16 - // only unnamed value exists - return IsEmpty(szValue); - #endif // WIN16/32 } // returns TRUE if this key has any values @@ -787,7 +774,7 @@ bool wxRegKey::HasSubKey(const wxChar *szKey) const // this function should be silent, so suppress possible messages from Open() wxLogNull nolog; - if ( !CONST_CAST Open() ) + if ( !CONST_CAST Open(Read) ) return FALSE; return KeyExists(m_hKey, szKey); @@ -795,8 +782,7 @@ bool wxRegKey::HasSubKey(const wxChar *szKey) const wxRegKey::ValueType wxRegKey::GetValueType(const wxChar *szValue) const { - #ifdef __WIN32__ - if ( ! CONST_CAST Open() ) + if ( ! CONST_CAST Open(Read) ) return Type_None; DWORD dwType; @@ -809,9 +795,6 @@ wxRegKey::ValueType wxRegKey::GetValueType(const wxChar *szValue) const } return (ValueType)dwType; - #else //WIN16 - return IsEmpty(szValue) ? Type_String : Type_None; - #endif //WIN16/32 } #ifdef __WIN32__ @@ -831,7 +814,7 @@ bool wxRegKey::SetValue(const wxChar *szValue, long lValue) bool wxRegKey::QueryValue(const wxChar *szValue, long *plValue) const { - if ( CONST_CAST Open() ) { + if ( CONST_CAST Open(Read) ) { DWORD dwType, dwSize = sizeof(DWORD); RegString pBuf = (RegString)plValue; m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED, @@ -853,14 +836,69 @@ bool wxRegKey::QueryValue(const wxChar *szValue, long *plValue) const return FALSE; } +bool wxRegKey::SetValue(const wxChar *szValue,const wxMemoryBuffer& buffer) +{ +#ifdef __TWIN32__ + wxFAIL_MSG("RegSetValueEx not implemented by TWIN32"); + return FALSE; +#else + if ( CONST_CAST Open() ) { + m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_BINARY, + (RegBinary)buffer.GetData(),buffer.GetDataLen()); + if ( m_dwLastError == ERROR_SUCCESS ) + return TRUE; + } + + wxLogSysError(m_dwLastError, _("Can't set value of '%s'"), + GetFullName(this, szValue)); + return FALSE; +#endif +} + +bool wxRegKey::QueryValue(const wxChar *szValue, wxMemoryBuffer& buffer) const +{ + if ( CONST_CAST Open() ) { + // first get the type and size of the data + DWORD dwType, dwSize; + m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED, + &dwType, NULL, &dwSize); + + if ( m_dwLastError == ERROR_SUCCESS ) { + if ( dwSize ) { + const RegBinary pBuf = (RegBinary)buffer.GetWriteBuf(dwSize); + m_dwLastError = RegQueryValueEx((HKEY) m_hKey, + WXSTRINGCAST szValue, + RESERVED, + &dwType, + pBuf, + &dwSize); + buffer.UngetWriteBuf(dwSize); + } else { + buffer.SetDataLen(0); + } + } + + + if ( m_dwLastError != ERROR_SUCCESS ) { + wxLogSysError(m_dwLastError, _("Can't read value of key '%s'"), + GetName().c_str()); + return FALSE; + } + return TRUE; + } + return FALSE; +} + + + #endif //Win32 bool wxRegKey::QueryValue(const wxChar *szValue, wxString& strValue, bool raw) const { - if ( CONST_CAST Open() ) { - #ifdef __WIN32__ + if ( CONST_CAST Open(Read) ) { + // first get the type and size of the data DWORD dwType, dwSize; m_dwLastError = RegQueryValueEx((HKEY) m_hKey, WXSTRINGCAST szValue, RESERVED, @@ -902,6 +940,8 @@ bool wxRegKey::QueryValue(const wxChar *szValue, wxLogLastError(_T("ExpandEnvironmentStrings")); } } +#else + wxUnusedVar(raw); #endif // __WXWINCE__ } @@ -914,15 +954,6 @@ bool wxRegKey::QueryValue(const wxChar *szValue, return TRUE; } } - #else //WIN16 - // named registry values don't exist in Win16 - wxASSERT( IsEmpty(szValue) ); - - m_dwLastError = RegQueryValue((HKEY) m_hKey, 0, strValue.GetWriteBuf(256), &l); - strValue.UngetWriteBuf(); - if ( m_dwLastError == ERROR_SUCCESS ) - return TRUE; - #endif //WIN16/32 } wxLogSysError(m_dwLastError, _("Can't read value of '%s'"), @@ -933,20 +964,11 @@ bool wxRegKey::QueryValue(const wxChar *szValue, bool wxRegKey::SetValue(const wxChar *szValue, const wxString& strValue) { if ( CONST_CAST Open() ) { -#if defined( __WIN32__) m_dwLastError = RegSetValueEx((HKEY) m_hKey, szValue, (DWORD) RESERVED, REG_SZ, (RegString)strValue.c_str(), (strValue.Len() + 1)*sizeof(wxChar)); if ( m_dwLastError == ERROR_SUCCESS ) return TRUE; -#else //WIN16 - // named registry values don't exist in Win16 - wxASSERT( IsEmpty(szValue) ); - - m_dwLastError = RegSetValue((HKEY) m_hKey, NULL, REG_SZ, strValue, NULL); - if ( m_dwLastError == ERROR_SUCCESS ) - return TRUE; -#endif //WIN16/32 } wxLogSysError(m_dwLastError, _("Can't set value of '%s'"), @@ -969,7 +991,7 @@ wxString wxRegKey::QueryDefaultValue() const bool wxRegKey::GetFirstValue(wxString& strValueName, long& lIndex) { - if ( !Open() ) + if ( !Open(Read) ) return FALSE; lIndex = 0; @@ -984,7 +1006,6 @@ bool wxRegKey::GetNextValue(wxString& strValueName, long& lIndex) const if ( lIndex == -1 ) return FALSE; -#if defined( __WIN32__) wxChar szValueName[1024]; // @@ use RegQueryInfoKey... DWORD dwValueLen = WXSIZEOF(szValueName); @@ -1009,20 +1030,13 @@ bool wxRegKey::GetNextValue(wxString& strValueName, long& lIndex) const } strValueName = szValueName; -#else //WIN16 - // only one unnamed value - wxASSERT( lIndex == 0 ); - - lIndex = -1; - strValueName.Empty(); -#endif return TRUE; } bool wxRegKey::GetFirstKey(wxString& strKeyName, long& lIndex) { - if ( !Open() ) + if ( !Open(Read) ) return FALSE; lIndex = 0; @@ -1095,7 +1109,7 @@ bool KeyExists(WXHKEY hRootKey, const wxChar *szKey) (HKEY)hRootKey, szKey, RESERVED, - KEY_ALL_ACCESS, + KEY_READ, // we might not have enough rights for rw access &hkeyDummy ) == ERROR_SUCCESS ) { @@ -1123,6 +1137,3 @@ void RemoveTrailingSeparator(wxString& str) str.Truncate(str.Len() - 1); } -#endif - // __WIN16__ -