MUtilities development repository
修订版 | 78fa3cf1465df987d56573a67167779a3e515a86 (tree) |
---|---|
时间 | 2015-05-04 01:17:54 |
作者 | ![]() |
Commiter | LoRd_MuldeR |
Various improvements to the Regsitry class.
@@ -39,6 +39,16 @@ namespace MUtils | ||
39 | 39 | } |
40 | 40 | reg_root_t; |
41 | 41 | |
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 | + | |
42 | 52 | //Forward declaration |
43 | 53 | namespace Internal |
44 | 54 | { |
@@ -49,7 +59,7 @@ namespace MUtils | ||
49 | 59 | class MUTILS_API RegistryKey |
50 | 60 | { |
51 | 61 | public: |
52 | - RegistryKey(const int &rootKey, const QString &keyName, const bool &readOnly); | |
62 | + RegistryKey(const int &rootKey, const QString &keyName, const int &access); | |
53 | 63 | ~RegistryKey(void); |
54 | 64 | |
55 | 65 | inline bool isOpen(void); |
@@ -60,16 +70,19 @@ namespace MUtils | ||
60 | 70 | bool value_read(const QString &valueName, quint32 &value) const; |
61 | 71 | bool value_read(const QString &valueName, QString &value) const; |
62 | 72 | |
73 | + bool enum_subkeys(QStringList &list) const; | |
74 | + | |
63 | 75 | private: |
64 | 76 | Internal::RegistryKeyPrivate *const p; |
65 | 77 | }; |
66 | 78 | |
67 | 79 | //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); | |
73 | 86 | } |
74 | 87 | } |
75 | 88 |
@@ -31,4 +31,4 @@ | ||
31 | 31 | |
32 | 32 | #define VER_MUTILS_MAJOR 1 |
33 | 33 | #define VER_MUTILS_MINOR_HI 0 |
34 | -#define VER_MUTILS_MINOR_LO 2 | |
34 | +#define VER_MUTILS_MINOR_LO 3 |
@@ -543,7 +543,7 @@ int MUtils::Internal::selfTest(const char *const buildKey, const bool debug) | ||
543 | 543 | if(strncmp(buildKey, MY_BUILD_KEY, 13) || (MY_DEBUG_FLAG != debug)) |
544 | 544 | { |
545 | 545 | 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!"); | |
547 | 547 | abort(); |
548 | 548 | } |
549 | 549 | return 0; |
@@ -25,6 +25,9 @@ | ||
25 | 25 | #include <MUtils/Registry.h> |
26 | 26 | #include <MUtils/Exception.h> |
27 | 27 | |
28 | +//Qt | |
29 | +#include <QStringList> | |
30 | + | |
28 | 31 | //Win32 |
29 | 32 | #define WIN32_LEAN_AND_MEAN |
30 | 33 | #include <Windows.h> |
@@ -43,6 +46,18 @@ static HKEY registry_root(const int &rootKey) | ||
43 | 46 | } |
44 | 47 | } |
45 | 48 | |
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 | + | |
46 | 61 | /////////////////////////////////////////////////////////////////////////////// |
47 | 62 | // RegistryKeyPrivate Key Class |
48 | 63 | /////////////////////////////////////////////////////////////////////////////// |
@@ -58,9 +73,9 @@ namespace MUtils | ||
58 | 73 | friend class MUtils::Registry::RegistryKey; |
59 | 74 | |
60 | 75 | 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; | |
64 | 79 | }; |
65 | 80 | } |
66 | 81 | } |
@@ -72,9 +87,9 @@ namespace MUtils | ||
72 | 87 | { \ |
73 | 88 | MUTILS_THROW("Cannot read from or write to a key is not currently open!"); \ |
74 | 89 | } \ |
75 | - if(p->m_readOnly != (X)) \ | |
90 | + if(!(p->m_access & (X))) \ | |
76 | 91 | { \ |
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!"); \ | |
78 | 93 | } \ |
79 | 94 | } \ |
80 | 95 | while(0) |
@@ -83,15 +98,15 @@ while(0) | ||
83 | 98 | // Registry Key Class |
84 | 99 | /////////////////////////////////////////////////////////////////////////////// |
85 | 100 | |
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) | |
87 | 102 | : |
88 | 103 | p(new Internal::RegistryKeyPrivate()) |
89 | 104 | { |
90 | - p->m_hKey = NULL; | |
91 | - p->m_readOnly = readOnly; | |
105 | + p->m_hKey = NULL; | |
106 | + p->m_access = registry_access(access); | |
92 | 107 | p->m_isOpen = false; |
93 | 108 | |
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); | |
95 | 110 | if(!p->m_isOpen) |
96 | 111 | { |
97 | 112 | qWarning("Failed to open registry key!"); |
@@ -116,20 +131,20 @@ inline bool MUtils::Registry::RegistryKey::isOpen(void) | ||
116 | 131 | |
117 | 132 | bool MUtils::Registry::RegistryKey::value_write(const QString &valueName, const quint32 &value) |
118 | 133 | { |
119 | - CHECK_STATUS(false); | |
134 | + CHECK_STATUS(KEY_WRITE); | |
120 | 135 | return (RegSetValueEx(p->m_hKey, valueName.isEmpty() ? NULL : MUTILS_WCHR(valueName), 0, REG_DWORD, reinterpret_cast<const BYTE*>(&value), sizeof(quint32)) == ERROR_SUCCESS); |
121 | 136 | } |
122 | 137 | |
123 | 138 | bool MUtils::Registry::RegistryKey::value_write(const QString &valueName, const QString &value) |
124 | 139 | { |
125 | - CHECK_STATUS(false); | |
140 | + CHECK_STATUS(KEY_WRITE); | |
126 | 141 | 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); |
127 | 142 | } |
128 | 143 | |
129 | 144 | bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, quint32 &value) const |
130 | 145 | { |
131 | 146 | DWORD size = sizeof(quint32), type = -1; |
132 | - CHECK_STATUS(true); | |
147 | + CHECK_STATUS(KEY_READ); | |
133 | 148 | return (RegQueryValueEx(p->m_hKey, valueName.isEmpty() ? NULL : MUTILS_WCHR(valueName), 0, &type, reinterpret_cast<BYTE*>(&value), &size) == ERROR_SUCCESS) && (type == REG_DWORD); |
134 | 149 | } |
135 | 150 |
@@ -137,8 +152,8 @@ bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, QString | ||
137 | 152 | { |
138 | 153 | wchar_t buffer[2048]; |
139 | 154 | 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))) | |
142 | 157 | { |
143 | 158 | value = QString::fromUtf16(reinterpret_cast<const ushort*>(buffer)); |
144 | 159 | return true; |
@@ -146,6 +161,25 @@ bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, QString | ||
146 | 161 | return false; |
147 | 162 | } |
148 | 163 | |
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 | + | |
149 | 183 | /////////////////////////////////////////////////////////////////////////////// |
150 | 184 | // HELPER FUNCTIONS |
151 | 185 | /////////////////////////////////////////////////////////////////////////////// |
@@ -156,7 +190,7 @@ bool MUtils::Registry::RegistryKey::value_read(const QString &valueName, QString | ||
156 | 190 | bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const quint32 &value) |
157 | 191 | { |
158 | 192 | bool success = false; |
159 | - RegistryKey regKey(rootKey, keyName, false); | |
193 | + RegistryKey regKey(rootKey, keyName, access_readwrite); | |
160 | 194 | if(regKey.isOpen()) |
161 | 195 | { |
162 | 196 | success = regKey.value_write(valueName, value); |
@@ -170,7 +204,7 @@ bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyNam | ||
170 | 204 | bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyName, const QString &valueName, const QString &value) |
171 | 205 | { |
172 | 206 | bool success = false; |
173 | - RegistryKey regKey(rootKey, keyName, false); | |
207 | + RegistryKey regKey(rootKey, keyName, access_readwrite); | |
174 | 208 | if(regKey.isOpen()) |
175 | 209 | { |
176 | 210 | success = regKey.value_write(valueName, value); |
@@ -184,7 +218,7 @@ bool MUtils::Registry::reg_value_write(const int &rootKey, const QString &keyNam | ||
184 | 218 | bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName, const QString &valueName, quint32 &value) |
185 | 219 | { |
186 | 220 | bool success = false; |
187 | - RegistryKey regKey(rootKey, keyName, true); | |
221 | + RegistryKey regKey(rootKey, keyName, access_readonly); | |
188 | 222 | if(regKey.isOpen()) |
189 | 223 | { |
190 | 224 | success = regKey.value_read(valueName, value); |
@@ -198,7 +232,7 @@ bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName | ||
198 | 232 | bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName, const QString &valueName, QString &value) |
199 | 233 | { |
200 | 234 | bool success = false; |
201 | - RegistryKey regKey(rootKey, keyName, true); | |
235 | + RegistryKey regKey(rootKey, keyName, access_readonly); | |
202 | 236 | if(regKey.isOpen()) |
203 | 237 | { |
204 | 238 | success = regKey.value_read(valueName, value); |
@@ -207,6 +241,20 @@ bool MUtils::Registry::reg_value_read(const int &rootKey, const QString &keyName | ||
207 | 241 | } |
208 | 242 | |
209 | 243 | /* |
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 | +/* | |
210 | 258 | * Delete registry key |
211 | 259 | */ |
212 | 260 | bool MUtils::Registry::reg_key_delete(const int &rootKey, const QString &keyName) |