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