-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbaseReg.cpp
326 lines (302 loc) · 8.88 KB
/
baseReg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include "baseReg.h"
#include <Shlwapi.h>
namespace acm {
////////////////////////////////////////////////////////////////////////////////////////////////////////
// regKeyÀà 2014-08-13
// ×¢²á±í¹ÜÀíÀà
////////////////////////////////////////////////////////////////////////////////////////////////////////
regKey::regKey(void)
: m_hKey(NULL)
, m_lLastError(ERROR_SUCCESS)
{
}
regKey::~regKey(void)
{
close();
}
regKey::operator HKEY() const
{
return m_hKey;
}
bool regKey::succeeded() const
{
return m_hKey && m_lLastError == ERROR_SUCCESS;
}
LONG regKey::getLastError() const
{
return m_lLastError;
}
bool regKey::create(HKEY hKeyParent, PCWSTR pKeyName, REGSAM samDesired /*= KEY_READ | KEY_WRITE*/)
{
assert(hKeyParent);
bool bl = false;
DWORD dw;
HKEY hKey = NULL;
m_lLastError = RegCreateKeyExW(hKeyParent, pKeyName, 0, REG_NONE, REG_OPTION_NON_VOLATILE, samDesired, NULL, &hKey, &dw);
if (m_lLastError == ERROR_SUCCESS)
{
bl = close();
m_hKey = hKey;
}
return bl;
}
bool regKey::open(HKEY hKeyParent, PCWSTR pKeyName, REGSAM samDesired /*= KEY_READ | KEY_WRITE*/)
{
assert(hKeyParent);
bool bl = false;
HKEY hKey = NULL;
m_lLastError = RegOpenKeyExW(hKeyParent, pKeyName, 0, samDesired, &hKey);
if (m_lLastError == ERROR_SUCCESS)
{
bl = close();
m_hKey = hKey;
}
return bl;
}
bool regKey::readValue(PCWSTR pValueName, SBDataType &sbValue, DWORD *pdwType)
{
ULONG nBytes = 0;
m_lLastError = ::RegQueryValueExW(m_hKey, pValueName, NULL, pdwType, NULL, &nBytes);
if (m_lLastError == ERROR_MORE_DATA)
{
sbValue.resize(nBytes);
}
m_lLastError = ::RegQueryValueExW(m_hKey, pValueName, NULL, pdwType, static_cast< LPBYTE >(sbValue.data()), &nBytes);
return m_lLastError == ERROR_SUCCESS;
}
bool regKey::close()
{
m_lLastError = ERROR_SUCCESS;
if (m_hKey)
{
m_lLastError = RegCloseKey(m_hKey);
m_hKey = NULL;
}
return m_lLastError == ERROR_SUCCESS;
}
bool regKey::existsKey(PCWSTR pkeyName, DWORD *pType /*= NULL*/)
{
m_lLastError = RegQueryValueEx(m_hKey, pkeyName, NULL, pType, NULL, NULL);
return m_lLastError != ERROR_FILE_NOT_FOUND;
}
bool regKey::deleteKey(PCWSTR pkeyName)
{
m_lLastError = RegDeleteValue(m_hKey, pkeyName);
return m_lLastError == ERROR_SUCCESS;
}
bool regKey::createKey(PCWSTR pkeyName, PCWSTR pValue)
{
return writeString(pkeyName, pValue);;
//DWORD dwType = 0;
//if (existsKey(pkeyName, &dwType))
//{
// writeString(pkeyName, pValue);
//}
//else
//{
// RegSetValueEx(m_hKey, pkeyName, NULL, REG_SZ, pValue, 0);
//}
}
bool regKey::readUI4(PCWSTR pValueName, DWORD &dwValue)
{
assert(m_hKey);
ULONG nBytes = sizeof(DWORD);
DWORD dwType;
m_lLastError = ::RegQueryValueExW(m_hKey, pValueName, NULL, &dwType, reinterpret_cast<LPBYTE>(&dwValue), &nBytes);
if(m_lLastError != ERROR_SUCCESS) return false;
if (dwType != REG_DWORD)
{
m_lLastError = ERROR_INVALID_DATA;
return false;
}
return true;
}
bool regKey::readUI8(PCWSTR pValueName, ULONGLONG &ullValue)
{
assert(m_hKey);
ULONG nBytes = sizeof(ULONGLONG);
DWORD dwType;
m_lLastError = ::RegQueryValueExW(m_hKey, pValueName, NULL, &dwType, reinterpret_cast<LPBYTE>(&ullValue), &nBytes);
if(m_lLastError != ERROR_SUCCESS) return false;
if (dwType != REG_QWORD)
{
m_lLastError = ERROR_INVALID_DATA;
return false;
}
return true;
}
bool regKey::readString(PCWSTR pValueName, std::wstring &wsValue)
{
assert(m_hKey);
DWORD dwType = 0;
ULONG uiChars = MAX_PATH;
ULONG nBytes = uiChars * sizeof(wchar_t);
wchar_t *pValue = new wchar_t[uiChars];
wsValue.clear();
m_lLastError = ::RegQueryValueExW(m_hKey, pValueName, NULL, &dwType, reinterpret_cast<LPBYTE>(pValue), &nBytes);
if(m_lLastError != ERROR_MORE_DATA)
{
delete [] pValue;
uiChars = nBytes/sizeof(wchar_t);
pValue = new wchar_t[uiChars];
m_lLastError = ::RegQueryValueExW(m_hKey, pValueName, NULL, &dwType, reinterpret_cast<LPBYTE>(pValue), &nBytes);
}
if(dwType != REG_SZ && dwType != REG_EXPAND_SZ)
{
//delete [] pValue;
m_lLastError = ERROR_INVALID_DATA;
//return false;
}
if (pValue != NULL && nBytes !=0 && ((nBytes % sizeof(wchar_t) != 0) || (pValue[nBytes / sizeof(wchar_t) -1] != 0)))
{
//delete [] pValue;
m_lLastError = ERROR_INVALID_DATA;
//return false;
}
else if(nBytes == 0)
pValue[0] = L'\0';
if(m_lLastError == ERROR_SUCCESS)
wsValue = pValue;
delete [] pValue;
return m_lLastError == ERROR_SUCCESS;
}
bool regKey::readStrings(PCWSTR pValueName, std::vector<std::wstring> &vecValue)
{
assert(m_hKey);
UINT uiChars = MAX_PATH;
DWORD dwType = 0;
ULONG nBytes = uiChars * sizeof(wchar_t);
wchar_t *pValue = new wchar_t[uiChars];
vecValue.clear();
m_lLastError = ::RegQueryValueExW(m_hKey, pValueName, NULL, &dwType, reinterpret_cast<LPBYTE>(pValue), &nBytes);
if(m_lLastError != ERROR_MORE_DATA)
{
delete[] pValue;
uiChars = nBytes/sizeof(wchar_t);
pValue = new wchar_t[uiChars];
m_lLastError = ::RegQueryValueExW(m_hKey, pValueName, NULL, &dwType, reinterpret_cast<LPBYTE>(pValue), &nBytes);
}
if (dwType != REG_MULTI_SZ || (dwType == REG_MULTI_SZ && pValue != NULL
&& (nBytes % sizeof(wchar_t) != 0 || nBytes / sizeof(wchar_t) < 1
|| pValue[nBytes / sizeof(wchar_t) -1] != 0
|| ((nBytes/sizeof(wchar_t))>1 && pValue[nBytes / sizeof(wchar_t) - 2] != 0))))
{
m_lLastError = ERROR_INVALID_DATA;
}
if (m_lLastError == ERROR_SUCCESS)
{
wchar_t *pOffset = pValue;
std::size_t len = 0;
while (pOffset < pValue+uiChars)
{
len = std::wcslen(pOffset);
if(len <= 0) break;
vecValue.push_back(pOffset);
pOffset += len + 1;
}
}
//
delete[] pValue;
return m_lLastError == ERROR_SUCCESS;
}
bool regKey::readBinary(PCWSTR pValueName, SBDataType &sbValue)
{
assert(m_hKey);
DWORD dwType = 0;
ULONG uiBytes = 0;
uint8_t *pValue = NULL;
m_lLastError = ::RegQueryValueExW(m_hKey, pValueName, NULL, &dwType, NULL, &uiBytes);
if(m_lLastError != ERROR_MORE_DATA)
{
pValue = new uint8_t[uiBytes];
m_lLastError = ::RegQueryValueExW(m_hKey, pValueName, NULL, &dwType, reinterpret_cast<LPBYTE>(pValue), &uiBytes);
}
if (dwType != REG_BINARY)
{
//delete [] pValue;
m_lLastError = ERROR_INVALID_DATA;
//return false;
}
if(m_lLastError == ERROR_SUCCESS)
sbValue.assign(pValue, pValue + uiBytes);
delete [] pValue;
return m_lLastError == ERROR_SUCCESS;
}
bool regKey::writeUI4(PCWSTR pValueName, DWORD dwValue)
{
assert(m_hKey);
m_lLastError = ::RegSetValueExW(m_hKey, pValueName, 0, REG_DWORD, reinterpret_cast<const BYTE*>(&dwValue), sizeof(DWORD));
return m_lLastError == ERROR_SUCCESS;
}
bool regKey::writeUI8(PCWSTR pValueName, ULONGLONG ullValue)
{
assert(m_hKey);
m_lLastError = ::RegSetValueExW(m_hKey, pValueName, 0, REG_QWORD, reinterpret_cast<const BYTE*>(&ullValue), sizeof(ULONGLONG));
return m_lLastError == ERROR_SUCCESS;
}
bool regKey::writeString(PCWSTR pValueName, const std::wstring &wsValue, DWORD dwType /*= REG_SZ*/)
{
assert(m_hKey);
m_lLastError = ::RegSetValueExW(m_hKey, pValueName, 0, dwType, reinterpret_cast<const BYTE*>(wsValue.data()), (wsValue.length()+1)*sizeof(wchar_t));
return m_lLastError == ERROR_SUCCESS;
}
bool regKey::writeBinary(PCWSTR pValueName, const SBDataType &sbValue)
{
assert(m_hKey);
m_lLastError = ::RegSetValueExW(m_hKey, pValueName, 0, REG_BINARY, reinterpret_cast<const BYTE*>(sbValue.data()), sbValue.size());
return m_lLastError == ERROR_SUCCESS;
}
bool regKey::enumKey(DWORD iIndex, std::wstring &wsName, FILETIME *plastWriteTime /*= NULL*/, ULONG uiChars /*= MAX_PATH*/)
{
assert(m_hKey);
FILETIME ftLastWriteTime = {0};
wchar_t *pName = new wchar_t[uiChars];
if (plastWriteTime == NULL)
{
plastWriteTime = &ftLastWriteTime;
}
m_lLastError = ::RegEnumKeyExW(m_hKey, iIndex, pName, &uiChars, NULL, NULL, NULL, plastWriteTime);
//if(m_lLastError != ERROR_SUCCESS)
//{
// delete [] pName;
// return false;
//}
if(m_lLastError == ERROR_SUCCESS)
wsName = pName;
delete pName;
return m_lLastError == ERROR_SUCCESS;
}
bool regKey::notifyChangeKeyValue(bool bWatchSubtree, DWORD dwNotifyFilter, HANDLE hEvent)
{
if(!hEvent) return false;
assert(m_hKey);
m_lLastError = ::RegNotifyChangeKeyValue(m_hKey, bWatchSubtree, dwNotifyFilter, hEvent, TRUE);
return m_lLastError == ERROR_SUCCESS;
}
bool regKey::readString(HKEY hKey, PCWSTR pSubKeyName, PCWSTR pValue, std::wstring &wsValue)
{
assert(pSubKeyName);
LSTATUS ls = ERROR_SUCCESS;
DWORD dwType = 0;
DWORD dwSize = 0;
wsValue.clear();
ls = SHGetValueW(hKey, pSubKeyName, pValue, &dwType, NULL, &dwSize);
if (ls == ERROR_SUCCESS && dwSize > 0)
{
wchar_t *pbuf = new wchar_t[dwSize+1];
memset(pbuf, 0, (dwSize+1)*sizeof(wchar_t));
ls = SHGetValueW(hKey, pSubKeyName, pValue, &dwType, pbuf, &dwSize);
if(ls == ERROR_SUCCESS)
wsValue = pbuf;
delete[] pbuf;
}
return ls == ERROR_SUCCESS;
}
bool regKey::writeString(HKEY hKey, PCWSTR pSubKeyName, PCWSTR pValue, const std::wstring &wsValue)
{
assert(pSubKeyName);
if(!pSubKeyName) return false;
LSTATUS ls = SHSetValue(hKey, pSubKeyName, pValue, REG_SZ, wsValue.c_str(), wsValue.length()*sizeof(wchar_t));
return ls == ERROR_SUCCESS;
}
}