X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=src%2FRegistry_Win32.cpp;h=6542bdb7e9bd1e42178faacaae7c83dc77b96fc5;hb=2e385f8b270956a273dbd6969aa238f1d59d98b0;hp=b1b67d42307baff03bde82b5ae23006a3b4874b3;hpb=636e61c4842671ae20a320cf2f53d9eddd353d0e;p=mutilities%2FMUtilities.git diff --git a/src/Registry_Win32.cpp b/src/Registry_Win32.cpp index b1b67d4..6542bdb 100644 --- a/src/Registry_Win32.cpp +++ b/src/Registry_Win32.cpp @@ -25,6 +25,9 @@ #include #include +//Qt +#include + //Win32 #define WIN32_LEAN_AND_MEAN #include @@ -43,6 +46,18 @@ static HKEY registry_root(const int &rootKey) } } +static DWORD registry_access(const int &access) +{ + switch(access) + { + case MUtils::Registry::access_readonly: return KEY_READ; break; + case MUtils::Registry::access_writeonly: return KEY_WRITE; break; + case MUtils::Registry::access_readwrite: return KEY_READ | KEY_WRITE; break; + case MUtils::Registry::access_enumerate: return KEY_ENUMERATE_SUB_KEYS; break; + default: MUTILS_THROW("Unknown access value was specified!"); + } +} + /////////////////////////////////////////////////////////////////////////////// // RegistryKeyPrivate Key Class /////////////////////////////////////////////////////////////////////////////// @@ -58,9 +73,9 @@ namespace MUtils friend class MUtils::Registry::RegistryKey; private: - HKEY m_hKey; - bool m_readOnly; - bool m_isOpen; + HKEY m_hKey; + DWORD m_access; + bool m_isOpen; }; } } @@ -72,9 +87,9 @@ namespace MUtils { \ MUTILS_THROW("Cannot read from or write to a key is not currently open!"); \ } \ - if(p->m_readOnly != (X)) \ + if(!(p->m_access & (X))) \ { \ - MUTILS_THROW("Cannot write to read-only key or read from write-only key!"); \ + MUTILS_THROW("This operation is not support with current access rights!"); \ } \ } \ while(0) @@ -83,15 +98,15 @@ while(0) // Registry Key Class /////////////////////////////////////////////////////////////////////////////// -MUtils::Registry::RegistryKey::RegistryKey(const int &rootKey, const QString &keyName, const bool &readOnly) +MUtils::Registry::RegistryKey::RegistryKey(const int &rootKey, const QString &keyName, const int &access) : p(new Internal::RegistryKeyPrivate()) { - p->m_hKey = NULL; - p->m_readOnly = readOnly; + p->m_hKey = NULL; + p->m_access = registry_access(access); p->m_isOpen = false; - p->m_isOpen = (RegCreateKeyEx(registry_root(rootKey), MUTILS_WCHR(keyName), 0, NULL, 0, p->m_readOnly ? KEY_READ : KEY_WRITE, NULL, &p->m_hKey, NULL) == ERROR_SUCCESS); + p->m_isOpen = (RegCreateKeyEx(registry_root(rootKey), MUTILS_WCHR(keyName), 0, NULL, 0, p->m_access, NULL, &p->m_hKey, NULL) == ERROR_SUCCESS); if(!p->m_isOpen) { qWarning("Failed to open registry key!"); @@ -106,6 +121,7 @@ MUtils::Registry::RegistryKey::~RegistryKey(void) p->m_hKey = NULL; p->m_isOpen = false; } + delete p; } inline bool MUtils::Registry::RegistryKey::isOpen(void) @@ -115,29 +131,30 @@ inline bool MUtils::Registry::RegistryKey::isOpen(void) bool MUtils::Registry::RegistryKey::value_write(const QString &valueName, const quint32 &value) { - CHECK_STATUS(false); + CHECK_STATUS(KEY_WRITE); return (RegSetValueEx(p->m_hKey, valueName.isEmpty() ? NULL : MUTILS_WCHR(valueName), 0, REG_DWORD, reinterpret_cast(&value), sizeof(quint32)) == ERROR_SUCCESS); } bool MUtils::Registry::RegistryKey::value_write(const QString &valueName, const QString &value) { - CHECK_STATUS(false); + CHECK_STATUS(KEY_WRITE); return (RegSetValueEx(p->m_hKey, valueName.isEmpty() ? NULL : MUTILS_WCHR(valueName), 0, REG_SZ, reinterpret_cast(value.utf16()), (value.length() + 1) * sizeof(wchar_t)) == ERROR_SUCCESS); } bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, quint32 &value) const { + value = 0; DWORD size = sizeof(quint32), type = -1; - CHECK_STATUS(false); + 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); } bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, QString &value) const { - wchar_t buffer[2048]; - DWORD size = sizeof(wchar_t) * 2048, type = -1; - CHECK_STATUS(false); - if((RegQueryValueEx(p->m_hKey, valueName.isEmpty() ? NULL : MUTILS_WCHR(valueName), 0, &type, reinterpret_cast(&value), &size) == ERROR_SUCCESS) && ((type == REG_SZ) || (type == REG_EXPAND_SZ))) + 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))) { value = QString::fromUtf16(reinterpret_cast(buffer)); return true; @@ -145,6 +162,44 @@ 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]; + list.clear(); + CHECK_STATUS(KEY_ENUMERATE_SUB_KEYS); + for(DWORD i = 0; i < UINT_MAX; i++) + { + DWORD size = 2048; + const DWORD ret = RegEnumKeyEx(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; +} + /////////////////////////////////////////////////////////////////////////////// // HELPER FUNCTIONS /////////////////////////////////////////////////////////////////////////////// @@ -155,7 +210,7 @@ bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, QString bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const quint32 &value) { bool success = false; - RegistryKey regKey(rootKey, keyName, false); + RegistryKey regKey(rootKey, keyName, access_readwrite); if(regKey.isOpen()) { success = regKey.value_write(valueName, value); @@ -169,7 +224,7 @@ bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyNam bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const QString &value) { bool success = false; - RegistryKey regKey(rootKey, keyName, false); + RegistryKey regKey(rootKey, keyName, access_readwrite); if(regKey.isOpen()) { success = regKey.value_write(valueName, value); @@ -183,11 +238,15 @@ bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyNam bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName, const QString &valueName, quint32 &value) { bool success = false; - RegistryKey regKey(rootKey, keyName, true); + RegistryKey regKey(rootKey, keyName, access_readonly); if(regKey.isOpen()) { success = regKey.value_read(valueName, value); } + else + { + value = 0; + } return success; } @@ -197,11 +256,51 @@ bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName, const QString &valueName, QString &value) { bool success = false; - RegistryKey regKey(rootKey, keyName, true); + RegistryKey regKey(rootKey, keyName, access_readonly); if(regKey.isOpen()) { success = regKey.value_read(valueName, value); } + else + { + value = QString(); + } + return success; +} + +/* + * 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) +{ + bool success = false; + RegistryKey regKey(rootKey, keyName, access_enumerate); + if(regKey.isOpen()) + { + success = regKey.enum_subkeys(subkeys); + } + else + { + subkeys.clear(); + } return success; }