• R/O
  • HTTP
  • SSH
  • HTTPS

MUtilities: 提交

MUtilities development repository


Commit MetaInfo

修订版78fa3cf1465df987d56573a67167779a3e515a86 (tree)
时间2015-05-04 01:17:54
作者LoRd_MuldeR <mulder2@gmx....>
CommiterLoRd_MuldeR

Log Message

Various improvements to the Regsitry class.

更改概述

差异

--- a/include/MUtils/Registry.h
+++ b/include/MUtils/Registry.h
@@ -39,6 +39,16 @@ namespace MUtils
3939 }
4040 reg_root_t;
4141
42+ //Regsitry access
43+ typedef enum
44+ {
45+ access_readonly = 0,
46+ access_writeonly = 1,
47+ access_readwrite = 2,
48+ access_enumerate = 3
49+ }
50+ reg_access_t;
51+
4252 //Forward declaration
4353 namespace Internal
4454 {
@@ -49,7 +59,7 @@ namespace MUtils
4959 class MUTILS_API RegistryKey
5060 {
5161 public:
52- RegistryKey(const int &rootKey, const QString &keyName, const bool &readOnly);
62+ RegistryKey(const int &rootKey, const QString &keyName, const int &access);
5363 ~RegistryKey(void);
5464
5565 inline bool isOpen(void);
@@ -60,16 +70,19 @@ namespace MUtils
6070 bool value_read(const QString &valueName, quint32 &value) const;
6171 bool value_read(const QString &valueName, QString &value) const;
6272
73+ bool enum_subkeys(QStringList &list) const;
74+
6375 private:
6476 Internal::RegistryKeyPrivate *const p;
6577 };
6678
6779 //Regsitry functions
68- MUTILS_API bool reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const quint32 &value);
69- MUTILS_API bool reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const QString &value);
70- MUTILS_API bool reg_value_read (const int &rootKey, const QString &keyName, const QString &valueName, quint32 &value);
71- MUTILS_API bool reg_value_read (const int &rootKey, const QString &keyName, const QString &valueName, QString &value);
72- MUTILS_API bool reg_key_delete (const int &rootKey, const QString &keyName);
80+ MUTILS_API bool reg_value_write (const int &rootKey, const QString &keyName, const QString &valueName, const quint32 &value);
81+ MUTILS_API bool reg_value_write (const int &rootKey, const QString &keyName, const QString &valueName, const QString &value);
82+ MUTILS_API bool reg_value_read (const int &rootKey, const QString &keyName, const QString &valueName, quint32 &value);
83+ MUTILS_API bool reg_value_read (const int &rootKey, const QString &keyName, const QString &valueName, QString &value);
84+ MUTILS_API bool reg_key_delete (const int &rootKey, const QString &keyName);
85+ MUTILS_API bool reg_enum_subkeys(const int &rootKey, const QString &keyName, QStringList &subkeys);
7386 }
7487 }
7588
--- a/src/Config.h
+++ b/src/Config.h
@@ -31,4 +31,4 @@
3131
3232 #define VER_MUTILS_MAJOR 1
3333 #define VER_MUTILS_MINOR_HI 0
34-#define VER_MUTILS_MINOR_LO 2
34+#define VER_MUTILS_MINOR_LO 3
--- a/src/Global.cpp
+++ b/src/Global.cpp
@@ -543,7 +543,7 @@ int MUtils::Internal::selfTest(const char *const buildKey, const bool debug)
543543 if(strncmp(buildKey, MY_BUILD_KEY, 13) || (MY_DEBUG_FLAG != debug))
544544 {
545545 MUtils::OS::system_message_err(L"MUtils", L"FATAL ERROR: MUtils library version mismatch detected!");
546- MUtils::OS::system_message_wrn(L"MUtils", L"Please re-build the complete solution in order to fix this issue!");
546+ MUtils::OS::system_message_wrn(L"MUtils", L"Perform a clean(!) re-install of the application to fix the problem!");
547547 abort();
548548 }
549549 return 0;
--- a/src/Registry_Win32.cpp
+++ b/src/Registry_Win32.cpp
@@ -25,6 +25,9 @@
2525 #include <MUtils/Registry.h>
2626 #include <MUtils/Exception.h>
2727
28+//Qt
29+#include <QStringList>
30+
2831 //Win32
2932 #define WIN32_LEAN_AND_MEAN
3033 #include <Windows.h>
@@ -43,6 +46,18 @@ static HKEY registry_root(const int &rootKey)
4346 }
4447 }
4548
49+static DWORD registry_access(const int &access)
50+{
51+ switch(access)
52+ {
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!");
58+ }
59+}
60+
4661 ///////////////////////////////////////////////////////////////////////////////
4762 // RegistryKeyPrivate Key Class
4863 ///////////////////////////////////////////////////////////////////////////////
@@ -58,9 +73,9 @@ namespace MUtils
5873 friend class MUtils::Registry::RegistryKey;
5974
6075 private:
61- HKEY m_hKey;
62- bool m_readOnly;
63- bool m_isOpen;
76+ HKEY m_hKey;
77+ DWORD m_access;
78+ bool m_isOpen;
6479 };
6580 }
6681 }
@@ -72,9 +87,9 @@ namespace MUtils
7287 { \
7388 MUTILS_THROW("Cannot read from or write to a key is not currently open!"); \
7489 } \
75- if(p->m_readOnly != (X)) \
90+ if(!(p->m_access & (X))) \
7691 { \
77- MUTILS_THROW("Cannot write to read-only key or read from write-only key!"); \
92+ MUTILS_THROW("This operation is not support with current access rights!"); \
7893 } \
7994 } \
8095 while(0)
@@ -83,15 +98,15 @@ while(0)
8398 // Registry Key Class
8499 ///////////////////////////////////////////////////////////////////////////////
85100
86-MUtils::Registry::RegistryKey::RegistryKey(const int &rootKey, const QString &keyName, const bool &readOnly)
101+MUtils::Registry::RegistryKey::RegistryKey(const int &rootKey, const QString &keyName, const int &access)
87102 :
88103 p(new Internal::RegistryKeyPrivate())
89104 {
90- p->m_hKey = NULL;
91- p->m_readOnly = readOnly;
105+ p->m_hKey = NULL;
106+ p->m_access = registry_access(access);
92107 p->m_isOpen = false;
93108
94- 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);
109+ p->m_isOpen = (RegCreateKeyEx(registry_root(rootKey), MUTILS_WCHR(keyName), 0, NULL, 0, p->m_access, NULL, &p->m_hKey, NULL) == ERROR_SUCCESS);
95110 if(!p->m_isOpen)
96111 {
97112 qWarning("Failed to open registry key!");
@@ -116,20 +131,20 @@ inline bool MUtils::Registry::RegistryKey::isOpen(void)
116131
117132 bool MUtils::Registry::RegistryKey::value_write(const QString &valueName, const quint32 &value)
118133 {
119- CHECK_STATUS(false);
134+ CHECK_STATUS(KEY_WRITE);
120135 return (RegSetValueEx(p->m_hKey, valueName.isEmpty() ? NULL : MUTILS_WCHR(valueName), 0, REG_DWORD, reinterpret_cast<const BYTE*>(&value), sizeof(quint32)) == ERROR_SUCCESS);
121136 }
122137
123138 bool MUtils::Registry::RegistryKey::value_write(const QString &valueName, const QString &value)
124139 {
125- CHECK_STATUS(false);
140+ CHECK_STATUS(KEY_WRITE);
126141 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);
127142 }
128143
129144 bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, quint32 &value) const
130145 {
131146 DWORD size = sizeof(quint32), type = -1;
132- CHECK_STATUS(true);
147+ CHECK_STATUS(KEY_READ);
133148 return (RegQueryValueEx(p->m_hKey, valueName.isEmpty() ? NULL : MUTILS_WCHR(valueName), 0, &type, reinterpret_cast<BYTE*>(&value), &size) == ERROR_SUCCESS) && (type == REG_DWORD);
134149 }
135150
@@ -137,8 +152,8 @@ bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, QString
137152 {
138153 wchar_t buffer[2048];
139154 DWORD size = sizeof(wchar_t) * 2048, type = -1;
140- CHECK_STATUS(true);
141- 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)))
155+ CHECK_STATUS(KEY_READ);
156+ 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)))
142157 {
143158 value = QString::fromUtf16(reinterpret_cast<const ushort*>(buffer));
144159 return true;
@@ -146,6 +161,25 @@ bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, QString
146161 return false;
147162 }
148163
164+bool MUtils::Registry::RegistryKey::enum_subkeys(QStringList &list) const
165+{
166+ wchar_t buffer[2048];
167+ list.clear();
168+ CHECK_STATUS(KEY_ENUMERATE_SUB_KEYS);
169+ for(DWORD i = 0; i < UINT_MAX; i++)
170+ {
171+ DWORD size = 2048;
172+ const DWORD ret = RegEnumKeyEx(p->m_hKey, i, buffer, &size, NULL, NULL, NULL, NULL);
173+ if(ret == ERROR_SUCCESS)
174+ {
175+ list << QString::fromUtf16(reinterpret_cast<const ushort*>(buffer));
176+ continue;
177+ }
178+ return (ret == ERROR_NO_MORE_ITEMS);
179+ }
180+ return false;
181+}
182+
149183 ///////////////////////////////////////////////////////////////////////////////
150184 // HELPER FUNCTIONS
151185 ///////////////////////////////////////////////////////////////////////////////
@@ -156,7 +190,7 @@ bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, QString
156190 bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const quint32 &value)
157191 {
158192 bool success = false;
159- RegistryKey regKey(rootKey, keyName, false);
193+ RegistryKey regKey(rootKey, keyName, access_readwrite);
160194 if(regKey.isOpen())
161195 {
162196 success = regKey.value_write(valueName, value);
@@ -170,7 +204,7 @@ bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyNam
170204 bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const QString &value)
171205 {
172206 bool success = false;
173- RegistryKey regKey(rootKey, keyName, false);
207+ RegistryKey regKey(rootKey, keyName, access_readwrite);
174208 if(regKey.isOpen())
175209 {
176210 success = regKey.value_write(valueName, value);
@@ -184,7 +218,7 @@ bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyNam
184218 bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName, const QString &valueName, quint32 &value)
185219 {
186220 bool success = false;
187- RegistryKey regKey(rootKey, keyName, true);
221+ RegistryKey regKey(rootKey, keyName, access_readonly);
188222 if(regKey.isOpen())
189223 {
190224 success = regKey.value_read(valueName, value);
@@ -198,7 +232,7 @@ bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName
198232 bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName, const QString &valueName, QString &value)
199233 {
200234 bool success = false;
201- RegistryKey regKey(rootKey, keyName, true);
235+ RegistryKey regKey(rootKey, keyName, access_readonly);
202236 if(regKey.isOpen())
203237 {
204238 success = regKey.value_read(valueName, value);
@@ -207,6 +241,20 @@ bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName
207241 }
208242
209243 /*
244+ * Read registry value
245+ */
246+bool MUtils::Registry::reg_enum_subkeys(const int &rootKey, const QString &keyName, QStringList &subkeys)
247+{
248+ bool success = false;
249+ RegistryKey regKey(rootKey, keyName, access_enumerate);
250+ if(regKey.isOpen())
251+ {
252+ success = regKey.enum_subkeys(subkeys);
253+ }
254+ return success;
255+}
256+
257+/*
210258 * Delete registry key
211259 */
212260 bool MUtils::Registry::reg_key_delete(const int &rootKey, const QString &keyName)
Show on old repository browser