Report type mismatch when reading registry values
Instead of asserting, report a user-visible error when trying to use a wrong key, this should give more information to the developers and users if (when) things go wrong. See #16719.
This commit is contained in:
@@ -136,6 +136,33 @@ static wxString GetFullName(const wxRegKey *pKey, const wxString& szValue);
|
|||||||
// to NULL
|
// to NULL
|
||||||
static inline const wxChar *RegValueStr(const wxString& szValue);
|
static inline const wxChar *RegValueStr(const wxString& szValue);
|
||||||
|
|
||||||
|
// Return the user-readable name of the given REG_XXX type constant.
|
||||||
|
static wxString GetTypeString(DWORD dwType)
|
||||||
|
{
|
||||||
|
#define REG_TYPE_TO_STR(type) case REG_ ## type: return wxS(#type)
|
||||||
|
|
||||||
|
switch ( dwType )
|
||||||
|
{
|
||||||
|
REG_TYPE_TO_STR(NONE);
|
||||||
|
REG_TYPE_TO_STR(SZ);
|
||||||
|
REG_TYPE_TO_STR(EXPAND_SZ);
|
||||||
|
REG_TYPE_TO_STR(BINARY);
|
||||||
|
REG_TYPE_TO_STR(DWORD);
|
||||||
|
// REG_TYPE_TO_STR(DWORD_LITTLE_ENDIAN); -- same as REG_DWORD
|
||||||
|
REG_TYPE_TO_STR(DWORD_BIG_ENDIAN);
|
||||||
|
REG_TYPE_TO_STR(LINK);
|
||||||
|
REG_TYPE_TO_STR(MULTI_SZ);
|
||||||
|
REG_TYPE_TO_STR(RESOURCE_LIST);
|
||||||
|
REG_TYPE_TO_STR(FULL_RESOURCE_DESCRIPTOR);
|
||||||
|
REG_TYPE_TO_STR(RESOURCE_REQUIREMENTS_LIST);
|
||||||
|
REG_TYPE_TO_STR(QWORD);
|
||||||
|
// REG_TYPE_TO_STR(QWORD_LITTLE_ENDIAN); -- same as REG_QWORD
|
||||||
|
|
||||||
|
default:
|
||||||
|
return wxString::Format(_("unknown (%lu)"), dwType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
// implementation of wxRegKey class
|
// implementation of wxRegKey class
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
@@ -897,14 +924,16 @@ bool wxRegKey::QueryValue(const wxString& szValue, long *plValue) const
|
|||||||
GetName().c_str());
|
GetName().c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
// check that we read the value of right type
|
// check that we read the value of right type
|
||||||
wxASSERT_MSG( IsNumericValue(szValue),
|
if ( dwType != REG_DWORD_LITTLE_ENDIAN && dwType != REG_DWORD_BIG_ENDIAN ) {
|
||||||
wxT("Type mismatch in wxRegKey::QueryValue().") );
|
wxLogError(_("Registry value \"%s\" is not numeric (but of type %s)"),
|
||||||
|
GetFullName(this, szValue), GetTypeString(dwType));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
else
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -939,6 +968,12 @@ bool wxRegKey::QueryValue(const wxString& szValue, wxMemoryBuffer& buffer) const
|
|||||||
&dwType, NULL, &dwSize);
|
&dwType, NULL, &dwSize);
|
||||||
|
|
||||||
if ( m_dwLastError == ERROR_SUCCESS ) {
|
if ( m_dwLastError == ERROR_SUCCESS ) {
|
||||||
|
if ( dwType != REG_BINARY ) {
|
||||||
|
wxLogError(_("Registry value \"%s\" is not binary (but of type %s)"),
|
||||||
|
GetFullName(this, szValue), GetTypeString(dwType));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if ( dwSize ) {
|
if ( dwSize ) {
|
||||||
const RegBinary pBuf = (RegBinary)buffer.GetWriteBuf(dwSize);
|
const RegBinary pBuf = (RegBinary)buffer.GetWriteBuf(dwSize);
|
||||||
m_dwLastError = RegQueryValueEx((HKEY) m_hKey,
|
m_dwLastError = RegQueryValueEx((HKEY) m_hKey,
|
||||||
@@ -981,6 +1016,13 @@ bool wxRegKey::QueryValue(const wxString& szValue,
|
|||||||
&dwType, NULL, &dwSize);
|
&dwType, NULL, &dwSize);
|
||||||
if ( m_dwLastError == ERROR_SUCCESS )
|
if ( m_dwLastError == ERROR_SUCCESS )
|
||||||
{
|
{
|
||||||
|
if ( dwType != REG_SZ && dwType != REG_EXPAND_SZ )
|
||||||
|
{
|
||||||
|
wxLogError(_("Registry value \"%s\" is not text (but of type %s)"),
|
||||||
|
GetFullName(this, szValue), GetTypeString(dwType));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if ( !dwSize )
|
if ( !dwSize )
|
||||||
{
|
{
|
||||||
// must treat this case specially as GetWriteBuf() doesn't like
|
// must treat this case specially as GetWriteBuf() doesn't like
|
||||||
@@ -1034,15 +1076,9 @@ bool wxRegKey::QueryValue(const wxString& szValue,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ( m_dwLastError == ERROR_SUCCESS )
|
if ( m_dwLastError == ERROR_SUCCESS )
|
||||||
{
|
|
||||||
// check that it was the right type
|
|
||||||
wxASSERT_MSG( !IsNumericValue(szValue),
|
|
||||||
wxT("Type mismatch in wxRegKey::QueryValue().") );
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
wxLogSysError(m_dwLastError, _("Can't read value of '%s'"),
|
wxLogSysError(m_dwLastError, _("Can't read value of '%s'"),
|
||||||
GetFullName(this, szValue));
|
GetFullName(this, szValue));
|
||||||
|
Reference in New Issue
Block a user