1 | #include "registry.h"
|
---|
2 | #include <stdlib.h>
|
---|
3 |
|
---|
4 | bool registry_createkey(HKEY key, const char *name, PHKEY keyout)
|
---|
5 | { LONG reply;
|
---|
6 | DWORD disposition;
|
---|
7 |
|
---|
8 | reply = RegCreateKeyEx( key, name, NULL, NULL,
|
---|
9 | REG_OPTION_NON_VOLATILE,
|
---|
10 | KEY_ALL_ACCESS, NULL,
|
---|
11 | keyout, &disposition);
|
---|
12 | if (reply != ERROR_SUCCESS)
|
---|
13 | { return FALSE;
|
---|
14 | }
|
---|
15 | return TRUE;
|
---|
16 | }
|
---|
17 |
|
---|
18 | bool registry_openkey(HKEY key, const char *name, PHKEY keyout)
|
---|
19 | { LONG reply;
|
---|
20 |
|
---|
21 | reply = RegOpenKeyEx(key, name, NULL, KEY_QUERY_VALUE, keyout);
|
---|
22 | if (reply != ERROR_SUCCESS)
|
---|
23 | { return FALSE;
|
---|
24 | }
|
---|
25 | return TRUE;
|
---|
26 | }
|
---|
27 |
|
---|
28 | bool registry_closekey(HKEY key)
|
---|
29 | { LONG reply;
|
---|
30 | reply = RegCloseKey(key);
|
---|
31 | if (reply != ERROR_SUCCESS)
|
---|
32 | { return FALSE;
|
---|
33 | }
|
---|
34 | return TRUE;
|
---|
35 | }
|
---|
36 |
|
---|
37 | bool registry_keyExists(HKEY key, const char *name)
|
---|
38 | { HKEY local;
|
---|
39 |
|
---|
40 | if (!registry_openkey(key, name, &local))
|
---|
41 | { return false;
|
---|
42 | }
|
---|
43 | registry_closekey(local);
|
---|
44 | return true;
|
---|
45 | }
|
---|
46 |
|
---|
47 | bool registry_ensureKeyExists(HKEY key, const char *name)
|
---|
48 | { HKEY local;
|
---|
49 |
|
---|
50 | if (!registry_openkey(key, name, &local))
|
---|
51 | { if (!registry_createkey(key, name, &local))
|
---|
52 | { return false;
|
---|
53 | }
|
---|
54 | }
|
---|
55 | registry_closekey(local);
|
---|
56 | return true;
|
---|
57 | }
|
---|
58 |
|
---|
59 | bool registry_setstring(HKEY key, const char *name, const char *value)
|
---|
60 | { LONG reply;
|
---|
61 |
|
---|
62 | reply = RegSetValueEx(key, (LPCTSTR) name, NULL, REG_SZ, (CONST BYTE *) value, strlen(value) + 1);
|
---|
63 | if (reply != ERROR_SUCCESS)
|
---|
64 | { return FALSE;
|
---|
65 | }
|
---|
66 | return TRUE;
|
---|
67 | }
|
---|
68 |
|
---|
69 | bool registry_setlong(HKEY key, char *name, LONG value)
|
---|
70 | { LONG reply;
|
---|
71 | DWORD word;
|
---|
72 |
|
---|
73 | word = (DWORD) value;
|
---|
74 | reply = RegSetValueEx(key, (LPCTSTR) name, NULL, REG_DWORD, (CONST BYTE *) &word, sizeof(DWORD));
|
---|
75 | if (reply != ERROR_SUCCESS)
|
---|
76 | { return FALSE;
|
---|
77 | }
|
---|
78 | return TRUE;
|
---|
79 | }
|
---|
80 |
|
---|
81 | bool registry_setblock(HKEY key, char *name, char *data, int size)
|
---|
82 | { LONG reply;
|
---|
83 |
|
---|
84 | reply = RegSetValueEx(key, name, NULL, REG_BINARY, (CONST BYTE *) data, size);
|
---|
85 | if (reply != ERROR_SUCCESS)
|
---|
86 | { // display_error();
|
---|
87 | return FALSE;
|
---|
88 | }
|
---|
89 | return TRUE;
|
---|
90 | }
|
---|
91 |
|
---|
92 | bool registry_setint(HKEY key, char *name, int value)
|
---|
93 | { return registry_setlong(key, name, (LONG) value);
|
---|
94 | }
|
---|
95 |
|
---|
96 | bool registry_getstring(HKEY key, char *name, char **string)
|
---|
97 | { LONG reply;
|
---|
98 | DWORD type;
|
---|
99 | DWORD size;
|
---|
100 | BYTE buffer[128];
|
---|
101 | int tries = 0;
|
---|
102 |
|
---|
103 | do
|
---|
104 | {
|
---|
105 | reply = RegQueryValueEx(key, name, NULL, &type, buffer, &size);
|
---|
106 | if (reply != ERROR_SUCCESS)
|
---|
107 | { //display_error();
|
---|
108 | //sprintf(buffer, "%x {%s}", key, name);
|
---|
109 | //MessageBox(0, buffer, name, MB_OK);
|
---|
110 | }
|
---|
111 | tries ++;
|
---|
112 | } while (reply != ERROR_SUCCESS && tries < 3);
|
---|
113 |
|
---|
114 | if (reply != ERROR_SUCCESS)
|
---|
115 | { return FALSE;
|
---|
116 | }
|
---|
117 |
|
---|
118 | *string = (char *) malloc(size);
|
---|
119 | if (*string == NULL)
|
---|
120 | { return FALSE;
|
---|
121 | }
|
---|
122 | strcpy(*string, (char *) buffer);
|
---|
123 | return TRUE;
|
---|
124 | }
|
---|
125 |
|
---|
126 | bool registry_getint(HKEY key, char *name, int *out)
|
---|
127 | { DWORD type;
|
---|
128 | DWORD size;
|
---|
129 | LONG reply;
|
---|
130 |
|
---|
131 | reply = RegQueryValueEx(key, (LPTSTR) name, NULL, &type, (LPBYTE) out, &size);
|
---|
132 | if (reply != ERROR_SUCCESS)
|
---|
133 | { return FALSE;
|
---|
134 | }
|
---|
135 | return TRUE;
|
---|
136 | }
|
---|
137 |
|
---|
138 | bool registry_getlong(HKEY key, char *name, LONG *out)
|
---|
139 | { DWORD type;
|
---|
140 | DWORD size;
|
---|
141 | LONG reply;
|
---|
142 | DWORD got;
|
---|
143 |
|
---|
144 | size = sizeof(DWORD);
|
---|
145 |
|
---|
146 | reply = RegQueryValueEx(key, name, NULL, &type, (LPBYTE) &got, &size);
|
---|
147 | if (reply != ERROR_SUCCESS)
|
---|
148 | { return FALSE;
|
---|
149 | }
|
---|
150 | *out = (LONG) got;
|
---|
151 | return TRUE;
|
---|
152 | }
|
---|
153 |
|
---|
154 | int registry_getblock(HKEY key, char *name, char *data, int size)
|
---|
155 | { DWORD type;
|
---|
156 | DWORD lsize;
|
---|
157 | LONG reply;
|
---|
158 |
|
---|
159 | lsize = size;
|
---|
160 |
|
---|
161 | reply = RegQueryValueEx(key, name, NULL, &type, (LPBYTE) data, &lsize);
|
---|
162 | if (reply != ERROR_SUCCESS)
|
---|
163 | { return -1;
|
---|
164 | }
|
---|
165 | return lsize;
|
---|
166 | }
|
---|
167 |
|
---|
168 | bool registry_fetchlong(char *keystr, LONG *lptr)
|
---|
169 | { HKEY key;
|
---|
170 |
|
---|
171 | if (!registry_openkey(HKEY_LOCAL_MACHINE, "Software\\Kudlian Soft\\ReTreeval", &key))
|
---|
172 | { return FALSE;
|
---|
173 | }
|
---|
174 | if (!registry_getlong(key, keystr, lptr))
|
---|
175 | { return FALSE;
|
---|
176 | }
|
---|
177 | if (registry_closekey(key) == FALSE)
|
---|
178 | { return FALSE;
|
---|
179 | }
|
---|
180 | return TRUE;
|
---|
181 | }
|
---|
182 |
|
---|
183 | bool registry_fetchblock(char *keystr, char *data, int size)
|
---|
184 | { HKEY key;
|
---|
185 | int read;
|
---|
186 |
|
---|
187 | if (!registry_openkey(HKEY_LOCAL_MACHINE, "Software\\Kudlian Soft\\ReTreeval", &key))
|
---|
188 | { return FALSE;
|
---|
189 | }
|
---|
190 | read = registry_getblock(key, keystr, data, size);
|
---|
191 | if (read < 0)
|
---|
192 | { return FALSE;
|
---|
193 | }
|
---|
194 | if (registry_closekey(key) == FALSE)
|
---|
195 | { return FALSE;
|
---|
196 | }
|
---|
197 | return TRUE;
|
---|
198 | }
|
---|
199 |
|
---|
200 | bool registry_storelong(char *keystr, LONG value)
|
---|
201 | { HKEY key;
|
---|
202 |
|
---|
203 | if (!registry_createkey(HKEY_LOCAL_MACHINE, "Software\\Kudlian Soft\\ReTreeval", &key))
|
---|
204 | { return FALSE;
|
---|
205 | }
|
---|
206 | if (!registry_setlong(key, keystr, value))
|
---|
207 | { return FALSE;
|
---|
208 | }
|
---|
209 | if (!registry_closekey(key))
|
---|
210 | { return FALSE;
|
---|
211 | }
|
---|
212 | return TRUE;
|
---|
213 | }
|
---|
214 |
|
---|
215 | bool registry_storeint(char *keystr, int value)
|
---|
216 | { HKEY key;
|
---|
217 |
|
---|
218 | if (!registry_createkey(HKEY_LOCAL_MACHINE, "Software\\Kudlian Soft\\ReTreeval", &key))
|
---|
219 | { return FALSE;
|
---|
220 | }
|
---|
221 | if (!registry_setint(key, keystr, value))
|
---|
222 | { return FALSE;
|
---|
223 | }
|
---|
224 | if (!registry_closekey(key))
|
---|
225 | { return FALSE;
|
---|
226 | }
|
---|
227 | return TRUE;
|
---|
228 | }
|
---|
229 |
|
---|
230 | bool registry_storeblock(char *keystr, char *data, int size)
|
---|
231 | { HKEY key;
|
---|
232 |
|
---|
233 | if (!registry_createkey(HKEY_LOCAL_MACHINE, "Software\\Kudlian Soft\\ReTreeval", &key))
|
---|
234 | { return FALSE;
|
---|
235 | }
|
---|
236 | if (!registry_setblock(key, keystr, data, size))
|
---|
237 | { return FALSE;
|
---|
238 | }
|
---|
239 | if (!registry_closekey(key))
|
---|
240 | { return FALSE;
|
---|
241 | }
|
---|
242 | return TRUE;
|
---|
243 | }
|
---|
244 |
|
---|
245 | bool registry_storestring(char *keystr, char *value)
|
---|
246 | { HKEY key;
|
---|
247 |
|
---|
248 | if (!registry_createkey(HKEY_LOCAL_MACHINE, "Software\\Kudlian Soft\\ReTreeval", &key))
|
---|
249 | { return FALSE;
|
---|
250 | }
|
---|
251 | if (!registry_setstring(key, keystr, value))
|
---|
252 | { return FALSE;
|
---|
253 | }
|
---|
254 | if (!registry_closekey(key))
|
---|
255 | { return FALSE;
|
---|
256 | }
|
---|
257 | return TRUE;
|
---|
258 | }
|
---|