• R/O
  • HTTP
  • SSH
  • HTTPS

MUtilities: 提交

MUtilities development repository


Commit MetaInfo

修订版034e8f94f42c73185a17b33b2bba5a20ad7696b1 (tree)
时间2015-07-26 21:22:55
作者LoRd_MuldeR <mulder2@gmx....>
CommiterLoRd_MuldeR

Log Message

Some improvements to registry code.

更改概述

差异

--- a/include/MUtils/Registry.h
+++ b/include/MUtils/Registry.h
@@ -59,7 +59,7 @@ namespace MUtils
5959 class MUTILS_API RegistryKey
6060 {
6161 public:
62- RegistryKey(const int &rootKey, const QString &keyName, const int &access);
62+ RegistryKey(const reg_root_t &rootKey, const QString &keyName, const reg_access_t &access);
6363 ~RegistryKey(void);
6464
6565 inline bool isOpen(void);
@@ -78,13 +78,13 @@ namespace MUtils
7878 };
7979
8080 //Regsitry functions
81- MUTILS_API bool reg_value_write (const int &rootKey, const QString &keyName, const QString &valueName, const quint32 &value);
82- MUTILS_API bool reg_value_write (const int &rootKey, const QString &keyName, const QString &valueName, const QString &value);
83- MUTILS_API bool reg_value_read (const int &rootKey, const QString &keyName, const QString &valueName, quint32 &value);
84- MUTILS_API bool reg_value_read (const int &rootKey, const QString &keyName, const QString &valueName, QString &value);
85- MUTILS_API bool reg_key_delete (const int &rootKey, const QString &keyName);
86- MUTILS_API bool reg_enum_values (const int &rootKey, const QString &keyName, QStringList &list);
87- MUTILS_API bool reg_enum_subkeys(const int &rootKey, const QString &keyName, QStringList &list);
81+ MUTILS_API bool reg_value_write (const reg_root_t &rootKey, const QString &keyName, const QString &valueName, const quint32 &value);
82+ MUTILS_API bool reg_value_write (const reg_root_t &rootKey, const QString &keyName, const QString &valueName, const QString &value);
83+ MUTILS_API bool reg_value_read (const reg_root_t &rootKey, const QString &keyName, const QString &valueName, quint32 &value);
84+ MUTILS_API bool reg_value_read (const reg_root_t &rootKey, const QString &keyName, const QString &valueName, QString &value);
85+ MUTILS_API bool reg_key_delete (const reg_root_t &rootKey, const QString &keyName);
86+ MUTILS_API bool reg_enum_values (const reg_root_t &rootKey, const QString &keyName, QStringList &list);
87+ MUTILS_API bool reg_enum_subkeys(const reg_root_t &rootKey, const QString &keyName, QStringList &list);
8888 }
8989 }
9090
--- a/src/Registry_Win32.cpp
+++ b/src/Registry_Win32.cpp
@@ -34,27 +34,63 @@
3434 #include <Shlwapi.h>
3535
3636 ///////////////////////////////////////////////////////////////////////////////
37+// INTERNAL FUNCTIONS
38+///////////////////////////////////////////////////////////////////////////////
3739
38-static HKEY registry_root(const int &rootKey)
39-{
40- switch(rootKey)
41- {
42- case MUtils::Registry::root_classes: return HKEY_CLASSES_ROOT; break;
43- case MUtils::Registry::root_user: return HKEY_CURRENT_USER; break;
44- case MUtils::Registry::root_machine: return HKEY_LOCAL_MACHINE; break;
45- default: MUTILS_THROW("Unknown root reg value was specified!");
46- }
47-}
40+#define ENUM2STR(X,Y) do \
41+{ \
42+ static const char *_name = #Y; \
43+ if((X) == (Y)) return _name; \
44+} \
45+while(0)
4846
49-static DWORD registry_access(const int &access)
47+namespace MUtils
5048 {
51- switch(access)
49+ namespace Registry
5250 {
53- case MUtils::Registry::access_readonly: return KEY_READ; break;
54- case MUtils::Registry::access_writeonly: return KEY_WRITE; break;
55- case MUtils::Registry::access_readwrite: return KEY_READ | KEY_WRITE; break;
56- case MUtils::Registry::access_enumerate: return KEY_ENUMERATE_SUB_KEYS; break;
57- default: MUTILS_THROW("Unknown access value was specified!");
51+ static HKEY registry_root(const reg_root_t &rootKey)
52+ {
53+ switch(rootKey)
54+ {
55+ case root_classes: return HKEY_CLASSES_ROOT; break;
56+ case root_user: return HKEY_CURRENT_USER; break;
57+ case root_machine: return HKEY_LOCAL_MACHINE; break;
58+ default: MUTILS_THROW("Unknown root reg value was specified!");
59+ }
60+ }
61+
62+ static DWORD registry_access(const reg_access_t &access)
63+ {
64+ switch(access)
65+ {
66+ case access_readonly: return KEY_READ; break;
67+ case access_writeonly: return KEY_WRITE; break;
68+ case access_readwrite: return KEY_READ | KEY_WRITE; break;
69+ case access_enumerate: return KEY_ENUMERATE_SUB_KEYS; break;
70+ default: MUTILS_THROW("Unknown access value was specified!");
71+ }
72+ }
73+
74+ static const char* reg_root2str(const reg_root_t &rootKey)
75+ {
76+ ENUM2STR(rootKey, root_classes);
77+ ENUM2STR(rootKey, root_user);
78+ ENUM2STR(rootKey, root_machine);
79+
80+ static const char *unknown = "<unknown>";
81+ return unknown;
82+ }
83+
84+ static const char* reg_access2str(const reg_access_t &access)
85+ {
86+ ENUM2STR(access, access_readonly);
87+ ENUM2STR(access, access_writeonly);
88+ ENUM2STR(access, access_readwrite);
89+ ENUM2STR(access, access_enumerate);
90+
91+ static const char *unknown = "<unknown>";
92+ return unknown;
93+ }
5894 }
5995 }
6096
@@ -98,7 +134,7 @@ while(0)
98134 // Registry Key Class
99135 ///////////////////////////////////////////////////////////////////////////////
100136
101-MUtils::Registry::RegistryKey::RegistryKey(const int &rootKey, const QString &keyName, const int &access)
137+MUtils::Registry::RegistryKey::RegistryKey(const reg_root_t &rootKey, const QString &keyName, const reg_access_t &access)
102138 :
103139 p(new Internal::RegistryKeyPrivate())
104140 {
@@ -109,7 +145,7 @@ MUtils::Registry::RegistryKey::RegistryKey(const int &rootKey, const QString &ke
109145 p->m_isOpen = (RegCreateKeyEx(registry_root(rootKey), MUTILS_WCHR(keyName), 0, NULL, 0, p->m_access, NULL, &p->m_hKey, NULL) == ERROR_SUCCESS);
110146 if(!p->m_isOpen)
111147 {
112- qWarning("Failed to open registry key!");
148+ qWarning("Failed to open registry key \"%s\"! (rootKey: %s, access: %s)", MUTILS_UTF8(keyName), reg_root2str(rootKey), reg_access2str(access));
113149 }
114150 }
115151
@@ -207,7 +243,7 @@ bool MUtils::Registry::RegistryKey::enum_subkeys(QStringList &list) const
207243 /*
208244 * Write registry value
209245 */
210-bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const quint32 &value)
246+bool MUtils::Registry::reg_value_write(const reg_root_t &rootKey, const QString &keyName, const QString &valueName, const quint32 &value)
211247 {
212248 bool success = false;
213249 RegistryKey regKey(rootKey, keyName, access_readwrite);
@@ -221,7 +257,7 @@ bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyNam
221257 /*
222258 * Write registry value
223259 */
224-bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const QString &value)
260+bool MUtils::Registry::reg_value_write(const reg_root_t &rootKey, const QString &keyName, const QString &valueName, const QString &value)
225261 {
226262 bool success = false;
227263 RegistryKey regKey(rootKey, keyName, access_readwrite);
@@ -235,7 +271,7 @@ bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyNam
235271 /*
236272 * Read registry value
237273 */
238-bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName, const QString &valueName, quint32 &value)
274+bool MUtils::Registry::reg_value_read(const reg_root_t &rootKey, const QString &keyName, const QString &valueName, quint32 &value)
239275 {
240276 bool success = false;
241277 RegistryKey regKey(rootKey, keyName, access_readonly);
@@ -253,7 +289,7 @@ bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName
253289 /*
254290 * Read registry value
255291 */
256-bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName, const QString &valueName, QString &value)
292+bool MUtils::Registry::reg_value_read(const reg_root_t &rootKey, const QString &keyName, const QString &valueName, QString &value)
257293 {
258294 bool success = false;
259295 RegistryKey regKey(rootKey, keyName, access_readonly);
@@ -271,7 +307,7 @@ bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName
271307 /*
272308 * Enumerate value names
273309 */
274-bool MUtils::Registry::reg_enum_values(const int &rootKey, const QString &keyName, QStringList &values)
310+bool MUtils::Registry::reg_enum_values(const reg_root_t &rootKey, const QString &keyName, QStringList &values)
275311 {
276312 bool success = false;
277313 RegistryKey regKey(rootKey, keyName, access_readonly);
@@ -289,7 +325,7 @@ bool MUtils::Registry::reg_enum_values(const int &rootKey, const QString &keyNam
289325 /*
290326 * Enumerate subkey names
291327 */
292-bool MUtils::Registry::reg_enum_subkeys(const int &rootKey, const QString &keyName, QStringList &subkeys)
328+bool MUtils::Registry::reg_enum_subkeys(const reg_root_t &rootKey, const QString &keyName, QStringList &subkeys)
293329 {
294330 bool success = false;
295331 RegistryKey regKey(rootKey, keyName, access_enumerate);
@@ -307,7 +343,7 @@ bool MUtils::Registry::reg_enum_subkeys(const int &rootKey, const QString &keyNa
307343 /*
308344 * Delete registry key
309345 */
310-bool MUtils::Registry::reg_key_delete(const int &rootKey, const QString &keyName)
346+bool MUtils::Registry::reg_key_delete(const reg_root_t &rootKey, const QString &keyName)
311347 {
312348 return (SHDeleteKey(registry_root(rootKey), MUTILS_WCHR(keyName)) == ERROR_SUCCESS);
313349 }
Show on old repository browser