-
Notifications
You must be signed in to change notification settings - Fork 1
/
Registry.cpp
346 lines (289 loc) · 9.54 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
/*
* $Header: /Book/Registry.cpp 22 16.07.04 10:42 Oslph312 $
*
* This is really a get/setProfile kind of thing.
*/
#include "precomp.h"
#include "RegKey.h"
#include "Registry.h"
#include "RegRename.h"
#include "String.h"
#include "Exception.h"
#include "formatMessage.h"
#define REGKEY_BASE _T( "Software\\Hesselberg Consulting" )
#define APP_NAME _T( "TextEdit" )
#define WIN_SETTINGS _T( "Microsoft\\Windows\\CurrentVersion" )
#define CPL_SETTINGS _T( "Control Panel\\Desktop\\WindowMetrics" )
String Registry::formatKey( HKEY hkRoot, LPCTSTR pszKey ) {
assert( isGoodStringPtr( pszKey ) );
String strKey;
if ( HKEY_CURRENT_USER == hkRoot || HKEY_LOCAL_MACHINE == hkRoot ) {
if ( 0 == _tcsstr( pszKey, WIN_SETTINGS ) &&
0 == _tcsstr( pszKey, CPL_SETTINGS ) )
{
strKey = REGKEY_BASE _T( "\\" ) APP_NAME;
if ( 0 != *pszKey ) {
strKey += _T( "\\" );
}
}
}
if ( 0 != *pszKey ) {
strKey += pszKey;
}
return strKey;
}
HKEY Registry::createKey( HKEY hkRoot, LPCTSTR pszKey ) {
const String strKey = formatKey( hkRoot, pszKey );
HKEY hk = openFormattedKey( hkRoot, strKey.c_str(), KEY_WRITE );
if ( 0 != hk ) {
return hk;
}
DWORD dwDisposition = 0;
const long lResult = RegCreateKeyEx( hkRoot, strKey.c_str(), 0, 0,
REG_OPTION_NON_VOLATILE, KEY_WRITE,
reinterpret_cast< LPSECURITY_ATTRIBUTES >( 0 ),
&hk, &dwDisposition );
return NOERROR == lResult ? hk : 0;
}
/**
* This functions exists merely to avoid calling formatKey
* twice when the first openKey fails in createKey.
*/
HKEY Registry::openFormattedKey( HKEY hkRoot, LPCTSTR pszKey, DWORD dwMode ) {
HKEY hk = 0;
const long lResult = RegOpenKeyEx( hkRoot, pszKey, 0, dwMode, &hk );
if ( NOERROR != lResult ) {
trace( _T( "Unable to open registry key %s: %s\n" ), pszKey, WinException( lResult ).what() );
}
return NOERROR == lResult ? hk : 0;
}
HKEY Registry::openKey( HKEY hkRoot, LPCTSTR pszKey, DWORD dwMode ) {
const String strKey = formatKey( hkRoot, pszKey );
return openFormattedKey( hkRoot, strKey.c_str(), dwMode );
}
/**
* @param hkRoot HKEY_CURRENT_USER or HKEY_LOCAL_MACHINE
* @param pszKey Key name, e.g., "Settings\\RunMaximized"
*/
int Registry::getInt( HKEY hkRoot, LPCTSTR pszKey, LPCTSTR pszName, int nDefault ) {
int nValue = nDefault;
RegKey hk( openKey( hkRoot, pszKey ) );
if ( hk.isValid() ) {
DWORD dwSize = sizeof nValue;
DWORD dwType = 0;
#ifdef _DEBUG
const long lResult =
#endif
RegQueryValueEx( hk, pszName, 0, &dwType, reinterpret_cast< BYTE * >( &nValue ), &dwSize );
// Check type and size for sanity:
assert( 4 == dwSize );
assert( REG_DWORD == dwType || ERROR_FILE_NOT_FOUND == lResult );
}
return nValue;
}
void Registry::setInt(
HKEY hkRoot, LPCTSTR pszKey, LPCTSTR pszName, int nValue )
{
assert( isGoodStringPtr( pszKey ) );
assert( isGoodStringPtr( pszName ) );
RegKey hk( createKey( hkRoot, pszKey ) );
if ( hk.isValid() ) {
#ifdef _DEBUG
const long lResult =
#endif
RegSetValueEx( hk, pszName, 0, REG_DWORD, reinterpret_cast< CONST BYTE * >( &nValue ), sizeof nValue );
assert( NOERROR == lResult );
}
}
void __cdecl Registry::setString(
HKEY hkRoot, LPCTSTR pszKey, LPCTSTR pszName, LPCTSTR pszFmt, ... )
{
assert( isGoodStringPtr( pszKey ) );
assert( isGoodStringPtr( pszName ) );
assert( isGoodStringPtr( pszFmt ) );
va_list vl;
va_start( vl, pszFmt );
String str = formatMessageV( pszFmt, vl );
va_end( vl );
RegKey hk( createKey( hkRoot, pszKey ) );
if ( hk.isValid() ) {
#ifdef _DEBUG
const long lResult =
#endif
RegSetValueEx( hk, pszName, 0, REG_SZ,
reinterpret_cast< CONST BYTE * >( str.c_str() ),
(str.length() + 1) * sizeof( TCHAR ) );
assert( NOERROR == lResult );
}
}
void __cdecl Registry::setString2(
HKEY hkRoot, LPCTSTR pszKey, LPCTSTR pszName, LPCTSTR pszValue)
{
assert(isGoodStringPtr(pszKey));
assert(isGoodStringPtr(pszName));
assert(isGoodStringPtr(pszValue));
String str(pszValue);
RegKey hk(createKey(hkRoot, pszKey));
if (hk.isValid()) {
#ifdef _DEBUG
const long lResult =
#endif
RegSetValueEx(hk, pszName, 0, REG_SZ,
reinterpret_cast< CONST BYTE * >(str.c_str()),
(str.length() + 1) * sizeof(TCHAR));
assert(NOERROR == lResult);
}
}
static void __cdecl setString2(
HKEY hkRoot,
LPCTSTR pszKey,
LPCTSTR pszName,
LPCTSTR pszValue);
String Registry::getString(
HKEY hkRoot, LPCTSTR pszKey, LPCTSTR pszName, LPCTSTR pszDefault )
{
PATHNAME sz = { 0 };
if (0 != pszDefault)
{
assert(isGoodStringPtr(pszDefault));
assert(isGoodStringPtr(sz));
assert(_tcsclen(pszDefault) < dim(sz));
verify(NOERROR == _tcsncpy_s(sz, dim(sz), pszDefault, _tcslen(pszDefault)));
}
RegKey hk( openKey( hkRoot, pszKey ) );
if ( hk.isValid() ) {
DWORD dwSize = sizeof sz; // Bytes, not characters.
DWORD dwType = 0;
#ifdef _DEBUG
const long lResult =
#endif
RegQueryValueEx( hk, pszName, 0, &dwType,
reinterpret_cast< BYTE * >( sz ), &dwSize );
// Note that ERROR_MORE_DATA is one possible return value.
// REG_EXPAND_SZ?
assert( REG_EXPAND_SZ == dwType || REG_SZ == dwType || ERROR_FILE_NOT_FOUND == lResult );
}
return sz;
}
bool Registry::getBlob(
HKEY hkRoot, LPCTSTR pszKey,
LPCTSTR pszName, LPVOID pBlob, UINT cb )
{
RegKey hk( openKey( hkRoot, pszKey ) );
if ( hk.isValid() ) {
DWORD dwSize = cb; // Bytes, not characters.
DWORD dwType = 0; // REG_BINARY expected
const long lResult = RegQueryValueEx( hk, pszName, 0, &dwType, reinterpret_cast< BYTE * >( pBlob ), &dwSize );
// Note that ERROR_MORE_DATA is one possible return value.
assert( REG_BINARY == dwType );
return NOERROR == lResult;
}
return false;
}
String Registry::fileTypeDescriptionFromExtension(
LPCTSTR pszExtension )
{
const String strClass =
Registry::getString( HKEY_CLASSES_ROOT, pszExtension );
const String strDescr =
Registry::getString( HKEY_CLASSES_ROOT, strClass.c_str() );
return strDescr;
}
bool enumOpenedKeyNames(
HKEY hk, LPCTSTR pszKey, DWORD dwIndex, String *pstrName )
{
assert( isGoodStringPtr( pszKey ) );
assert( isGoodPtr( pstrName ) );
PATHNAME szName = { 0 };
PATHNAME szClass = { 0 };
DWORD dwNameSize = dim( szName ); // Characters, not bytes.
DWORD dwClassSize = dim( szClass );
FILETIME ftLastWriteTime = { 0 };
const long lResult = RegEnumKeyEx( hk, dwIndex, szName, &dwNameSize, 0, szClass, &dwClassSize, &ftLastWriteTime );
if ( NOERROR == lResult ) {
assert( 0 != pstrName );
pstrName->assign( szName );
} else if ( ERROR_NO_MORE_ITEMS != lResult ) {
trace( _T( "RegEnumKeyEx %s: %s\n " ), pszKey, WinException( lResult ).what() );
}
return NOERROR == lResult;
}
bool Registry::enumKeyNames(
HKEY hkRoot, LPCTSTR pszKey, DWORD dwIndex, String *pstrName )
{
bool bFound = false;
RegKey hk( openKey( hkRoot, pszKey ) );
if ( hk.isValid() ) {
bFound = enumOpenedKeyNames( hkRoot, pszKey, dwIndex, pstrName );
}
return bFound;
}
PRIVATE bool enumOpenedValues(
HKEY hk, LPCTSTR pszKey, DWORD dwIndex, String *pstrName )
{
PATHNAME szName = { 0 };
DWORD dwNameSize = dim( szName ); // Characters, not bytes.
const long lResult = RegEnumValue(
hk, dwIndex, szName, &dwNameSize, 0, 0, 0, 0 );
if ( NOERROR == lResult ) {
assert( 0 != pstrName );
pstrName->assign( szName );
} else if ( ERROR_NO_MORE_ITEMS != lResult ) {
trace( _T( "RegEnumValue %s: %s\n " ),
pszKey, WinException( lResult ).what() );
}
return NOERROR == lResult;
}
bool Registry::enumValues(
HKEY hkRoot, LPCTSTR pszKey, DWORD dwIndex, String *pstrName )
{
bool bFound = false;
RegKey hk( openKey( hkRoot, pszKey ) );
if ( hk.isValid() ) {
bFound = enumOpenedValues( hkRoot, pszKey, dwIndex, pstrName );
}
return bFound;
}
PRIVATE bool deleteRecursive( HKEY hkRoot, LPCTSTR pszKey ) {
{ // TODO new func -- needs this scope to close RegKey
String strName;
DWORD dwIndex = 0;
RegKey hk( Registry::openFormattedKey( hkRoot, pszKey ) );
if ( !hk.isValid() ) {
return false;
}
while ( enumOpenedKeyNames( hk, pszKey, dwIndex, &strName ) ) {
const String strKey(
formatMessage( _T( "%1\\%2" ), pszKey, strName.c_str() ) );
if ( !deleteRecursive( hkRoot, strKey.c_str() ) ) {
++dwIndex;
}
}
}
const long lResult = RegDeleteKey( hkRoot, pszKey );
if ( NOERROR != lResult ) {
trace( _T( "Error deleting registry key %s: %s\n" ),
pszKey, WinException( lResult ).what() );
}
return NOERROR == lResult;
}
bool Registry::deleteEntry(
HKEY hkRoot, LPCTSTR pszKey, LPCTSTR pszName )
{
if ( 0 == pszName ) {
const String strKey = formatKey( hkRoot, pszKey );
return deleteRecursive( hkRoot, strKey.c_str() ); //*** EXIT PT
}
RegKey hk( openKey( hkRoot, pszKey, KEY_ALL_ACCESS ) );
bool bOK = hk.isValid();
if ( bOK ) {
const long lResult = RegDeleteValue( hk, pszName );
if ( NOERROR != lResult ) {
trace( _T( "Error deleting registry value %s in %s: %s\n" ),
pszName, pszKey, WinException( lResult ).what() );
bOK = false;
}
}
return bOK;
}
// end of file