DCL 3.7.4
Loading...
Searching...
No Matches
Registry.cpp
Go to the documentation of this file.
1#include <dcl/Config.h>
2
3#ifdef __WINNT__
4#include <windows.h>
5
6#include <dcl/Object.h>
7
8#if __DCL_HAVE_ALLOC_DEBUG
9#undef __DCL_ALLOC_LEVEL
10#define __DCL_ALLOC_LEVEL __DCL_ALLOC_INTERNAL
11#endif
12
13#include <dcl/Registry.h>
14
15#if __DCL_HAVE_THIS_FILE__
16#undef __THIS_FILE__
17static const char_t __THIS_FILE__[] = __T("dcl/core/Registry.cpp");
18#endif
19
20__DCL_BEGIN_NAMESPACE
21
23
24Registry::Registry(HKEY hParentKey)
25{
26 __DCL_ASSERT(hParentKey != NULL);
27
28 __hParentKey = hParentKey;
29 __hKey = NULL;
30}
31
32Registry::Registry(
33 HKEY hParentKey,
34 const String& strKey,
35 REGSAM samDesired // = KEY_READ
37{
38 __DCL_ASSERT(hParentKey != NULL);
39
40 __hParentKey = hParentKey;
41 __hKey = NULL;
42
43 open(strKey, samDesired);
44}
45
46Registry::~Registry()
47{
48 if (opened())
49 close();
50
52}
53
54void Registry::create(
55 const String& strKey,
56 REGSAM samDesired // = KEY_ALL_ACCESS
58{
59 __DCL_ASSERT(!strKey.isEmpty());
60
61 if (opened())
62 close();
63
64 DWORD dwDisposition;
65 LONG lError = RegCreateKeyExW(
66 __hParentKey,
67 strKey.data(),
68 0,
69 NULL,
70 REG_OPTION_NON_VOLATILE,
71 samDesired,
72 NULL,
73 &__hKey,
74 &dwDisposition
75 );
76
77 if (lError != ERROR_SUCCESS) {
78 throw(new IOException(strKey, WINAPI_ERROR(lError)));
79 }
80
81 __strKey = strKey;
82 __samDesired = samDesired;
83}
84
85void Registry::open(
86 const String& strKey,
87 REGSAM samDesired // = KEY_ALL_ACCESS
89{
90 __DCL_ASSERT(!strKey.isEmpty());
91
92 if (opened())
93 close();
94
95 LONG lError = RegOpenKeyExW(
96 __hParentKey,
97 strKey.data(),
98 0,
99 samDesired,
100 &__hKey
101 );
102 if (lError != ERROR_SUCCESS) {
103 throw(new IOException(strKey, WINAPI_ERROR(lError)));
104 }
105
106 __strKey = strKey;
107 __samDesired = samDesired;
108}
109
110void Registry::close()
112{
113 __DCL_ASSERT(opened());
114
115 LONG lError = RegCloseKey(__hKey);
116 __hKey = NULL;
117 if (lError != ERROR_SUCCESS) {
118 throw(new IOException(__strKey, WINAPI_ERROR(lError)));
119 }
120}
121
122LONG Registry::getDataInfo(const String& strName, REGISTRY_DATAINFO& info)
123{
124 __DCL_ASSERT(opened());
125 __DCL_ASSERT(!strName.isEmpty());
126
127 info.dwDataType = REG_NONE;
128 info.dwDataSize = 0;
129
130 return RegQueryValueExW(
131 __hKey,
132 strName.data(),
133 NULL,
134 &info.dwDataType,
135 NULL,
136 &info.dwDataSize
137 );
138}
139
140bool Registry::existsValue(const String& strName)
141{
143 return getDataInfo(strName, info) == ERROR_SUCCESS;
144}
145
146String Registry::getString(const String& strName)
148{
149 __DCL_ASSERT(opened());
150 __DCL_ASSERT(!strName.isEmpty());
151
152 String r;
154 LONG lError = getDataInfo(strName, info);
155 if (lError != ERROR_SUCCESS) {
156 throw(new IOException(strName, WINAPI_ERROR(lError)));
157 }
158
159 if (!((info.dwDataType == REG_EXPAND_SZ || info.dwDataType == REG_SZ))) {
160 throw(new IOException(strName, WINAPI_ERROR(ERROR_INVALID_DATA)));
161 }
162
163 if (info.dwDataSize > 0) {
164 DWORD dwDataSize = info.dwDataSize;
165 CharBuffer* buf = CharBuffer::create(dwDataSize);
166 lError = RegQueryValueExW(
167 __hKey,
168 strName,
169 NULL,
170 NULL,
171 (byte_t*)buf->data(),
172 &dwDataSize
173 );
174
175 if (lError != ERROR_SUCCESS) {
176 buf->release();
177 throw(new IOException(strName, WINAPI_ERROR(lError)));
178 }
179
180 buf->__dataLength = dwDataSize;
181 buf->data()[buf->__dataLength] = L'\0';
182 r = buf;
183 buf->release();
184 }
185
186 return r;
187}
188
189int Registry::getInteger(const String& strName)
191{
192 __DCL_ASSERT(opened());
193 __DCL_ASSERT(!strName.isEmpty());
194
196 LONG lError = getDataInfo(strName, info);
197 if (lError != ERROR_SUCCESS) {
198 throw(new IOException(strName, WINAPI_ERROR(lError)));
199 }
200
201 if (info.dwDataType != REG_DWORD) {
202 throw(new IOException(strName, WINAPI_ERROR(ERROR_INVALID_DATA)));
203 }
204
205 DWORD dwData;
206 DWORD dwDataSize = sizeof(dwData);
207 lError = RegQueryValueExW(
208 __hKey,
209 strName.data(),
210 NULL,
211 NULL,
212 (byte_t*)&dwData,
213 &dwDataSize
214 );
215
216 if (lError != ERROR_SUCCESS) {
217 throw(new IOException(strName, WINAPI_ERROR(lError)));
218 }
219
220 return (int)dwData;
221}
222
223void Registry::setValue(const String& strName, const String& strValue)
225{
226 __DCL_ASSERT(opened());
227 __DCL_ASSERT(!strName.isEmpty());
228 __DCL_ASSERT(strValue.length() < UINT_MAX);
229
230 DWORD len = (DWORD)strValue.length();
231 LONG lError = RegSetValueExW(
232 __hKey,
233 strName.data(),
234 0,
235 REG_SZ,
236 (const byte_t*)strValue.data(),
237 len + 1 // include NULL
238 );
239
240 if (lError != ERROR_SUCCESS) {
241 throw(new IOException(strName, WINAPI_ERROR(lError)));
242 }
243}
244
245void Registry::setValue(const String& strName, int nValue)
247{
248 __DCL_ASSERT(opened());
249 __DCL_ASSERT(!strName.isEmpty());
250 __DCL_ASSERT(sizeof(DWORD) >= sizeof(int));
251
252 DWORD dwData = (DWORD)nValue;
253 LONG lError = RegSetValueExW(
254 __hKey,
255 strName.data(),
256 0,
257 REG_DWORD,
258 (byte_t*)&dwData,
259 sizeof(DWORD)
260 );
261
262 if (lError != ERROR_SUCCESS) {
263 throw(new IOException(strName, WINAPI_ERROR(lError)));
264 }
265}
266
267bool Registry::existsKey(const String& strKeyName, HKEY hParentKey)
268{
269 HKEY hCurrent = NULL;
270 LONG lError = RegOpenKeyExW(
271 hParentKey,
272 strKeyName.data(),
273 0,
274 KEY_READ,
275 &hCurrent
276 );
277 if (lError == ERROR_SUCCESS) {
278 ::RegCloseKey(hCurrent);
279 return true;
280 }
281 return false;
282}
283
284__DCL_END_NAMESPACE
285
286#endif // __WINNT__
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:312
wchar_t char_t
Definition Config.h:247
unsigned char byte_t
Definition Config.h:246
#define __DCL_THROWS1(e)
Definition Config.h:152
#define WINAPI_ERROR(n)
Definition Exception.h:72
IOException *size_t r
Definition MediaInfo.cpp:82
#define __DCL_ASSERT(expr)
Definition Object.h:394
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
Definition Object.h:245
#define __T(str)
Definition Object.h:60
__DCL_BEGIN_NAMESPACE struct _REGISTRY_DATAINFO REGISTRY_DATAINFO
LONG getDataInfo(const String &strName, REGISTRY_DATAINFO &info)
HKEY __hKey
Definition Registry.h:80
String __strKey
Definition Registry.h:82
HKEY __hParentKey
Definition Registry.h:79