Some improvements to registry code.

This commit is contained in:
LoRd_MuldeR 2015-07-26 14:22:55 +02:00
parent 910de3d3a7
commit 034e8f94f4
2 changed files with 70 additions and 34 deletions

View File

@ -59,7 +59,7 @@ namespace MUtils
class MUTILS_API RegistryKey class MUTILS_API RegistryKey
{ {
public: public:
RegistryKey(const int &rootKey, const QString &keyName, const int &access); RegistryKey(const reg_root_t &rootKey, const QString &keyName, const reg_access_t &access);
~RegistryKey(void); ~RegistryKey(void);
inline bool isOpen(void); inline bool isOpen(void);
@ -78,13 +78,13 @@ namespace MUtils
}; };
//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 reg_root_t &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 reg_root_t &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 reg_root_t &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 reg_root_t &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 reg_root_t &rootKey, const QString &keyName);
MUTILS_API bool reg_enum_values (const int &rootKey, const QString &keyName, QStringList &list); MUTILS_API bool reg_enum_values (const reg_root_t &rootKey, const QString &keyName, QStringList &list);
MUTILS_API bool reg_enum_subkeys(const int &rootKey, const QString &keyName, QStringList &list); MUTILS_API bool reg_enum_subkeys(const reg_root_t &rootKey, const QString &keyName, QStringList &list);
} }
} }

View File

