source: trunk/gsinstaller/registry.cpp@ 2324

Last change on this file since 2324 was 2324, checked in by sjboddie, 23 years ago

Replaced some bool, true and false with BOOL, TRUE and FALSE because
VC++ is stupid.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1#include "registry.h"
2#include <stdlib.h>
3
4BOOL registry_createkey(HKEY key, const char *name, PHKEY keyout)
5{
6 LONG reply;
7 DWORD disposition;
8
9 reply = RegCreateKeyEx(key, name, 0, NULL,
10 REG_OPTION_NON_VOLATILE,
11 KEY_ALL_ACCESS, NULL,
12 keyout, &disposition);
13 if (reply != ERROR_SUCCESS)
14 {
15 return FALSE;
16 }
17 return TRUE;
18}
19
20BOOL registry_openkey(HKEY key, const char *name, PHKEY keyout)
21{
22 LONG reply;
23
24 reply = RegOpenKeyEx(key, name, 0, KEY_QUERY_VALUE | KEY_SET_VALUE, keyout);
25 if (reply != ERROR_SUCCESS)
26 {
27 return FALSE;
28 }
29 return TRUE;
30}
31
32BOOL registry_deletekey(HKEY key, const char *name)
33{
34 if (RegDeleteKey(key, name) != ERROR_SUCCESS)
35 {
36 return FALSE;
37 }
38 return TRUE;
39}
40
41BOOL registry_closekey(HKEY key)
42{
43 LONG reply;
44 reply = RegCloseKey(key);
45 if (reply != ERROR_SUCCESS)
46 {
47 return FALSE;
48 }
49 return TRUE;
50}
51
52BOOL registry_keyExists(HKEY key, const char *name)
53{
54 HKEY local;
55
56 if (!registry_openkey(key, name, &local))
57 {
58 return FALSE;
59 }
60 registry_closekey(local);
61 return TRUE;
62}
63
64BOOL registry_ensureKeyExists(HKEY key, const char *name)
65{
66 HKEY local;
67
68 if (!registry_openkey(key, name, &local))
69 {
70 if (!registry_createkey(key, name, &local))
71 {
72 return FALSE;
73 }
74 }
75 registry_closekey(local);
76 return TRUE;
77}
78
79BOOL registry_deleteitem(HKEY key, const char *name)
80{
81 if (RegDeleteValue(key, name) != ERROR_SUCCESS)
82 {
83 return FALSE;
84 }
85 return TRUE;
86}
87
88BOOL registry_setstring(HKEY key, const char *name, const char *value)
89{
90 LONG reply;
91
92 reply = RegSetValueEx(key, (LPCTSTR) name, 0, REG_SZ, (CONST BYTE *) value, strlen(value) + 1);
93 if (reply != ERROR_SUCCESS)
94 {
95 return FALSE;
96 }
97 return TRUE;
98}
99
100BOOL registry_setlong(HKEY key, char *name, LONG value)
101{
102 LONG reply;
103 DWORD word;
104
105 word = (DWORD) value;
106 reply = RegSetValueEx(key, (LPCTSTR) name, 0, REG_DWORD, (CONST BYTE *) &word, sizeof(DWORD));
107 if (reply != ERROR_SUCCESS)
108 {
109 return FALSE;
110 }
111 return TRUE;
112}
113
114BOOL registry_setblock(HKEY key, char *name, char *data, int size)
115{
116 LONG reply;
117
118 reply = RegSetValueEx(key, name, 0, REG_BINARY, (CONST BYTE *) data, size);
119 if (reply != ERROR_SUCCESS)
120 {
121 // display_error();
122 return FALSE;
123 }
124 return TRUE;
125}
126
127BOOL registry_setint(HKEY key, char *name, int value)
128{
129 return registry_setlong(key, name, (LONG) value);
130}
131
132BOOL registry_getstring(HKEY key, char *name, char **string)
133{
134 LONG reply;
135 DWORD type;
136 DWORD size;
137 BYTE buffer[128];
138 int tries = 0;
139
140 do
141 {
142 reply = RegQueryValueEx(key, name, NULL, &type, buffer, &size);
143 if (reply != ERROR_SUCCESS)
144 {
145 //display_error();
146 //sprintf(buffer, "%x {%s}", key, name);
147 //MessageBox(0, buffer, name, MB_OK);
148 }
149 tries ++;
150 } while (reply != ERROR_SUCCESS && tries < 3);
151
152 if (reply != ERROR_SUCCESS)
153 {
154 return FALSE;
155 }
156
157 *string = (char *) malloc(size);
158 if (*string == NULL)
159 {
160 return FALSE;
161 }
162 strcpy(*string, (char *) buffer);
163 return TRUE;
164}
165
166BOOL registry_getint(HKEY key, char *name, int *out)
167{
168 DWORD type;
169 DWORD size;
170 LONG reply;
171
172 reply = RegQueryValueEx(key, (LPTSTR) name, NULL, &type, (LPBYTE) out, &size);
173 if (reply != ERROR_SUCCESS)
174 {
175 return FALSE;
176 }
177 return TRUE;
178}
179
180BOOL registry_getlong(HKEY key, char *name, LONG *out)
181{
182 DWORD type;
183 DWORD size;
184 LONG reply;
185 DWORD got;
186
187 size = sizeof(DWORD);
188
189 reply = RegQueryValueEx(key, name, NULL, &type, (LPBYTE) &got, &size);
190 if (reply != ERROR_SUCCESS)
191 {
192 return FALSE;
193 }
194 *out = (LONG) got;
195 return TRUE;
196}
197
198int registry_getblock(HKEY key, char *name, char *data, int size)
199{
200 DWORD type;
201 DWORD lsize;
202 LONG reply;
203
204 lsize = size;
205
206 reply = RegQueryValueEx(key, name, NULL, &type, (LPBYTE) data, &lsize);
207 if (reply != ERROR_SUCCESS)
208 {
209 return -1;
210 }
211 return lsize;
212}
213
214BOOL registry_fetchlong(char *keystr, LONG *lptr)
215{
216 HKEY key;
217
218 if (!registry_openkey(HKEY_LOCAL_MACHINE, "Software\\Kudlian Soft\\ReTreeval", &key))
219 {
220 return FALSE;
221 }
222 if (!registry_getlong(key, keystr, lptr))
223 {
224 return FALSE;
225 }
226 if (registry_closekey(key) == FALSE)
227 {
228 return FALSE;
229 }
230 return TRUE;
231}
232
233BOOL registry_fetchblock(char *keystr, char *data, int size)
234{
235 HKEY key;
236 int read;
237
238 if (!registry_openkey(HKEY_LOCAL_MACHINE, "Software\\Kudlian Soft\\ReTreeval", &key))
239 {
240 return FALSE;
241 }
242 read = registry_getblock(key, keystr, data, size);
243 if (read < 0)
244 {
245 return FALSE;
246 }
247 if (registry_closekey(key) == FALSE)
248 {
249 return FALSE;
250 }
251 return TRUE;
252}
253
254BOOL registry_storelong(char *keystr, LONG value)
255{
256 HKEY key;
257
258 if (!registry_createkey(HKEY_LOCAL_MACHINE, "Software\\Kudlian Soft\\ReTreeval", &key))
259 {
260 return FALSE;
261 }
262 if (!registry_setlong(key, keystr, value))
263 {
264 return FALSE;
265 }
266 if (!registry_closekey(key))
267 {
268 return FALSE;
269 }
270 return TRUE;
271}
272
273BOOL registry_storeint(char *keystr, int value)
274{
275 HKEY key;
276
277 if (!registry_createkey(HKEY_LOCAL_MACHINE, "Software\\Kudlian Soft\\ReTreeval", &key))
278 {
279 return FALSE;
280 }
281 if (!registry_setint(key, keystr, value))
282 {
283 return FALSE;
284 }
285 if (!registry_closekey(key))
286 {
287 return FALSE;
288 }
289 return TRUE;
290}
291
292BOOL registry_storeblock(char *keystr, char *data, int size)
293{
294 HKEY key;
295
296 if (!registry_createkey(HKEY_LOCAL_MACHINE, "Software\\Kudlian Soft\\ReTreeval", &key))
297 {
298 return FALSE;
299 }
300 if (!registry_setblock(key, keystr, data, size))
301 {
302 return FALSE;
303 }
304 if (!registry_closekey(key))
305 {
306 return FALSE;
307 }
308 return TRUE;
309}
310
311BOOL registry_storestring(char *keystr, char *value)
312{
313 HKEY key;
314
315 if (!registry_createkey(HKEY_LOCAL_MACHINE, "Software\\Kudlian Soft\\ReTreeval", &key))
316 {
317 return FALSE;
318 }
319 if (!registry_setstring(key, keystr, value))
320 {
321 return FALSE;
322 }
323 if (!registry_closekey(key))
324 {
325 return FALSE;
326 }
327 return TRUE;
328}
Note: See TracBrowser for help on using the repository browser.