view src/win32/Reg.cpp @ 8:08a8e09ca414

add files required by automake
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:54:39 -0600
parents f9f4f1b99eed
children
line wrap: on
line source
1 #include "stdafx.h"
2 #include "Reg.h"
4 static char buffer[2048];
5 static HKEY vbKey = NULL;
6 static CString regVbaPath;
8 #define VBA_PREF "preferences"
10 bool regEnabled = true;
12 void regInit(const char *path)
13 {
14 if (regEnabled)
15 {
16 DWORD disp = 0;
17 LONG res = RegCreateKeyEx(HKEY_CURRENT_USER,
18 "Software\\Emulators\\VisualBoyAdvance",
19 0,
20 "",
21 REG_OPTION_NON_VOLATILE,
22 KEY_ALL_ACCESS,
23 NULL,
24 &vbKey,
25 &disp);
26 }
27 regVbaPath.Format("%s\\vba.ini", path);
28 }
30 void regShutdown()
31 {
32 LONG res = RegCloseKey(vbKey);
33 }
35 const char *regGetINIPath()
36 {
37 return regVbaPath;
38 }
40 const char *regQueryStringValue(const char *key, const char *def)
41 {
42 if (regEnabled)
43 {
44 DWORD type = 0;
45 DWORD size = 2048;
47 LONG res = RegQueryValueEx(vbKey,
48 key,
49 NULL,
50 &type,
51 (UCHAR *)buffer,
52 &size);
54 if (res == ERROR_SUCCESS && type == REG_SZ)
55 return buffer;
57 return def;
58 }
60 DWORD res = GetPrivateProfileString(VBA_PREF,
61 key,
62 def,
63 (LPTSTR)buffer,
64 2048,
65 regVbaPath);
67 if (res)
68 return buffer;
70 return def;
71 }
73 DWORD regQueryDwordValue(const char *key, DWORD def, bool force)
74 {
75 if (regEnabled || force)
76 {
77 DWORD type = 0;
78 DWORD size = sizeof(DWORD);
79 DWORD result = 0;
81 LONG res = RegQueryValueEx(vbKey,
82 key,
83 NULL,
84 &type,
85 (UCHAR *)&result,
86 &size);
88 if (res == ERROR_SUCCESS && type == REG_DWORD)
89 return result;
91 return def;
92 }
94 return GetPrivateProfileInt(VBA_PREF,
95 key,
96 def,
97 regVbaPath);
98 }
100 BOOL regQueryBinaryValue(const char *key, char *value, int count)
101 {
102 if (regEnabled)
103 {
104 DWORD type = 0;
105 DWORD size = count;
106 DWORD result = 0;
108 LONG res = RegQueryValueEx(vbKey,
109 key,
110 NULL,
111 &type,
112 (UCHAR *)value,
113 &size);
115 if (res == ERROR_SUCCESS && type == REG_BINARY)
116 return TRUE;
118 return FALSE;
119 }
120 CString k = key;
121 k += "Count";
122 int size = GetPrivateProfileInt(VBA_PREF,
123 k,
124 -1,
125 regVbaPath);
126 if (size >= 0 && size < count)
127 count = size;
128 return GetPrivateProfileStruct(VBA_PREF,
129 key,
130 value,
131 count,
132 regVbaPath);
133 }
135 void regSetStringValue(const char *key, const char *value)
136 {
137 if (regEnabled)
138 {
139 LONG res = RegSetValueEx(vbKey,
140 key,
141 NULL,
142 REG_SZ,
143 (const UCHAR *)value,
144 strlen(value)+1);
145 }
146 else
147 {
148 WritePrivateProfileString(VBA_PREF,
149 key,
150 value,
151 regVbaPath);
152 }
153 }
155 void regSetDwordValue(const char *key, DWORD value, bool force)
156 {
157 if (regEnabled || force)
158 {
159 LONG res = RegSetValueEx(vbKey,
160 key,
161 NULL,
162 REG_DWORD,
163 (const UCHAR *)&value,
164 sizeof(DWORD));
165 }
166 else
167 {
168 wsprintf(buffer, "%u", value);
169 WritePrivateProfileString(VBA_PREF,
170 key,
171 buffer,
172 regVbaPath);
173 }
174 }
176 void regSetBinaryValue(const char *key, char *value, int count)
177 {
178 if (regEnabled)
179 {
180 LONG res = RegSetValueEx(vbKey,
181 key,
182 NULL,
183 REG_BINARY,
184 (const UCHAR *)value,
185 count);
186 }
187 else
188 {
189 CString k = key;
190 k += "Count";
191 wsprintf(buffer, "%u", count);
193 WritePrivateProfileString(VBA_PREF,
194 k,
195 buffer,
196 regVbaPath);
198 WritePrivateProfileStruct(VBA_PREF,
199 key,
200 value,
201 count,
202 regVbaPath);
203 }
204 }
206 void regDeleteValue(const char *key)
207 {
208 if (regEnabled)
209 {
210 LONG res = RegDeleteValue(vbKey,
211 key);
212 }
213 else
214 {
215 WritePrivateProfileString(VBA_PREF,
216 key,
217 NULL,
218 regVbaPath);
219 }
220 }
222 bool regCreateFileType(const char *ext, const char *type)
223 {
224 DWORD disp = 0;
225 HKEY key;
226 LONG res = RegCreateKeyEx(HKEY_CLASSES_ROOT,
227 ext,
228 0,
229 "",
230 REG_OPTION_NON_VOLATILE,
231 KEY_ALL_ACCESS,
232 NULL,
233 &key,
234 &disp);
235 if (res == ERROR_SUCCESS)
236 {
237 res = RegSetValueEx(key,
238 "",
239 0,
240 REG_SZ,
241 (const UCHAR *)type,
242 strlen(type)+1);
243 RegCloseKey(key);
244 return true;
245 }
246 return false;
247 }
249 bool regAssociateType(const char *type, const char *desc, const char *application)
250 {
251 DWORD disp = 0;
252 HKEY key;
253 LONG res = RegCreateKeyEx(HKEY_CLASSES_ROOT,
254 type,
255 0,
256 "",
257 REG_OPTION_NON_VOLATILE,
258 KEY_ALL_ACCESS,
259 NULL,
260 &key,
261 &disp);
262 if (res == ERROR_SUCCESS)
263 {
264 res = RegSetValueEx(key,
265 "",
266 0,
267 REG_SZ,
268 (const UCHAR *)desc,
269 strlen(desc)+1);
270 HKEY key2;
271 res = RegCreateKeyEx(key,
272 "Shell\\Open\\Command",
273 0,
274 "",
275 REG_OPTION_NON_VOLATILE,
276 KEY_ALL_ACCESS,
277 NULL,
278 &key2,
279 &disp);
280 if (res == ERROR_SUCCESS)
281 {
282 res = RegSetValueEx(key2,
283 "",
284 0,
285 REG_SZ,
286 (const UCHAR *)application,
287 strlen(application)+1);
288 RegCloseKey(key2);
289 RegCloseKey(key);
290 return true;
291 }
293 RegCloseKey(key);
294 }
295 return false;
296 }
298 static void regExportSettingsToINI(HKEY key, const char *section)
299 {
300 char valueName[256];
301 int index = 0;
302 while (1)
303 {
304 DWORD nameSize = 256;
305 DWORD size = 2048;
306 DWORD type;
307 LONG res = RegEnumValue(key,
308 index,
309 valueName,
310 &nameSize,
311 NULL,
312 &type,
313 (LPBYTE)buffer,
314 &size);
316 if (res == ERROR_SUCCESS)
317 {
318 switch (type)
319 {
320 case REG_DWORD:
321 {
322 char temp[256];
323 wsprintf(temp, "%u", *((DWORD *)buffer));
324 WritePrivateProfileString(section,
325 valueName,
326 temp,
327 regVbaPath);
328 break;
329 }
330 case REG_SZ:
331 WritePrivateProfileString(section,
332 valueName,
333 buffer,
334 regVbaPath);
335 break;
336 case REG_BINARY:
337 {
338 char temp[256];
340 wsprintf(temp, "%u", size);
341 CString k = valueName;
342 k += "Count";
343 WritePrivateProfileString(section,
344 k,
345 temp,
346 regVbaPath);
347 WritePrivateProfileStruct(section,
348 valueName,
349 buffer,
350 size,
351 regVbaPath);
352 break;
353 }
354 }
355 index++;
356 }
357 else
358 break;
359 }
360 }
362 void regExportSettingsToINI()
363 {
364 if (vbKey != NULL)
365 {
366 regExportSettingsToINI(vbKey, VBA_PREF);
367 }
369 HKEY key;
371 if (RegOpenKey(HKEY_CURRENT_USER,
372 "Software\\Emulators\\VisualBoyAdvance\\Viewer", &key) ==
373 ERROR_SUCCESS)
374 {
375 regExportSettingsToINI(key, "Viewer");
376 RegCloseKey(key);
377 }
378 }