-
Notifications
You must be signed in to change notification settings - Fork 20
/
RegKey.cpp
305 lines (251 loc) · 8.09 KB
/
RegKey.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
/* DIME IME for Windows 7/8/10/11
BSD 3-Clause License
Copyright (c) 2022, Jeremy Wu
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Private.h"
#include "RegKey.h"
//---------------------------------------------------------------------
//
// ctor
//
//---------------------------------------------------------------------
CRegKey::CRegKey()
{
_keyHandle = nullptr;
}
//---------------------------------------------------------------------
//
// dtor
//
//---------------------------------------------------------------------
CRegKey::~CRegKey()
{
Close();
}
//---------------------------------------------------------------------
//
// operator
//
//---------------------------------------------------------------------
HKEY CRegKey::GetHKEY()
{
return _keyHandle;
}
//---------------------------------------------------------------------
//
// Create
//
//---------------------------------------------------------------------
LONG CRegKey::Create(_In_ HKEY hKeyPresent, _In_ LPCWSTR pwszKeyName, _In_reads_opt_(255) LPWSTR pwszClass, DWORD dwOptions, REGSAM samDesired, _Inout_ LPSECURITY_ATTRIBUTES lpSecAttr, _Out_opt_ LPDWORD lpdwDisposition)
{
DWORD disposition = 0;
HKEY keyHandle = nullptr;
LONG res = RegCreateKeyEx(hKeyPresent, pwszKeyName, 0,
pwszClass, dwOptions, samDesired, lpSecAttr, &keyHandle, &disposition);
if (lpdwDisposition != nullptr)
{
*lpdwDisposition = disposition;
}
if (res == ERROR_SUCCESS)
{
Close();
_keyHandle = keyHandle;
}
return res;
}
//---------------------------------------------------------------------
//
// Open
//
//---------------------------------------------------------------------
LONG CRegKey::Open(_In_ HKEY hKeyParent, _In_ LPCWSTR pwszKeyName, REGSAM samDesired)
{
HKEY keyHandle = nullptr;
LONG res = RegOpenKeyEx(hKeyParent, pwszKeyName, 0, samDesired, &keyHandle);
if (res == ERROR_SUCCESS)
{
Close();
_keyHandle = keyHandle;
}
return res;
}
//---------------------------------------------------------------------
//
// Close
//
//---------------------------------------------------------------------
LONG CRegKey::Close()
{
LONG res = ERROR_SUCCESS;
if (_keyHandle)
{
res = RegCloseKey(_keyHandle);
_keyHandle = nullptr;
}
return res;
}
//---------------------------------------------------------------------
//
// DeleteSubKey
// RecurseDeleteKey
//
//---------------------------------------------------------------------
LONG CRegKey::DeleteSubKey(_In_ LPCWSTR pwszSubKey)
{
return RegDeleteKey(_keyHandle, pwszSubKey);
}
LONG CRegKey::RecurseDeleteKey(_In_ LPCWSTR pwszSubKey)
{
CRegKey key;
LONG res = key.Open(_keyHandle, pwszSubKey, KEY_READ | KEY_WRITE);
if (res != ERROR_SUCCESS)
{
return res;
}
FILETIME time;
WCHAR subKeyName[256] = {'\0'};
DWORD subKeyNameSize = ARRAYSIZE(subKeyName);
while (RegEnumKeyEx(key.GetHKEY(), 0, subKeyName, &subKeyNameSize, NULL, NULL, NULL, &time) == ERROR_SUCCESS)
{
subKeyName[ARRAYSIZE(subKeyName)-1] = L'\0';
res = key.RecurseDeleteKey(subKeyName);
if (res != ERROR_SUCCESS)
{
return res;
}
subKeyNameSize = ARRAYSIZE(subKeyName);
}
key.Close();
return DeleteSubKey(pwszSubKey);
}
//---------------------------------------------------------------------
//
// DeleteValue
//
//---------------------------------------------------------------------
LONG CRegKey::DeleteValue(_In_ LPCWSTR pwszValue)
{
return RegDeleteValue(_keyHandle, pwszValue);
}
//---------------------------------------------------------------------
//
// QueryStingValue
// SetStringValue
//
//---------------------------------------------------------------------
LONG CRegKey::QueryStringValue(_In_opt_ LPCWSTR pwszValueName, _Out_writes_opt_(*pnChars) LPWSTR pwszValue, _Inout_ ULONG *pnChars)
{
LONG res = 0;
DWORD dataType = REG_NONE;
ULONG pwszValueSize = 0;
if (pnChars == nullptr)
{
return E_INVALIDARG;
}
pwszValueSize = (*pnChars)*sizeof(WCHAR);
*pnChars = 0;
res = RegQueryValueEx(_keyHandle, pwszValueName, NULL, &dataType, (LPBYTE)pwszValue, &pwszValueSize);
if (res != ERROR_SUCCESS)
{
return res;
}
if ((dataType != REG_SZ) && (dataType != REG_EXPAND_SZ))
{
return ERROR_INVALID_DATA;
}
*pnChars = pwszValueSize / sizeof(WCHAR);
return ERROR_SUCCESS;
}
LONG CRegKey::SetStringValue(_In_opt_ LPCWSTR pwszValueName, _In_ LPCWSTR pwszValue, DWORD dwType)
{
size_t lenOfValue = 0;
if (pwszValue == nullptr)
{
return ERROR_INVALID_PARAMETER;
}
if (StringCchLength(pwszValue, STRSAFE_MAX_CCH, &lenOfValue) != S_OK)
{
return ERROR_INVALID_PARAMETER;
}
DWORD len = static_cast<DWORD>(lenOfValue);
return RegSetValueEx(_keyHandle, pwszValueName, NULL, dwType, (LPBYTE)pwszValue, (++len)*sizeof(WCHAR));
}
//---------------------------------------------------------------------
//
// QueryDWORDValue
// SetDWORDValue
//
//---------------------------------------------------------------------
LONG CRegKey::QueryDWORDValue(_In_opt_ LPCWSTR pwszValueName, _Out_ DWORD &dwValue)
{
LONG res = 0;
DWORD dataType = REG_NONE;
ULONG pwszValueSize = 0;
pwszValueSize = sizeof(DWORD);
res = RegQueryValueEx(_keyHandle, pwszValueName, NULL, &dataType, (LPBYTE)(&dwValue), &pwszValueSize);
if (res != ERROR_SUCCESS)
{
return res;
}
if (dataType != REG_DWORD)
{
return ERROR_INVALID_DATA;
}
return ERROR_SUCCESS;
}
LONG CRegKey::SetDWORDValue(_In_opt_ LPCWSTR pwszValueName, DWORD dwValue)
{
return RegSetValueEx(_keyHandle, pwszValueName, NULL, REG_DWORD, (LPBYTE)(&dwValue), sizeof(DWORD));
}
//---------------------------------------------------------------------
//
// QueryBinaryValue
// SetBinaryValue
//
//---------------------------------------------------------------------
LONG CRegKey::QueryBinaryValue(_In_opt_ LPCWSTR pwszValueName, _Out_writes_opt_(cbData) BYTE* lpData, DWORD cbData)
{
LONG res = 0;
DWORD dataType = REG_NONE;
ULONG pwszValueSize = 0;
pwszValueSize = cbData;
res = RegQueryValueEx(_keyHandle, pwszValueName, NULL, &dataType, lpData, &pwszValueSize);
if (res != ERROR_SUCCESS)
{
return res;
}
if (dataType != REG_BINARY)
{
return ERROR_INVALID_DATA;
}
if (pwszValueSize != cbData)
{
return ERROR_INVALID_DATA;
}
return ERROR_SUCCESS;
}
LONG CRegKey::SetBinaryValue(_In_opt_ LPCWSTR pwszValueName, _In_reads_(cbData) BYTE* lpData, DWORD cbData)
{
return RegSetValueEx(_keyHandle, pwszValueName, NULL, REG_BINARY, lpData, cbData);
}