Various improvements to the Regsitry class.

This commit is contained in:
LoRd_MuldeR 2015-05-03 18:17:54 +02:00
parent 0bad6355d0
commit 78fa3cf146
4 changed files with 87 additions and 26 deletions

View File

@ -39,6 +39,16 @@ namespace MUtils
} }
reg_root_t; reg_root_t;
//Regsitry access
typedef enum
{
access_readonly = 0,
access_writeonly = 1,
access_readwrite = 2,
access_enumerate = 3
}
reg_access_t;
//Forward declaration //Forward declaration
namespace Internal namespace Internal
{ {
@ -49,7 +59,7 @@ namespace MUtils
class MUTILS_API RegistryKey class MUTILS_API RegistryKey
{ {
public: public:
RegistryKey(const int &rootKey, const QString &keyName, const bool &readOnly); RegistryKey(const int &rootKey, const QString &keyName, const int &access);
~RegistryKey(void); ~RegistryKey(void);
inline bool isOpen(void); inline bool isOpen(void);
@ -60,16 +70,19 @@ namespace MUtils
bool value_read(const QString &valueName, quint32 &value) const; bool value_read(const QString &valueName, quint32 &value) const;
bool value_read(const QString &valueName, QString &value) const; bool value_read(const QString &valueName, QString &value) const;
bool enum_subkeys(QStringList &list) const;
private: private:
Internal::RegistryKeyPrivate *const p; Internal::RegistryKeyPrivate *const p;
}; };
//Regsitry functions //Regsitry functions
MUTILS_API bool reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const quint32 &value); MUTILS_API bool reg_value_write (const int &rootKey, const QString &keyName, const QString &valueName, const quint32 &value);
MUTILS_API bool reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const QString &value); MUTILS_API bool reg_value_write (const int &rootKey, const QString &keyName, const QString &valueName, const QString &value);
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, quint32 &value);
MUTILS_API bool reg_value_read (const int &rootKey, const QString &keyName, const QString &valueName, QString &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_key_delete (const int &rootKey, const QString &keyName);
MUTILS_API bool reg_enum_subkeys(const int &rootKey, const QString &keyName, QStringList &subkeys);
} }
} }

View File

@ -31,4 +31,4 @@
#define VER_MUTILS_MAJOR 1 #define VER_MUTILS_MAJOR 1
#define VER_MUTILS_MINOR_HI 0 #define VER_MUTILS_MINOR_HI 0
#define VER_MUTILS_MINOR_LO 2 #define VER_MUTILS_MINOR_LO 3

View File

@ -543,7 +543,7 @@ int MUtils::Internal::selfTest(const char *const buildKey, const bool debug)
if(strncmp(buildKey, MY_BUILD_KEY, 13) || (MY_DEBUG_FLAG != debug)) if(strncmp(buildKey, MY_BUILD_KEY, 13) || (MY_DEBUG_FLAG != debug))
{ {
MUtils::OS::system_message_err(L"MUtils", L"FATAL ERROR: MUtils library version mismatch detected!"); MUtils::OS::system_message_err(L"MUtils", L"FATAL ERROR: MUtils library version mismatch detected!");
MUtils::OS::system_message_wrn(L"MUtils", L"Please re-build the complete solution in order to fix this issue!"); MUtils::OS::system_message_wrn(L"MUtils", L"Perform a clean(!) re-install of the application to fix the problem!");
abort(); abort();
} }
return 0; return 0;

View File