@ -33,31 +33,67 @@
#include <Windows.h> #include <Windows.h>
#include <Shlwapi.h> #include <Shlwapi.h>
///////////////////////////////////////////////////////////////////////////////
// INTERNAL FUNCTIONS
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static HKEY registry_root(const int &rootKey) #define ENUM2STR(X,Y) do \
{ \
static const char *_name = #Y; \
if((X) == (Y)) return _name; \
} \
while(0)
namespace MUtils
{
namespace Registry
{
static HKEY registry_root(const reg_root_t &rootKey)
{ {
switch(rootKey) switch(rootKey)
{ {
case MUtils::Registry::root_classes: return HKEY_CLASSES_ROOT; break; case root_classes: return HKEY_CLASSES_ROOT; break;
case MUtils::Registry::root_user: return HKEY_CURRENT_USER; break; case root_user: return HKEY_CURRENT_USER; break;
case MUtils::Registry::root_machine: return HKEY_LOCAL_MACHINE; break; case root_machine: return HKEY_LOCAL_MACHINE; break;
default: MUTILS_THROW("Unknown root reg value was specified!"); default: MUTILS_THROW("Unknown root reg value was specified!");
} }
} }
static DWORD registry_access(const int &access) static DWORD registry_access(const reg_access_t &access)
{ {
switch(access) switch(access)
{ {
case MUtils::Registry::access_readonly: return KEY_READ; break; case access_readonly: return KEY_READ; break;
case MUtils::Registry::access_writeonly: return KEY_WRITE; break; case access_writeonly: return KEY_WRITE; break;
case MUtils::Registry::access_readwrite: return KEY_READ | KEY_WRITE; break; case access_readwrite: return KEY_READ | KEY_WRITE; break;
case MUtils::Registry::access_enumerate: return KEY_ENUMERATE_SUB_KEYS; break; case access_enumerate: return KEY_ENUMERATE_SUB_KEYS; break;
default: MUTILS_THROW("Unknown access value was specified!"); default: MUTILS_THROW("Unknown access value was specified!");
} }
} }
static const char* reg_root2str(const reg_root_t &rootKey)
{
ENUM2STR(rootKey, root_classes);
ENUM2STR(rootKey, root_user);
ENUM2STR(rootKey, root_machine);
static const char *unknown = "<unknown>";
return unknown;
}
static const char* reg_access2str(const reg_access_t &access)
{
ENUM2STR(access, access_readonly);
ENUM2STR(access, access_writeonly);
ENUM2STR(access, access_readwrite);
ENUM2STR(access, access_enumerate);
static const char *unknown = "<unknown>";
return unknown;
}
}
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// RegistryKeyPrivate Key Class // RegistryKeyPrivate Key Class
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -98,7 +134,7 @@ while(0)
// Registry Key Class // Registry Key Class
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
MUtils::Registry::RegistryKey::RegistryKey(const int &rootKey, const QString &keyName, const int &access) MUtils::Registry::RegistryKey::RegistryKey(const reg_root_t &rootKey, const QString &keyName, const reg_access_t &access)
: :
p(new Internal::RegistryKeyPrivate()) p(new Internal::RegistryKeyPrivate())
{ {
@ -109,7 +145,7 @@ MUtils::Registry::RegistryKey::RegistryKey(const int &rootKey, const QString &ke
p->m_isOpen = (RegCreateKeyEx(registry_root(rootKey), MUTILS_WCHR(keyName), 0, NULL, 0, p->m_access, 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 \"%s\"! (rootKey: %s, access: %s)", MUTILS_UTF8(keyName), reg_root2str(rootKey), reg_access2str(access));
} }
} }
@ -207,7 +243,7 @@ bool MUtils::Registry::RegistryKey::enum_subkeys(QStringList &list) const
/* /*
* Write registry value * Write registry value
*/ */
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 reg_root_t &rootKey, const QString &keyName, const QString &valueName, const quint32 &value)
{ {
bool success = false; bool success = false;
RegistryKey regKey(rootKey, keyName, access_readwrite); RegistryKey regKey(rootKey, keyName, access_readwrite);
@ -221,7 +257,7 @@ bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyNam
/* /*
* Write registry value * Write registry value
*/ */
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 reg_root_t &rootKey, const QString &keyName, const QString &valueName, const QString &value)
{ {
bool success = false; bool success = false;
RegistryKey regKey(rootKey, keyName, access_readwrite); RegistryKey regKey(rootKey, keyName, access_readwrite);
@ -235,7 +271,7 @@ bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyNam
/* /*
* Read registry value * Read registry value
*/ */
bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName, const QString &valueName, quint32 &value) bool MUtils::Registry::reg_value_read(const reg_root_t &rootKey, const QString &keyName, const QString &valueName, quint32 &value)
{ {
bool success = false; bool success = false;
RegistryKey regKey(rootKey, keyName, access_readonly); RegistryKey regKey(rootKey, keyName, access_readonly);
@ -253,7 +289,7 @@ bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName
/* /*
* Read registry value * Read registry value
*/ */
bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName, const QString &valueName, QString &value) bool MUtils::Registry::reg_value_read(const reg_root_t &rootKey, const QString &keyName, const QString &valueName, QString &value)
{ {
bool success = false; bool success = false;
RegistryKey regKey(rootKey, keyName, access_readonly); RegistryKey regKey(rootKey, keyName, access_readonly);
@ -271,7 +307,7 @@ bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName
/* /*
* Enumerate value names * Enumerate value names
*/ */
bool MUtils::Registry::reg_enum_values(const int &rootKey, const QString &keyName, QStringList &values) bool MUtils::Registry::reg_enum_values(const reg_root_t &rootKey, const QString &keyName, QStringList &values)
{ {
bool success = false; bool success = false;
RegistryKey regKey(rootKey, keyName, access_readonly); RegistryKey regKey(rootKey, keyName, access_readonly);
@ -289,7 +325,7 @@ bool MUtils::Registry::reg_enum_values(const int &rootKey, const QString &keyNam
/* /*
* Enumerate subkey names * Enumerate subkey names
*/ */
bool MUtils::Registry::reg_enum_subkeys(const int &rootKey, const QString &keyName, QStringList &subkeys) bool MUtils::Registry::reg_enum_subkeys(const reg_root_t &rootKey, const QString &keyName, QStringList &subkeys)
{ {
bool success = false; bool success = false;
RegistryKey regKey(rootKey, keyName, access_enumerate); RegistryKey regKey(rootKey, keyName, access_enumerate);
@ -307,7 +343,7 @@ bool MUtils::Registry::reg_enum_subkeys(const int &rootKey, const QString &keyNa
/* /*
* Delete registry key * Delete registry key
*/ */
bool MUtils::Registry::reg_key_delete(const int &rootKey, const QString &keyName) bool MUtils::Registry::reg_key_delete(const reg_root_t &rootKey, const QString &keyName)
{ {
return (SHDeleteKey(registry_root(rootKey), MUTILS_WCHR(keyName)) == ERROR_SUCCESS); return (SHDeleteKey(registry_root(rootKey), MUTILS_WCHR(keyName)) == ERROR_SUCCESS);
} }