From 2e385f8b270956a273dbd6969aa238f1d59d98b0 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Sun, 3 May 2015 21:36:46 +0200 Subject: [PATCH] Added enum_values() function to the Registry name-space. --- include/MUtils/Registry.h | 4 ++- src/Registry_Win32.cpp | 56 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/include/MUtils/Registry.h b/include/MUtils/Registry.h index b6c64e2..5e266db 100644 --- a/include/MUtils/Registry.h +++ b/include/MUtils/Registry.h @@ -70,6 +70,7 @@ namespace MUtils bool value_read(const QString &valueName, quint32 &value) const; bool value_read(const QString &valueName, QString &value) const; + bool enum_values (QStringList &list) const; bool enum_subkeys(QStringList &list) const; private: @@ -82,7 +83,8 @@ namespace MUtils MUTILS_API bool reg_value_read (const int &rootKey, const QString &keyName, const QString &valueName, quint32 &value); MUTILS_API bool reg_value_read (const int &rootKey, const QString &keyName, const QString &valueName, QString &value); MUTILS_API bool reg_key_delete (const int &rootKey, const QString &keyName); - MUTILS_API bool reg_enum_subkeys(const int &rootKey, const QString &keyName, QStringList &subkeys); + MUTILS_API bool reg_enum_values (const int &rootKey, const QString &keyName, QStringList &list); + MUTILS_API bool reg_enum_subkeys(const int &rootKey, const QString &keyName, QStringList &list); } } diff --git a/src/Registry_Win32.cpp b/src/Registry_Win32.cpp index 1e81f4a..6542bdb 100644 --- a/src/Registry_Win32.cpp +++ b/src/Registry_Win32.cpp @@ -143,6 +143,7 @@ bool MUtils::Registry::RegistryKey::value_write(const QString &valueName, const bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, quint32 &value) const { + value = 0; DWORD size = sizeof(quint32), type = -1; CHECK_STATUS(KEY_READ); return (RegQueryValueEx(p->m_hKey, valueName.isEmpty() ? NULL : MUTILS_WCHR(valueName), 0, &type, reinterpret_cast(&value), &size) == ERROR_SUCCESS) && (type == REG_DWORD); @@ -150,8 +151,8 @@ bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, quint32 bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, QString &value) const { - wchar_t buffer[2048]; - DWORD size = sizeof(wchar_t) * 2048, type = -1; + value = QString(); + wchar_t buffer[2048]; DWORD size = sizeof(wchar_t) * 2048, type = -1; CHECK_STATUS(KEY_READ); if((RegQueryValueEx(p->m_hKey, valueName.isEmpty() ? NULL : MUTILS_WCHR(valueName), 0, &type, reinterpret_cast(&(buffer[0])), &size) == ERROR_SUCCESS) && ((type == REG_SZ) || (type == REG_EXPAND_SZ))) { @@ -161,6 +162,25 @@ bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, QString return false; } +bool MUtils::Registry::RegistryKey::enum_values(QStringList &list) const +{ + wchar_t buffer[2048]; + list.clear(); + CHECK_STATUS(KEY_QUERY_VALUE); + for(DWORD i = 0; i < UINT_MAX; i++) + { + DWORD size = 2048; + const DWORD ret = RegEnumValue(p->m_hKey, i, buffer, &size, NULL, NULL, NULL, NULL); + if(ret == ERROR_SUCCESS) + { + list << QString::fromUtf16(reinterpret_cast(buffer)); + continue; + } + return (ret == ERROR_NO_MORE_ITEMS); + } + return false; +} + bool MUtils::Registry::RegistryKey::enum_subkeys(QStringList &list) const { wchar_t buffer[2048]; @@ -223,6 +243,10 @@ bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName { success = regKey.value_read(valueName, value); } + else + { + value = 0; + } return success; } @@ -237,11 +261,33 @@ bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName { success = regKey.value_read(valueName, value); } + else + { + value = QString(); + } return success; } /* - * Read registry value + * Enumerate value names + */ +bool MUtils::Registry::reg_enum_values(const int &rootKey, const QString &keyName, QStringList &values) +{ + bool success = false; + RegistryKey regKey(rootKey, keyName, access_readonly); + if(regKey.isOpen()) + { + success = regKey.enum_values(values); + } + else + { + values.clear(); + } + return success; +} + +/* + * Enumerate subkey names */ bool MUtils::Registry::reg_enum_subkeys(const int &rootKey, const QString &keyName, QStringList &subkeys) { @@ -251,6 +297,10 @@ bool MUtils::Registry::reg_enum_subkeys(const int &rootKey, const QString &keyNa { success = regKey.enum_subkeys(subkeys); } + else + { + subkeys.clear(); + } return success; }