DCL 4.0
Loading...
Searching...
No Matches
Registry.cpp
Go to the documentation of this file.
1#include <dcl/Config.h>
2
3#if __DCL_WINDOWS
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 m_hParentKey = hParentKey;
29 m_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 m_hParentKey = hParentKey;
41 m_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 if (opened())
61 close();
62
63 DWORD dwDisposition;
64 LONG lError = ::RegCreateKeyExW(
65 m_hParentKey,
66 strKey.data(),
67 0,
68 NULL,
69 REG_OPTION_NON_VOLATILE,
70 samDesired,
71 NULL,
72 &m_hKey,
73 &dwDisposition
74 );
75
76 if (lError != ERROR_SUCCESS)
77 {
78 throw(new IOException(strKey, WINAPI_ERROR(lError)));
79 }
80
81 m_strKey = strKey;
82 m_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 m_hParentKey,
97 strKey.data(),
98 0,
99 samDesired,
100 &m_hKey
101 );
102 if (lError != ERROR_SUCCESS)
103 {
104 throw(new IOException(strKey, WINAPI_ERROR(lError)));
105 }
106
107 m_strKey = strKey;
108 m_samDesired = samDesired;
109}
110
111void Registry::close()
113{
114 __DCL_ASSERT(opened());
115
116 LONG lError = ::RegCloseKey(m_hKey);
117
118 m_hKey = NULL;
119
120 if (lError != ERROR_SUCCESS)
121 {
122 throw(new IOException(m_strKey, WINAPI_ERROR(lError)));
123 }
124}
125
126LONG Registry::getDataInfo(const String& strName, REGISTRY_DATAINFO& info)
127{
128 __DCL_ASSERT(opened());
129 __DCL_ASSERT(!strName.isEmpty());
130
131 info.dwDataType = REG_NONE;
132 info.dwDataSize = 0;
133
134 return ::RegQueryValueExW(
135 m_hKey,
136 strName.data(),
137 NULL,
138 &info.dwDataType,
139 NULL,
140 &info.dwDataSize
141 );
142}
143
144bool Registry::existsValue(const String& strName)
145{
147 return getDataInfo(strName, info) == ERROR_SUCCESS;
148}
149
150String Registry::getString(const String& strName)
152{
153 __DCL_ASSERT(opened());
154 __DCL_ASSERT(!strName.isEmpty());
155
156 String r;
157
159 LONG lError = getDataInfo(strName, info);
160 if (lError != ERROR_SUCCESS)
161 {
162 throw(new IOException(strName, WINAPI_ERROR(lError)));
163 }
164
165 if (!((info.dwDataType == REG_EXPAND_SZ || info.dwDataType == REG_SZ)))
166 {
167 throw(new IOException(strName, WINAPI_ERROR(ERROR_INVALID_DATA)));
168 }
169
170 if (info.dwDataSize > 0)
171 {
172 DWORD dwDataSize = info.dwDataSize;
173 CharBuffer* buf = CharBuffer::create(dwDataSize);
174 lError = ::RegQueryValueExW(
175 m_hKey,
176 strName,
177 NULL,
178 NULL,
179 (byte_t*)buf->data(),
180 &dwDataSize
181 );
182
183 if (lError != ERROR_SUCCESS)
184 {
185 buf->release();
186 throw(new IOException(strName, WINAPI_ERROR(lError)));
187 }
188
189 buf->__dataLength = dwDataSize;
190 buf->data()[buf->__dataLength] = L'\0';
191 r = buf;
192 buf->release();
193 }
194
195 return r;
196}
197
198int Registry::getInteger(const String& strName)
200{
201 __DCL_ASSERT(opened());
202 __DCL_ASSERT(!strName.isEmpty());
203
205 LONG lError = getDataInfo(strName, info);
206 if (lError != ERROR_SUCCESS)
207 {
208 throw(new IOException(strName, WINAPI_ERROR(lError)));
209 }
210
211 if (info.dwDataType != REG_DWORD)
212 {
213 throw(new IOException(strName, WINAPI_ERROR(ERROR_INVALID_DATA)));
214 }
215
216 DWORD dwData;
217 DWORD dwDataSize = sizeof(dwData);
218
219 lError = ::RegQueryValueExW(
220 m_hKey,
221 strName.data(),
222 NULL,
223 NULL,
224 (byte_t*)&dwData,
225 &dwDataSize
226 );
227
228 if (lError != ERROR_SUCCESS)
229 {
230 throw(new IOException(strName, WINAPI_ERROR(lError)));
231 }
232
233 return (int)dwData;
234}
235
236void Registry::setValue(const String& strName, const String& strValue)
238{
239 __DCL_ASSERT(opened());
240 __DCL_ASSERT(!strName.isEmpty());
241 __DCL_ASSERT(strValue.length() < UINT_MAX);
242 DWORD len = (DWORD)strValue.length();
243
244 LONG lError = ::RegSetValueExW(
245 m_hKey,
246 strName.data(),
247 0,
248 REG_SZ,
249 (const byte_t*)strValue.data(),
250 len + 1 // include NULL
251 );
252
253 if (lError != ERROR_SUCCESS)
254 {
255 throw(new IOException(strName, WINAPI_ERROR(lError)));
256 }
257}
258
259void Registry::setValue(const String& strName, int nValue)
261{
262 __DCL_ASSERT(opened());
263 __DCL_ASSERT(!strName.isEmpty());
264 __DCL_ASSERT(sizeof(DWORD) >= sizeof(int));
265
266 DWORD dwData = (DWORD)nValue;
267 LONG lError = ::RegSetValueExW(
268 m_hKey,
269 strName.data(),
270 0,
271 REG_DWORD,
272 (byte_t*)&dwData,
273 sizeof(DWORD)
274 );
275
276 if (lError != ERROR_SUCCESS)
277 {
278 throw(new IOException(strName, WINAPI_ERROR(lError)));
279 }
280}
281
282bool Registry::existsKey(const String& strKeyName, HKEY hParentKey)
283{
284 HKEY hCurrent = NULL;
285 LONG lError = ::RegOpenKeyExW(
286 hParentKey,
287 strKeyName.data(),
288 0,
289 KEY_READ,
290 &hCurrent
291 );
292 if (lError == ERROR_SUCCESS)
293 {
294 ::RegCloseKey(hCurrent);
295 return true;
296 }
297 return false;
298}
299
300__DCL_END_NAMESPACE
301
302#endif // __DCL_WINDOWS
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:340
wchar_t char_t
Definition Config.h:275
unsigned char byte_t
Definition Config.h:274
#define __DCL_THROWS1(e)
Definition Config.h:167
#define WINAPI_ERROR(n)
Definition Exception.h:72
#define __DCL_ASSERT(expr)
Definition Object.h:371
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
Definition Object.h:228
#define __T(str)
Definition Object.h:44
__DCL_BEGIN_NAMESPACE struct _REGISTRY_DATAINFO REGISTRY_DATAINFO
ByteString r
size_t len
ByteBuffer * buf
HKEY m_hParentKey
Definition Registry.h:80
String m_strKey
Definition Registry.h:83
LONG getDataInfo(const String &strName, REGISTRY_DATAINFO &info)
HKEY m_hKey
Definition Registry.h:81