forked from LemonHaze420/DCPopulous
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistryfunctions.cpp
More file actions
323 lines (299 loc) · 10.9 KB
/
registryfunctions.cpp
File metadata and controls
323 lines (299 loc) · 10.9 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
#include "RegistryFunctions.h"
#include <stdio.h>
#ifdef REMOTE_REGISTRY_BUILD
#include <Rapi.h>
#endif
// ----
// Validates that a key exists.
bool ValidateRegistry( HKEY _ExistingKey, // Existing key or default root.
CString* _SubKey, // Sub key, relative to _ExistingKey. (Must not start with a '/' char!)
CString* _ValueName, // Name of value to retrieve.
DWORD _ValueType)
{
LONG ret; // Values returned from function calls.
HKEY KeyResult; // Our key reference.
DWORD BufferLength; // Size of the buffer.
DWORD ValueType;
// Open the spefified key.
#ifdef _WIN32_WCE
ret = RegOpenKeyEx( _ExistingKey, // Handle to open key.
_SubKey->GetString(), // Address of name of subkey to open.
0, // Reserved.
0, // Security access mask. (Currently not supported)
&KeyResult); // Address of handle to open key.
#else
#ifndef REMOTE_REGISTRY_BUILD
ret = RegOpenKeyEx( _ExistingKey, // Handle to open key.
_SubKey->GetString(), // Address of name of subkey to open.
0, // Reserved.
KEY_ALL_ACCESS, // Security access mask.
&KeyResult); // Address of handle to open key.
#else
ret = CeRegOpenKeyEx( _ExistingKey, // Handle to open key.
_SubKey->GetString(), // Address of name of subkey to open.
0, // Reserved.
0, // Security access mask.
&KeyResult); // Address of handle to open key.
#endif
#endif
if (SUCCEEDED(ret))
{
// Accertain the amount of space required...
BufferLength = 0;
#ifndef REMOTE_REGISTRY_BUILD
ret = RegQueryValueEx( KeyResult, // handle to key to query
_ValueName->GetString(), // address of name of value to query
0, // reserved
&ValueType, // address of buffer for value type
NULL, // address of data buffer
(unsigned long*)&BufferLength); // address of data buffer size
#else
ret = CeRegQueryValueEx( KeyResult, // handle to key to query
_ValueName->GetString(), // address of name of value to query
0, // reserved
&ValueType, // address of buffer for value type
NULL, // address of data buffer
(unsigned long*)&BufferLength); // address of data buffer size
#endif
if ( (SUCCEEDED(ret))
&& (ValueType == _ValueType)
)
{
return true;
}
}
return false;
}
// ----
// Registry load / save routines.
LONG LoadFromRegistry( HKEY _ExistingKey, // Existing key or default root.
CString* _SubKey, // Sub key, relative to _ExistingKey. (Must not start with a '/' char!)
CString* _ValueName, // Name of value to retrieve.
DWORD* _ValueType, // A pointer to a DWORD filled-in by the function.
char** _Value, // A pointer to buffer created within the function - you are responsible for the freeing of this resource.
DWORD* _DataLength) // A pointer to a DWORD filled-in by the function that indicates the length of the data (in bytes) returned for _Value.
{
LONG ret; // Values returned from function calls.
HKEY KeyResult; // Our key reference.
unsigned char* Buffer; // Buffer for data (the address of this buffer will be returned to the user).
DWORD BufferLength; // Size of the buffer.
// Open root key.
#ifdef _WIN32_WCE
ret = RegOpenKeyEx( _ExistingKey, // Handle to open key.
_SubKey->GetString(), // Address of name of subkey to open.
0, // Reserved.
0, // Security access mask. (Currently not supported)
&KeyResult); // Address of handle to open key.
#else
#ifndef REMOTE_REGISTRY_BUILD
ret = RegOpenKeyEx( _ExistingKey, // Handle to open key.
_SubKey->GetString(), // Address of name of subkey to open.
0, // Reserved.
KEY_ALL_ACCESS, // Security access mask.
&KeyResult); // Address of handle to open key.
#else
ret = CeRegOpenKeyEx( _ExistingKey, // Handle to open key.
_SubKey->GetString(), // Address of name of subkey to open.
0, // Reserved.
0, // Security access mask.
&KeyResult); // Address of handle to open key.
#endif
#endif
if (SUCCEEDED(ret))
{
// Accertain the amount of space required...
BufferLength = 0;
#ifndef REMOTE_REGISTRY_BUILD
ret = RegQueryValueEx( KeyResult, // handle to key to query
_ValueName->GetString(), // address of name of value to query
0, // reserved
_ValueType, // address of buffer for value type
NULL, // address of data buffer
(unsigned long*)&BufferLength); // address of data buffer size
#else
ret = CeRegQueryValueEx( KeyResult, // handle to key to query
_ValueName->GetString(), // address of name of value to query
0, // reserved
_ValueType, // address of buffer for value type
NULL, // address of data buffer
(unsigned long*)&BufferLength); // address of data buffer size
#endif
if (SUCCEEDED(ret) && (BufferLength > 0))
{
// Allocate sufficient memory.
Buffer = new unsigned char[BufferLength];
if (Buffer)
{
#ifndef REMOTE_REGISTRY_BUILD
ret = RegQueryValueEx( KeyResult, // handle to key to query
_ValueName->GetString(), // address of name of value to query
0, // reserved
_ValueType, // address of buffer for value type
Buffer, // address of data buffer
(unsigned long*)&BufferLength); // address of data buffer size
#else
ret = CeRegQueryValueEx( KeyResult, // handle to key to query
_ValueName->GetString(), // address of name of value to query
0, // reserved
_ValueType, // address of buffer for value type
Buffer, // address of data buffer
(unsigned long*)&BufferLength); // address of data buffer size
#endif
// Close key.
#ifndef REMOTE_REGISTRY_BUILD
RegCloseKey(KeyResult);
#else
CeRegCloseKey(KeyResult);
#endif
if (SUCCEEDED(ret))
{
// ValueType is already assigned.
*_Value = (char*)Buffer;
*_DataLength = BufferLength;
return ret;
}
else
{
**_Value = NULL;
*_DataLength = 0;
delete[] Buffer;
Buffer = NULL;
return ret;
}
// Will have always returned by now.
}
}
else
{
#ifndef REMOTE_REGISTRY_BUILD
RegCloseKey(KeyResult);
#else
CeRegCloseKey(KeyResult);
#endif
return ret;
}
}
return ret;
}
// ----
LONG SaveToRegistry(HKEY _ExistingKey, // Existing key or default root.
CString* _SubKey, // Sub key, relative to _ExistingKey. (Must not start with a '/' char!)
CString* _ValueName, // Name of the value to store.
DWORD _ValueType, // Specifies the format for storing the data.
char* _Value, // A pointer to the data to store.
DWORD _DataLength) // Length of the _Value data, in bytes.
{
LONG ret; // Values returned from function calls.
HKEY KeyResult; // Our key reference.
DWORD KeyState; // State of that key.
// Create / Open key.
#ifdef _WIN32_WCE
ret = RegCreateKeyEx( _ExistingKey, // handle to an open key
_SubKey->GetString(), // address of subkey name
0, // reserved
TEXT("REG_SZ"), // address of class string
0, // special options flag (no currently supported)
0, // desired security access mask (no supported)
NULL, // address of key security structure
&KeyResult, // address of buffer for opened handle
&KeyState); // address of disposition value buffer
#else
#ifndef REMOTE_REGISTRY_BUILD
ret = RegCreateKeyEx( _ExistingKey, // handle to an open key
_SubKey->GetString(), // address of subkey name
0, // reserved
TEXT("REG_SZ"), // address of class string
REG_OPTION_NON_VOLATILE,// special options flag
KEY_ALL_ACCESS, // desired security access
NULL, // address of key security structure
&KeyResult, // address of buffer for opened handle
&KeyState); // address of disposition value buffer
#else
// Okay this build allows me to save information
// to a CE device attacted to the machine I'm using.
ret = CeRegCreateKeyEx( _ExistingKey, // handle to an open key
_SubKey->GetString(), // address of subkey name
0, // reserved
TEXT("REG_SZ"), // address of class string
0, // special options flag
0, // desired security access
NULL, // address of key security structure
&KeyResult, // address of buffer for opened handle
&KeyState);
#endif
#endif
if (SUCCEEDED(ret))
{
#ifndef REMOTE_REGISTRY_BUILD
ret = RegSetValueEx(KeyResult, // handle to key to set value for
_ValueName->GetString(), // name of the value to set
0, // reserved
_ValueType, // flag for value type
(const unsigned char*)_Value, // address of value data
_DataLength); // size of value data
#else
ret = CeRegSetValueEx(KeyResult, // handle to key to set value for
_ValueName->GetString(), // name of the value to set
0, // reserved
_ValueType, // flag for value type
(unsigned char*)_Value, // address of value data
_DataLength); // size of value data
#endif
// Free handle.
#ifndef REMOTE_REGISTRY_BUILD
RegCloseKey(KeyResult);
#else
CeRegCloseKey(KeyResult);
#endif
return ret;
}
return ret;
}
// ----
// Saves data to the registry.
LONG RemoveRegistryValue( HKEY _ExistingKey, // Existing key or default root.
CString* _SubKey, // Sub key, relative to _ExistingKey. (Must not start with a '/' char!)
CString* _ValueName) // Name of the value to remove
{
LONG ret; // Values returned from function calls.
HKEY KeyResult; // Our key reference.
// Open root
#ifdef _WIN32_WCE
ret = RegOpenKeyEx( _ExistingKey, // Handle to open key.
_SubKey->GetString(), // Address of name of subkey to open.
0, // Reserved.
0, // Security access mask. (Currently not supported)
&KeyResult); // Address of handle to open key.
#else
#ifndef REMOTE_REGISTRY_BUILD
ret = RegOpenKeyEx( _ExistingKey, // Handle to open key.
_SubKey->GetString(), // Address of name of subkey to open.
0, // Reserved.
KEY_ALL_ACCESS, // Security access mask.
&KeyResult); // Address of handle to open key.
#else
ret = CeRegOpenKeyEx( _ExistingKey, // Handle to open key.
_SubKey->GetString(), // Address of name of subkey to open.
0, // Reserved.
0, // Security access mask.
&KeyResult); // Address of handle to open key.
#endif
#endif
if (SUCCEEDED(ret))
{
#ifndef REMOTE_REGISTRY_BUILD
ret = RegDeleteValue( KeyResult,
_ValueName->GetString());
#else
ret = CeRegDeleteValue( KeyResult,
_ValueName->GetString());
#endif
}
// Close key.
#ifndef REMOTE_REGISTRY_BUILD
RegCloseKey(KeyResult);
#else
CeRegCloseKey(KeyResult);
#endif
return ret;
}