-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRegistry.cpp
431 lines (347 loc) · 10.8 KB
/
Registry.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/////////////////////////////////////////////////////////////////////////////
// Name: Registry.cpp
// Purpose: Read winows registry
// Author: Ashesh Vashi
// Created: 2010-05-25
// RCS-ID:
// Copyright: (c) EnterpriseDB
// Licence: BSD Licence
////////////////////////////////////////////////////////////////////////////
#include <wx/wx.h>
#include "Registry.h"
#ifndef __PGREG_WINREGISTRY__
#define __PGREG_WINREGISTRY__
#ifdef __WXMSW__
#define RESERVED 0
#define PGREG_SEPERATOR wxT("\\")
static struct stdkey {
HKEY key;
wxString name;
}
aStdHKeys[] = {
{HKEY_CLASSES_ROOT, wxT("HKEY_CLASSES_ROOT")},
{HKEY_CURRENT_USER, wxT("HKEY_CURRENT_USER")},
{HKEY_LOCAL_MACHINE, wxT("HKEY_LOCAL_MACHINE") },
{HKEY_USERS, wxT("HKEY_USERS")},
{HKEY_CURRENT_CONFIG, wxT("HKEY_CURRENT_CONFIG")}
};
#define NOS_STD_KEYS (size_t)(sizeof(aStdHKeys)/sizeof(stdkey))
pgRegKey::pgRegKey(HKEY root, const wxString& subkey, PGREGWOWMODE wowMode, PGREGACCESSMODE accessMode)
{
Init(root, subkey, wowMode, accessMode);
}
pgRegKey::pgRegKey(const pgRegKey& parent, const wxString& key)
{
wxString strKey;
strKey << parent.m_strName << PGREG_SEPERATOR << key;
Init(parent.m_hRoot, strKey, PGREG_WOW_DEFAULT, parent.m_accessMode);
m_wowMode = parent.m_wowMode;
}
void pgRegKey::Init(HKEY root, const wxString& subkey, PGREGWOWMODE wowMode, PGREGACCESSMODE accessMode)
{
m_hRoot = root;
m_strName = subkey;
m_accessMode = accessMode;
m_hKey = NULL;
#ifdef __WIN64__
m_wowMode = KEY_WOW64_64KEY;
#else
m_wowMode = KEY_WOW64_32KEY;
#endif
switch (wowMode)
{
case PGREG_WOW32:
#ifdef __WIN64__
m_wowMode = KEY_WOW64_32KEY;
#endif // __WIN64__
break;
case PGREG_WOW64:
#ifndef __WIN64__
// we can only access 64 bit registry under 64 bit platforms
if (::wxIsPlatform64Bit())
m_wowMode = KEY_WOW64_64KEY;
#endif // !__WIN64__
break;
}
}
pgRegKey *pgRegKey::OpenRegKey(HKEY root, const wxString& subkey, PGREGACCESSMODE accessmode, PGREGWOWMODE wowMode)
{
wxString strKey = subkey;
!subkey.IsEmpty() && subkey.EndsWith(wxT("\\"), &strKey);
pgRegKey *tmpKey = new pgRegKey(root, strKey, wowMode, accessmode);
HKEY tmpRegKey = 0;
long nError = ::RegOpenKeyEx(root, (LPCTSTR)strKey, RESERVED,
(accessmode == PGREG_READ ? KEY_READ : KEY_ALL_ACCESS) | tmpKey->m_wowMode,
&tmpRegKey);
if (nError != ERROR_SUCCESS)
{
delete(tmpKey);
tmpKey = NULL;
}
else
tmpKey->m_hKey = tmpRegKey;
return tmpKey;
}
pgRegKey* pgRegKey::CreateRegKey(HKEY root, const wxString& subkey, PGREGWOWMODE wowMode)
{
pgRegKey *key = OpenRegKey(root, subkey, PGREG_WRITE, wowMode);
if (key != NULL)
return key;
wxString strKey = subkey;
!subkey.IsEmpty() && subkey.EndsWith(wxT("\\"), &strKey);
key = new pgRegKey(root, strKey, wowMode, PGREG_WRITE);
HKEY tmpRegKey = 0;
DWORD disposition;
long nError = ::RegCreateKeyEx(root, (LPCTSTR)strKey, RESERVED, NULL, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS | key->m_wowMode, NULL, &tmpRegKey, &disposition);
if (nError != ERROR_SUCCESS)
{
delete(key);
key = NULL;
}
else
key->m_hKey = tmpRegKey;
return key;
}
void pgRegKey::Close()
{
if (m_hKey)
{
::RegCloseKey(m_hKey);
m_hKey = NULL;
}
}
pgRegKey::~pgRegKey()
{
Close();
}
bool pgRegKey::KeyExists(HKEY root, const wxString& subkey, PGREGWOWMODE wowMode)
{
if (subkey.IsEmpty())
return true;
#ifdef __WIN64__
long keyWowMode = KEY_WOW64_64KEY;
#else
long keyWowMode = KEY_WOW64_32KEY;
#endif
switch (wowMode)
{
case PGREG_WOW32:
#ifdef __WIN64__
keyWowMode = KEY_WOW64_32KEY;
#endif // __WIN64__
break;
case PGREG_WOW64:
#ifndef __WIN64__
// we can only access 64 bit registry under 64 bit platforms
if (::wxIsPlatform64Bit())
keyWowMode = KEY_WOW64_64KEY;
#endif // ! __WIN64__
break;
}
HKEY hkeyDummy;
if (::RegOpenKeyEx(root, (LPCTSTR)subkey, RESERVED, KEY_READ | keyWowMode, &hkeyDummy) == ERROR_SUCCESS)
{
::RegCloseKey(hkeyDummy);
return true;
}
return false;
}
bool pgRegKey::GetKeyInfo(size_t *pnSubKeys, size_t *pnMaxKeyLen, size_t *pnValues, size_t *pnMaxValueLen) const
{
long nError = ::RegQueryInfoKey(m_hKey, NULL, NULL, RESERVED, (LPDWORD)pnSubKeys, (LPDWORD)pnMaxKeyLen, NULL,
(LPDWORD)pnValues, (LPDWORD)pnMaxValueLen, NULL, NULL, NULL);
if (nError != ERROR_SUCCESS)
return false;
return true;
}
wxString pgRegKey::ToString() const
{
wxString str;
size_t index = 0;
for (; index < NOS_STD_KEYS; index++)
{
if (m_hRoot == aStdHKeys[index].key)
{
str = aStdHKeys[index].name;
break;
}
}
if (!m_strName.IsEmpty())
str << wxT("\\") << m_strName;
return str;
}
wxString pgRegKey::GetKeyName() const
{
if (!m_strName.IsEmpty())
{
int index = m_strName.Find(wxT('\\'), true);
return m_strName.SubString(index + 1, m_strName.Len());
}
return wxEmptyString;
}
bool pgRegKey::SetValue(const wxString& strVal, DWORD val)
{
if (m_accessMode == PGREG_READ)
return false;
long nError = ::RegSetValueEx(m_hKey, (LPCTSTR)strVal, RESERVED, REG_DWORD,
(const BYTE*)&val, sizeof(val));
if ( nError == ERROR_SUCCESS )
return true;
return false;
}
bool pgRegKey::SetValue(const wxString& strVal, const wxString& val)
{
if (m_accessMode == PGREG_READ)
return false;
long nError = ::RegSetValueEx(m_hKey, (LPCTSTR)strVal, RESERVED, REG_SZ,
(const BYTE*)((LPCTSTR)val), sizeof(TCHAR) * (val.Len() + 1));
if ( nError == ERROR_SUCCESS )
return true;
return false;
}
bool pgRegKey::QueryValue(const wxString& strkey, LPDWORD pVal) const
{
if (strkey.IsEmpty())
return false;
DWORD dwType, dwSize = sizeof(DWORD);
unsigned char * pBuf = (unsigned char *)pVal;
long nError = ::RegQueryValueEx(m_hKey, (LPCTSTR)strkey, RESERVED, &dwType, pBuf, &dwSize);
if (nError != ERROR_SUCCESS)
return false;
if (dwType != REG_DWORD && dwType != REG_DWORD_BIG_ENDIAN)
return false;
return true;
}
bool pgRegKey::QueryValue(const wxString& strVal, wxString& sVal) const
{
DWORD dwType, dwSize;
long nError = RegQueryValueEx(m_hKey, (LPCTSTR)strVal, RESERVED, &dwType, NULL, &dwSize);
if (nError == ERROR_SUCCESS)
{
sVal = wxT("");
if (dwSize == 0)
return true;
wxChar *pBuf = (wxChar *)calloc(dwSize, sizeof(wxChar));
nError = ::RegQueryValueEx(m_hKey, (LPCTSTR)strVal, RESERVED, &dwType, (LPBYTE)pBuf, &dwSize);
DWORD index = 0;
if (dwType == REG_EXPAND_SZ || dwType == REG_MULTI_SZ)
{
wxChar *actualValueStr = (wxChar *)calloc(dwSize, sizeof(wxChar));
memcpy(actualValueStr, pBuf, dwSize * sizeof(wxChar));
wxChar *curr_line;
sVal = wxT("");
do
{
DWORD dwExpSize = ::ExpandEnvironmentStrings((actualValueStr + index), NULL, 0);
curr_line = (wxChar *)calloc(dwExpSize, sizeof(wxChar));
::ExpandEnvironmentStrings(actualValueStr + index, curr_line, dwExpSize);
if (index != 0)
sVal << wxT("\n\r") << curr_line;
else
sVal << curr_line;
index += (DWORD)wxStrlen(actualValueStr + index) + 1;
free(curr_line);
} while (actualValueStr[index] && dwType == REG_MULTI_SZ);
free(actualValueStr);
}
else
sVal = pBuf;
return true;
}
return false;
}
bool pgRegKey::QueryValue(const wxString& strVal, LPBYTE& pVal, DWORD& len) const
{
DWORD dwType;
if(RegQueryValueEx(m_hKey, (LPCTSTR)strVal,
RESERVED, &dwType, pVal, &len) == ERROR_SUCCESS)
return true;
len = 0;
return false;
}
bool pgRegKey::GetFirstValue(wxString& strkey, long &lIndex) const
{
lIndex = 0;
return GetNextValue(strkey, lIndex);
}
bool pgRegKey::GetNextValue(wxString& strval, long &lIndex) const
{
if (lIndex < 0)
return false;
TCHAR szVal[1024];
DWORD dwValLen = 1024;
long nError = RegEnumValue(m_hKey, lIndex, szVal, &dwValLen, RESERVED, NULL, NULL, NULL);
if (nError != ERROR_SUCCESS)
{
if (nError == ERROR_NO_MORE_ITEMS)
{
nError = ERROR_SUCCESS;
lIndex = -1;
}
return false;
}
strval = szVal;
lIndex++;
return true;
}
bool pgRegKey::HasValue(const wxString& strval)
{
long nRetVal = ::RegQueryValueEx(m_hKey, (LPCTSTR)strval, RESERVED, NULL, NULL, NULL);
return nRetVal == ERROR_SUCCESS;
}
bool pgRegKey::GetFirstKey(pgRegKey*& pkey, long &lIndex) const
{
lIndex = 0;
return GetNextKey(pkey, lIndex);
}
bool pgRegKey::GetNextKey(pgRegKey*& pKey, long &lIndex) const
{
pKey = NULL;
if (lIndex < 0)
return false;
TCHAR szKey[1024];
DWORD dwKeyLen = 1023;
long nError = ::RegEnumKeyEx(m_hKey, lIndex, szKey, &dwKeyLen, NULL, NULL, NULL, NULL);
if (nError != ERROR_SUCCESS)
{
lIndex = -1;
return false;
}
lIndex++;
wxString strSubKey = m_strName;
strSubKey << wxT("\\") << szKey;
pgRegKey *tmpKey = new pgRegKey(m_hRoot, (LPCTSTR)strSubKey, PGREG_WOW_DEFAULT, m_accessMode);
tmpKey->m_wowMode = m_wowMode;
HKEY tmpRegKey = 0;
nError
= ::RegOpenKeyEx(m_hRoot, (LPCTSTR)strSubKey, RESERVED, (m_accessMode == PGREG_READ ? KEY_READ : KEY_ALL_ACCESS) | m_wowMode, &tmpRegKey);
if (nError != ERROR_SUCCESS)
{
delete(tmpKey);
tmpKey = NULL;
pKey = NULL;
return false;
}
else
tmpKey->m_hKey = tmpRegKey;
pKey = tmpKey;
return true;
}
bool pgRegKey::HasKey(const wxString& strKey) const
{
wxString strSubKey = m_strName;
strSubKey << wxT("\\") << strKey;
return pgRegKey::KeyExists(m_hRoot, strSubKey, m_wowMode == KEY_WOW64_64KEY ? PGREG_WOW64 : PGREG_WOW32) ;
}
DWORD pgRegKey::GetValueType(const wxString& key) const
{
DWORD dwType;
long nError = RegQueryValueEx((HKEY) m_hKey, (LPCTSTR)key, RESERVED,
&dwType, NULL, NULL);
if (nError != ERROR_SUCCESS)
return REG_NONE;
return dwType;
}
#endif // __WXMSW__
#endif // __PGREG_WINREGISTRY__