-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwreg.hpp
More file actions
233 lines (191 loc) · 5.89 KB
/
Copy pathwreg.hpp
File metadata and controls
233 lines (191 loc) · 5.89 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
#if !defined(_INC_WREG_HPP_) && defined(__cplusplus)
#define _INC_WREG_HPP_
#ifndef INVALID_HKEY
#define INVALID_HKEY ((HKEY)INVALID_HANDLE_VALUE)
#endif
#include <wbase.hpp>
class WRegKey : public WObjectHandle
{
private:
static bool SetError(LONG lRegError)
{
if (lRegError == NO_ERROR)
{
return true;
}
else
{
SetLastError(lRegError);
return false;
}
}
public:
/// Init with existing handle.
explicit WRegKey(HKEY handle = INVALID_HKEY)
: WObjectHandle((HANDLE)handle)
{
}
/// Init with handle from other object.
explicit WRegKey(const WRegKey &obj)
: WObjectHandle(obj.Handle())
{
}
explicit WRegKey(LPCSTR lpSubKey, HKEY hOpenKey = HKEY_CURRENT_USER,
bool bCreate = false)
: WObjectHandle(INVALID_HANDLE_VALUE)
{
LONG lRegError = (bCreate ? RegCreateKeyA : RegOpenKeyA)
(hOpenKey, lpSubKey, (PHKEY)&hObject);
if (lRegError == ERROR_SUCCESS)
return;
SetLastError(lRegError);
}
explicit WRegKey(LPCWSTR lpSubKey, HKEY hOpenKey = HKEY_CURRENT_USER,
bool bCreate = false)
: WObjectHandle(INVALID_HANDLE_VALUE)
{
LONG lRegError = (bCreate ? RegCreateKeyW : RegOpenKeyW)
(hOpenKey, lpSubKey, (PHKEY)&hObject);
if (lRegError == ERROR_SUCCESS)
return;
SetLastError(lRegError);
}
explicit WRegKey(LPCSTR lpSubKey, WRegKey& OpenKey, bool bCreate = false)
: WObjectHandle(INVALID_HANDLE_VALUE)
{
LONG lRegError = (bCreate ? RegCreateKeyA : RegOpenKeyA)
(OpenKey.hKey(), lpSubKey, (PHKEY)&hObject);
if (lRegError == ERROR_SUCCESS)
return;
SetLastError(lRegError);
}
explicit WRegKey(LPCWSTR lpSubKey, WRegKey& OpenKey, bool bCreate = false)
: WObjectHandle(INVALID_HANDLE_VALUE)
{
LONG lRegError = (bCreate ? RegCreateKeyW : RegOpenKeyW)
(OpenKey.hKey(), lpSubKey, (PHKEY)&hObject);
if (lRegError == ERROR_SUCCESS)
return;
SetLastError(lRegError);
}
/// Closes existing object and init with handle from other object.
WRegKey& operator=(const WRegKey &obj)
{
Close();
hObject = obj.Handle();
return *this;
}
/// Closes existing object and init with duplicate of handle.
WRegKey& operator=(HKEY handle)
{
Close();
hObject = (HANDLE)handle;
return *this;
}
/// Closes the handle.
~WRegKey()
{
Close();
}
/// Returns the handle to the embedded kernel object.
HKEY hKey() const
{
return (HKEY)hObject;
}
/// Closes the embedded handle if it is valid.
bool Close()
{
if (!IsValidHandle())
return false;
LONG lRegError = RegCloseKey(hKey());
if (lRegError == ERROR_SUCCESS)
{
hObject = INVALID_HANDLE_VALUE;
return true;
}
else
{
SetLastError(lRegError);
return false;
}
}
bool QueryKey(LPCSTR lpSubKey, LPSTR lpData, PLONG lpSize)
const
{
if (!IsValidHandle())
return SetError(ERROR_INVALID_HANDLE);
return SetError(RegQueryValueA(hKey(), lpSubKey, lpData, lpSize));
}
bool QueryKey(LPCWSTR lpSubKey, LPWSTR lpData, PLONG lpSize)
const
{
if (!IsValidHandle())
return SetError(ERROR_INVALID_HANDLE);
return SetError(RegQueryValueW(hKey(), lpSubKey, lpData, lpSize));
}
bool SetKey(LPCSTR lpSubKey, LPCSTR lpData, DWORD dwSize)
const
{
if (!IsValidHandle())
return SetError(ERROR_INVALID_HANDLE);
return SetError(RegSetValueA(hKey(), lpSubKey, REG_SZ, lpData, dwSize));
}
bool SetKey(LPCWSTR lpSubKey, LPCWSTR lpData, DWORD dwSize)
const
{
if (!IsValidHandle())
return SetError(ERROR_INVALID_HANDLE);
return SetError(RegSetValueW(hKey(), lpSubKey, REG_SZ, lpData, dwSize));
}
bool QueryValue(LPCSTR lpValueName = NULL, LPVOID lpData = NULL,
LPDWORD lpSize = 0, LPDWORD lpType = NULL)
const
{
if (!IsValidHandle())
return SetError(ERROR_INVALID_HANDLE);
return SetError(RegQueryValueExA(hKey(), lpValueName, NULL, lpType, (LPBYTE)lpData,
lpSize));
}
bool QueryValue(LPCWSTR lpValueName = NULL, LPVOID lpData = NULL,
LPDWORD lpSize = 0, LPDWORD lpType = NULL)
const
{
if (!IsValidHandle())
return SetError(ERROR_INVALID_HANDLE);
return SetError(RegQueryValueExW(hKey(), lpValueName, NULL, lpType, (LPBYTE)lpData,
lpSize));
}
bool SetValue(LPCSTR lpValueName = NULL, const LPVOID lpData = NULL,
DWORD dwSize = 0, DWORD dwType = REG_BINARY)
const
{
if (!IsValidHandle())
return SetError(ERROR_INVALID_HANDLE);
return SetError(RegSetValueExA(hKey(), lpValueName, NULL, dwType,
(CONST LPBYTE)lpData, dwSize));
}
bool SetValue(LPCWSTR lpValueName = NULL, const LPVOID lpData = NULL,
DWORD dwSize = 0, DWORD dwType = REG_BINARY)
const
{
if (!IsValidHandle())
return SetError(ERROR_INVALID_HANDLE);
return SetError(RegSetValueExW(hKey(), lpValueName, NULL, dwType,
(CONST LPBYTE)lpData, dwSize));
}
bool DeleteValue(LPCSTR lpValueName)
const
{
if (!IsValidHandle())
return SetError(ERROR_INVALID_HANDLE);
return SetError(RegDeleteValueA(hKey(), lpValueName));
}
bool DeleteValue(LPCWSTR lpValueName)
const
{
if (!IsValidHandle())
return SetError(ERROR_INVALID_HANDLE);
return SetError(RegDeleteValueW(hKey(), lpValueName));
}
};
#endif // _INC_WREG_HPP_