vregistry.cpp
15.4 KB
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
Wrapper for the Windows Registry
Version: 1.0
Modified on: 8-20-2009
Created with: QT 4.5 and QT Creator 1.2
Tested on: Windows XP SP3
Bryan Cairns
*/
#include "vregistry.h"
#if defined(Q_OS_WIN)
#include "windows.h"
#include "VQTConvert.h"
#include <QString>
#include <QByteArray>
#include <QStringList>
#include <qdebug.h>
namespace voidrealms
{
namespace win32
{
//////////////////////////////////////////////////////////////////////////////////////
// Windows API Methods
//////////////////////////////////////////////////////////////////////////////////////
//Returns a boolean indicating if the Registry is open
bool VRegistry::isOpen()
{
return this->mOpen;
}
//Opens a Registry Key
bool VRegistry::OpenKey(HKEY RootKey,LPCTSTR SubKey)
{
LONG lResult;
lResult = RegOpenKeyEx(RootKey,SubKey,0,KEY_ALL_ACCESS | KEY_WOW64_64KEY,&mKey);
switch(lResult)
{
case ERROR_SUCCESS: //ok
this->mOpen = true;
break;
default: //an error code from windows
this->mOpen = false;
break;
}
return this->mOpen;
}
//Creates a registry key and opens it
bool VRegistry::CreateKey(HKEY RootKey,LPCTSTR SubKey)
{
LONG lResult;
DWORD dwDisposition;
//Create and open the key
lResult = RegCreateKeyEx(RootKey,SubKey,0,NULL,0,0,NULL,&mKey,&dwDisposition);
switch(lResult)
{
case ERROR_SUCCESS: //ok
this->mOpen = true;
break;
default: //an error code from windows
this->mOpen = false;
break;
}
return this->mOpen;
}
//Closes the Registry Key
bool VRegistry::CloseKey()
{
//If it is not open just return
if(this->mOpen != true) return true;
LONG lResult;
lResult = RegCloseKey(this->mKey);
switch(lResult)
{
case ERROR_SUCCESS: //ok
this->mOpen = false;
break;
default: //an error code from windows
this->mOpen = true;
break;
}
return this->mOpen;
}
//Deletes a registry key and all values
bool VRegistry::DeleteKey(HKEY RootKey,LPCTSTR SubKey)
{
LONG lResult;
bool mReturn;
//Delete a key an all the values
lResult = RegDeleteKey(RootKey,SubKey);
switch(lResult)
{
case ERROR_SUCCESS: //ok
mReturn = true;
break;
default: //an error code from windows
mReturn = false;
break;
}
return mReturn;
}
//Determines if the key exists
bool VRegistry::KeyExists(HKEY RootKey,LPCTSTR SubKey)
{
LONG lResult;
HKEY mTempKey;
lResult = RegOpenKeyEx(RootKey,SubKey,0,KEY_ALL_ACCESS,&mTempKey);
switch(lResult)
{
case ERROR_SUCCESS: //ok
RegCloseKey(mTempKey);
return true;
break;
default: //an error code from windows
return false;
break;
}
}
//Deletes a registry value and all values
bool VRegistry::DeleteValue(LPCTSTR ValueName)
{
LONG lResult;
bool mReturn;
//Delete a key an all the values
lResult = RegDeleteValue(this->mKey,ValueName);
switch(lResult)
{
case ERROR_SUCCESS: //ok
mReturn = true;
break;
default: //an error code from windows
mReturn = false;
break;
}
return mReturn;
}
//Determines if a value exists
bool VRegistry::ValueExists(LPCTSTR ValueName)
{
LONG lResult;
DWORD dwSize = 0;
DWORD ValueType = REG_BINARY;
BYTE* pBuffer = new BYTE[dwSize];
//get the needed buffer size, recreate the buffer to match the size
lResult = RegQueryValueEx(this->mKey,ValueName,0,&ValueType,pBuffer, &dwSize);
delete[] pBuffer;
switch(lResult)
{
case ERROR_SUCCESS: //ok
return true;
break;
case ERROR_MORE_DATA: //buffer was too small
return true;
break;
case ERROR_FILE_NOT_FOUND: //value was not found
return false;
break;
default: //an error code from windows
return false;
break;
}
}
//Gets a Pointer to a Byte array filled with a REG_BINARY value from an open key (be sure to delete[] mVar)
BYTE* VRegistry::QueryValue(LPCTSTR ValueName, DWORD ValueType)
{
LONG lResult;
DWORD dwSize = 0;
BYTE* pBuffer = new BYTE[dwSize];
//get the needed buffer size, recreate the buffer to match the size
lResult = RegQueryValueEx(this->mKey,ValueName,0,&ValueType,pBuffer, &dwSize);
delete[] pBuffer;
pBuffer = new BYTE[dwSize];
//fill the buffer
lResult = RegQueryValueEx(this->mKey,ValueName,0,&ValueType,pBuffer, &dwSize);
switch(lResult)
{
case ERROR_SUCCESS: //ok
return pBuffer;
break;
case ERROR_MORE_DATA: //buffer was too small
throw "ERROR_MORE_DATA = Buffer is too small to read the value";
break;
case ERROR_FILE_NOT_FOUND: //value was not found
throw "ERROR_FILE_NOT_FOUND = The value was not found";
break;
default: //an error code from windows
throw "ERROR = " + lResult;
break;
}
}
//Sets a value in the regsitry
bool VRegistry::SetValue(LPCTSTR ValueName, DWORD ValueType, LPBYTE Value,DWORD dwSize)
{
LONG lResult;
//Attempt to save the value
lResult = RegSetValueEx(this->mKey,ValueName,0,ValueType,Value,dwSize);
switch(lResult)
{
case ERROR_SUCCESS: //ok
return true;
break;
default: //an error code from windows
return false;
break;
}
}
//////////////////////////////////////////////////////////////////////////////////////
// QT Friendly Methods
//////////////////////////////////////////////////////////////////////////////////////
//Opens a Registry Key
bool VRegistry::OpenKey(HKEY RootKey,QString SubKey)
{
LPCTSTR mSubKey = VQTConvert::QString_To_LPCTSTR(SubKey);
return OpenKey(RootKey,mSubKey);
}
//Creates a registry key and opens it
bool VRegistry::CreateKey(HKEY RootKey,QString SubKey)
{
LPCTSTR mSubKey = VQTConvert::QString_To_LPCTSTR(SubKey);
return CreateKey(RootKey,mSubKey);
}
//Deletes a registry key and all values
bool VRegistry::DeleteKey(HKEY RootKey,QString SubKey)
{
LPCTSTR mSubKey = VQTConvert::QString_To_LPCTSTR(SubKey);
return DeleteKey(RootKey,mSubKey);
}
//Determines if the key exists
bool VRegistry::KeyExists(HKEY RootKey,QString SubKey)
{
LPCTSTR mSubKey = VQTConvert::QString_To_LPCTSTR(SubKey);
return KeyExists(RootKey,mSubKey);
}
//Deletes a registry value and all values
bool VRegistry::DeleteValue(QString ValueName)
{
LPCTSTR mValueName = VQTConvert::QString_To_LPCTSTR(ValueName);
return DeleteValue(mValueName);
}
//Determines if a value exists
bool VRegistry::ValueExists(QString ValueName)
{
LPCTSTR mValueName = VQTConvert::QString_To_LPCTSTR(ValueName);
return ValueExists(mValueName);
}
//Gets REG_SZ value as a QString
QString VRegistry::get_REG_SZ(QString ValueName)
{
if (ValueExists(ValueName)) {
LPCTSTR mValueName = VQTConvert::QString_To_LPCTSTR(ValueName);
DWORD mType = REG_SZ;
BYTE* mBuffer = QueryValue(mValueName, mType);
QString mRet = VQTConvert::WCHAR_to_QString((wchar_t*)mBuffer);
delete[] mBuffer;
return mRet;
} else {
return "";
}
}
//Gets REG_EXPAND_SZ value as a QString
QString VRegistry::get_REG_EXPAND_SZ(QString ValueName)
{
if (ValueExists(ValueName)){
LPCTSTR mValueName = VQTConvert::QString_To_LPCTSTR(ValueName);
DWORD mType = REG_EXPAND_SZ;
BYTE* mBuffer = QueryValue(mValueName, mType);
QString mRet = VQTConvert::WCHAR_to_QString((wchar_t*)mBuffer);
delete[] mBuffer;
return mRet;
} else
return "";
}
//Gets REG_DWORD value as a UINT
uint VRegistry::get_REG_DWORD(QString ValueName)
{
LPCTSTR mValueName = VQTConvert::QString_To_LPCTSTR(ValueName);
DWORD mType = REG_DWORD;
BYTE* mBuffer = QueryValue(mValueName, mType);
DWORD mVal = *(DWORD*)&mBuffer[0];
delete[] mBuffer;
return (uint)mVal;
}
//Gets a REG_BINARY value as a QByteArray
QByteArray VRegistry::get_REG_BINARY(QString ValueName)
{
LPCTSTR mValueName = VQTConvert::QString_To_LPCTSTR(ValueName);
DWORD mType = REG_BINARY;
BYTE* mBuffer = QueryValue(mValueName, mType);
QByteArray mArray((const char*)mBuffer);
delete[] mBuffer;
return mArray;
}
//Gets a REG_MUTLI_SZ value as a QStringList
QStringList VRegistry::get_MULTI_SZ(QString ValueName)
{
QStringList mList;
LPCTSTR mValueName = VQTConvert::QString_To_LPCTSTR(ValueName);
DWORD mType = REG_MULTI_SZ;
BYTE* mBuffer = QueryValue(mValueName, mType);
WCHAR* mItems =(WCHAR*)mBuffer;
while(*mItems != '\0')
{
//mItems is the current string
QString mRet = VQTConvert::WCHAR_to_QString(mItems);
mList.append(mRet);
mItems = mItems + lstrlen(mItems); // pStr now points to null at end of string
mItems++; // pStr now points to the next string, or the second null to terminate
}
delete[] mBuffer;
return mList;
}
//Sets a REG_SZ value with a QString
bool VRegistry::set_REG_SZ(QString ValueName, QString Value)
{
LPCTSTR mValueName = VQTConvert::QString_To_LPCTSTR(ValueName);
DWORD mType = REG_SZ;
DWORD mSize = sizeof(Value.utf16()) * Value.length();
return SetValue(mValueName,mType,(LPBYTE)Value.utf16(),mSize);
}
//Sets a REG_EXPAND_SZ value with a QString
bool VRegistry::set_REG_EXPAND_SZ(QString ValueName, QString Value)
{
LPCTSTR mValueName = VQTConvert::QString_To_LPCTSTR(ValueName);
DWORD mType = REG_EXPAND_SZ;
DWORD mSize = sizeof(Value.utf16()) * Value.length();
return SetValue(mValueName,mType,(LPBYTE)Value.utf16(),mSize);
}
//Sets a REG_DWORD value with a UINT
bool VRegistry::set_REG_DWORD(QString ValueName, UINT Value)
{
LPCTSTR mValueName = VQTConvert::QString_To_LPCTSTR(ValueName);
DWORD mType = REG_DWORD;
DWORD mSize = 4;
return SetValue(mValueName,mType,(LPBYTE)&Value,mSize);
}
//Sets a REG_BINARY value with a QByteArray
bool VRegistry::set_REG_BINARY(QString ValueName, QByteArray Value)
{
LPCTSTR mValueName = VQTConvert::QString_To_LPCTSTR(ValueName);
DWORD mType = REG_BINARY;
BYTE* mData = (BYTE*)Value.data();
DWORD mSize = Value.length();
return SetValue(mValueName,mType,mData,mSize);
}
//Sets a REG_MULTI_SZ value with a QStringList
bool VRegistry::set_REG_MULTI_SZ(QString ValueName, QStringList Value)
{
LPCTSTR mValueName = VQTConvert::QString_To_LPCTSTR(ValueName);
DWORD mType = REG_MULTI_SZ;
QString mValue;
//DWORD mSize = 0;
for (int i = 0; i < Value.size(); ++i)
{
mValue += Value.at(i);
mValue += '\0';
}
mValue += '\0';
DWORD mSize = (sizeof(mValue.utf16()) * mValue.length()) / 2;
return SetValue(mValueName,mType,(LPBYTE)mValue.utf16(),mSize);
}
//Gets a string list filled with names of the sub keys from the current open key
QStringList VRegistry::enum_Keys()
{
LONG lResult;
LONG lNumber = 0;
QStringList mList;
bool mProcess = true;
//Enum through the items
do
{
DWORD dwSize = 255;
WCHAR* pBuffer = new WCHAR[dwSize];
lResult = RegEnumKeyEx(this->mKey,lNumber, pBuffer, &dwSize,NULL,NULL,NULL,NULL);
if(lResult == ERROR_SUCCESS)
{
QString mRet = VQTConvert::WCHAR_to_QString(pBuffer);
mList.append(mRet);
}
else
{
mProcess = false;
}
delete[] pBuffer;
lNumber++;
}while(mProcess);
return mList;
}
//Gets a string list filled with names of the values from the current open key
QStringList VRegistry::enum_Values()
{
LONG lResult;
LONG lNumber = 0;
QStringList mList;
bool mProcess = true;
//Enum through the items
do
{
DWORD dwSize = 16383;
WCHAR* pBuffer = new WCHAR[dwSize];
lResult = RegEnumValue(this->mKey,lNumber, pBuffer, &dwSize,NULL,NULL,NULL,NULL);
if(lResult == ERROR_SUCCESS)
{
QString mRet = VQTConvert::WCHAR_to_QString(pBuffer);
mList.append(mRet);
}
else
{
mProcess = false;
}
delete[] pBuffer;
lNumber++;
}while(mProcess);
return mList;
}
} //end voidrealms::win32 namespace
} //end voidrealms namespace
#endif // defined(Q_OS_WIN)