@ -25,6 +25,9 @@
#include <MUtils/Registry.h> #include <MUtils/Registry.h>
#include <MUtils/Exception.h> #include <MUtils/Exception.h>
//Qt
#include <QStringList>
//Win32 //Win32
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <Windows.h> #include <Windows.h>
@ -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 // RegistryKeyPrivate Key Class
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -59,7 +74,7 @@ namespace MUtils
private: private:
HKEY m_hKey; HKEY m_hKey;
bool m_readOnly; DWORD m_access;
bool m_isOpen; bool m_isOpen;
}; };
} }
@ -72,9 +87,9 @@ namespace MUtils
{ \ { \
MUTILS_THROW("Cannot read from or write to a key is not currently open!"); \ 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) while(0)
@ -83,15 +98,15 @@ while(0)
// Registry Key Class // 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(new Internal::RegistryKeyPrivate())
{ {
p->m_hKey = NULL; p->m_hKey = NULL;
p->m_readOnly = readOnly; p->m_access = registry_access(access);
p->m_isOpen = false; 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) if(!p->m_isOpen)
{ {
qWarning("Failed to open registry key!"); qWarning("Failed to open registry key!");
@ -116,20 +131,20 @@ inline bool MUtils::Registry::RegistryKey::isOpen(void)
bool MUtils::Registry::RegistryKey::value_write(const QString &valueName, const quint32 &value) 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<const BYTE*>(&value), sizeof(quint32)) == ERROR_SUCCESS); return (RegSetValueEx(p->m_hKey, valueName.isEmpty() ? NULL : MUTILS_WCHR(valueName), 0, REG_DWORD, reinterpret_cast<const BYTE*>(&value), sizeof(quint32)) == ERROR_SUCCESS);
} }
bool MUtils::Registry::RegistryKey::value_write(const QString &valueName, const QString &value) 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<const BYTE*>(value.utf16()), (value.length() + 1) * sizeof(wchar_t)) == ERROR_SUCCESS); return (RegSetValueEx(p->m_hKey, valueName.isEmpty() ? NULL : MUTILS_WCHR(valueName), 0, REG_SZ, reinterpret_cast<const BYTE*>(value.utf16()), (value.length() + 1) * sizeof(wchar_t)) == ERROR_SUCCESS);
} }
bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, quint32 &value) const bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, quint32 &value) const
{ {
DWORD size = sizeof(quint32), type = -1; DWORD size = sizeof(quint32), type = -1;
CHECK_STATUS(true); CHECK_STATUS(KEY_READ);
return (RegQueryValueEx(p->m_hKey, valueName.isEmpty() ? NULL : MUTILS_WCHR(valueName), 0, &type, reinterpret_cast<BYTE*>(&value), &size) == ERROR_SUCCESS) && (type == REG_DWORD); return (RegQueryValueEx(p->m_hKey, valueName.isEmpty() ? NULL : MUTILS_WCHR(valueName), 0, &type, reinterpret_cast<BYTE*>(&value), &size) == ERROR_SUCCESS) && (type == REG_DWORD);
} }
@ -137,8 +152,8 @@ bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, QString
{ {
wchar_t buffer[2048]; wchar_t buffer[2048];
DWORD size = sizeof(wchar_t) * 2048, type = -1; DWORD size = sizeof(wchar_t) * 2048, type = -1;
CHECK_STATUS(true); CHECK_STATUS(KEY_READ);
if((RegQueryValueEx(p->m_hKey, valueName.isEmpty() ? NULL : MUTILS_WCHR(valueName), 0, &type, reinterpret_cast<BYTE*>(&value), &size) == ERROR_SUCCESS) && ((type == REG_SZ) || (type == REG_EXPAND_SZ))) if((RegQueryValueEx(p->m_hKey, valueName.isEmpty() ? NULL : MUTILS_WCHR(valueName), 0, &type, reinterpret_cast<BYTE*>(&(buffer[0])), &size) == ERROR_SUCCESS) && ((type == REG_SZ) || (type == REG_EXPAND_SZ)))
{ {
value = QString::fromUtf16(reinterpret_cast<const ushort*>(buffer)); value = QString::fromUtf16(reinterpret_cast<const ushort*>(buffer));
return true; return true;
@ -146,6 +161,25 @@ bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, QString
return false; 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<const ushort*>(buffer));
continue;
}
return (ret == ERROR_NO_MORE_ITEMS);
}
return false;
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// HELPER FUNCTIONS // HELPER FUNCTIONS
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -156,7 +190,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 MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const quint32 &value)
{ {
bool success = false; bool success = false;
RegistryKey regKey(rootKey, keyName, false); RegistryKey regKey(rootKey, keyName, access_readwrite);
if(regKey.isOpen()) if(regKey.isOpen())
{ {
success = regKey.value_write(valueName, value); success = regKey.value_write(valueName, value);
@ -170,7 +204,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 MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const QString &value)
{ {
bool success = false; bool success = false;
RegistryKey regKey(rootKey, keyName, false); RegistryKey regKey(rootKey, keyName, access_readwrite);
if(regKey.isOpen()) if(regKey.isOpen())
{ {
success = regKey.value_write(valueName, value); success = regKey.value_write(valueName, value);
@ -184,7 +218,7 @@ 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 MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName, const QString &valueName, quint32 &value)
{ {
bool success = false; bool success = false;
RegistryKey regKey(rootKey, keyName, true); RegistryKey regKey(rootKey, keyName, access_readonly);
if(regKey.isOpen()) if(regKey.isOpen())
{ {
success = regKey.value_read(valueName, value); success = regKey.value_read(valueName, value);
@ -198,7 +232,7 @@ 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 MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName, const QString &valueName, QString &value)
{ {
bool success = false; bool success = false;
RegistryKey regKey(rootKey, keyName, true); RegistryKey regKey(rootKey, keyName, access_readonly);
if(regKey.isOpen()) if(regKey.isOpen())
{ {
success = regKey.value_read(valueName, value); success = regKey.value_read(valueName, value);
@ -206,6 +240,20 @@ bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName
return success; return success;
} }
/*
* Read registry value
*/
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);
}
return success;
}
/* /*
* Delete registry key * Delete registry key
*/ */