rlm@1: #include "stdafx.h" rlm@1: #include "Reg.h" rlm@1: rlm@1: static char buffer[2048]; rlm@1: static HKEY vbKey = NULL; rlm@1: static CString regVbaPath; rlm@1: rlm@1: #define VBA_PREF "preferences" rlm@1: rlm@1: bool regEnabled = true; rlm@1: rlm@1: void regInit(const char *path) rlm@1: { rlm@1: if (regEnabled) rlm@1: { rlm@1: DWORD disp = 0; rlm@1: LONG res = RegCreateKeyEx(HKEY_CURRENT_USER, rlm@1: "Software\\Emulators\\VisualBoyAdvance", rlm@1: 0, rlm@1: "", rlm@1: REG_OPTION_NON_VOLATILE, rlm@1: KEY_ALL_ACCESS, rlm@1: NULL, rlm@1: &vbKey, rlm@1: &disp); rlm@1: } rlm@1: regVbaPath.Format("%s\\vba.ini", path); rlm@1: } rlm@1: rlm@1: void regShutdown() rlm@1: { rlm@1: LONG res = RegCloseKey(vbKey); rlm@1: } rlm@1: rlm@1: const char *regGetINIPath() rlm@1: { rlm@1: return regVbaPath; rlm@1: } rlm@1: rlm@1: const char *regQueryStringValue(const char *key, const char *def) rlm@1: { rlm@1: if (regEnabled) rlm@1: { rlm@1: DWORD type = 0; rlm@1: DWORD size = 2048; rlm@1: rlm@1: LONG res = RegQueryValueEx(vbKey, rlm@1: key, rlm@1: NULL, rlm@1: &type, rlm@1: (UCHAR *)buffer, rlm@1: &size); rlm@1: rlm@1: if (res == ERROR_SUCCESS && type == REG_SZ) rlm@1: return buffer; rlm@1: rlm@1: return def; rlm@1: } rlm@1: rlm@1: DWORD res = GetPrivateProfileString(VBA_PREF, rlm@1: key, rlm@1: def, rlm@1: (LPTSTR)buffer, rlm@1: 2048, rlm@1: regVbaPath); rlm@1: rlm@1: if (res) rlm@1: return buffer; rlm@1: rlm@1: return def; rlm@1: } rlm@1: rlm@1: DWORD regQueryDwordValue(const char *key, DWORD def, bool force) rlm@1: { rlm@1: if (regEnabled || force) rlm@1: { rlm@1: DWORD type = 0; rlm@1: DWORD size = sizeof(DWORD); rlm@1: DWORD result = 0; rlm@1: rlm@1: LONG res = RegQueryValueEx(vbKey, rlm@1: key, rlm@1: NULL, rlm@1: &type, rlm@1: (UCHAR *)&result, rlm@1: &size); rlm@1: rlm@1: if (res == ERROR_SUCCESS && type == REG_DWORD) rlm@1: return result; rlm@1: rlm@1: return def; rlm@1: } rlm@1: rlm@1: return GetPrivateProfileInt(VBA_PREF, rlm@1: key, rlm@1: def, rlm@1: regVbaPath); rlm@1: } rlm@1: rlm@1: BOOL regQueryBinaryValue(const char *key, char *value, int count) rlm@1: { rlm@1: if (regEnabled) rlm@1: { rlm@1: DWORD type = 0; rlm@1: DWORD size = count; rlm@1: DWORD result = 0; rlm@1: rlm@1: LONG res = RegQueryValueEx(vbKey, rlm@1: key, rlm@1: NULL, rlm@1: &type, rlm@1: (UCHAR *)value, rlm@1: &size); rlm@1: rlm@1: if (res == ERROR_SUCCESS && type == REG_BINARY) rlm@1: return TRUE; rlm@1: rlm@1: return FALSE; rlm@1: } rlm@1: CString k = key; rlm@1: k += "Count"; rlm@1: int size = GetPrivateProfileInt(VBA_PREF, rlm@1: k, rlm@1: -1, rlm@1: regVbaPath); rlm@1: if (size >= 0 && size < count) rlm@1: count = size; rlm@1: return GetPrivateProfileStruct(VBA_PREF, rlm@1: key, rlm@1: value, rlm@1: count, rlm@1: regVbaPath); rlm@1: } rlm@1: rlm@1: void regSetStringValue(const char *key, const char *value) rlm@1: { rlm@1: if (regEnabled) rlm@1: { rlm@1: LONG res = RegSetValueEx(vbKey, rlm@1: key, rlm@1: NULL, rlm@1: REG_SZ, rlm@1: (const UCHAR *)value, rlm@1: strlen(value)+1); rlm@1: } rlm@1: else rlm@1: { rlm@1: WritePrivateProfileString(VBA_PREF, rlm@1: key, rlm@1: value, rlm@1: regVbaPath); rlm@1: } rlm@1: } rlm@1: rlm@1: void regSetDwordValue(const char *key, DWORD value, bool force) rlm@1: { rlm@1: if (regEnabled || force) rlm@1: { rlm@1: LONG res = RegSetValueEx(vbKey, rlm@1: key, rlm@1: NULL, rlm@1: REG_DWORD, rlm@1: (const UCHAR *)&value, rlm@1: sizeof(DWORD)); rlm@1: } rlm@1: else rlm@1: { rlm@1: wsprintf(buffer, "%u", value); rlm@1: WritePrivateProfileString(VBA_PREF, rlm@1: key, rlm@1: buffer, rlm@1: regVbaPath); rlm@1: } rlm@1: } rlm@1: rlm@1: void regSetBinaryValue(const char *key, char *value, int count) rlm@1: { rlm@1: if (regEnabled) rlm@1: { rlm@1: LONG res = RegSetValueEx(vbKey, rlm@1: key, rlm@1: NULL, rlm@1: REG_BINARY, rlm@1: (const UCHAR *)value, rlm@1: count); rlm@1: } rlm@1: else rlm@1: { rlm@1: CString k = key; rlm@1: k += "Count"; rlm@1: wsprintf(buffer, "%u", count); rlm@1: rlm@1: WritePrivateProfileString(VBA_PREF, rlm@1: k, rlm@1: buffer, rlm@1: regVbaPath); rlm@1: rlm@1: WritePrivateProfileStruct(VBA_PREF, rlm@1: key, rlm@1: value, rlm@1: count, rlm@1: regVbaPath); rlm@1: } rlm@1: } rlm@1: rlm@1: void regDeleteValue(const char *key) rlm@1: { rlm@1: if (regEnabled) rlm@1: { rlm@1: LONG res = RegDeleteValue(vbKey, rlm@1: key); rlm@1: } rlm@1: else rlm@1: { rlm@1: WritePrivateProfileString(VBA_PREF, rlm@1: key, rlm@1: NULL, rlm@1: regVbaPath); rlm@1: } rlm@1: } rlm@1: rlm@1: bool regCreateFileType(const char *ext, const char *type) rlm@1: { rlm@1: DWORD disp = 0; rlm@1: HKEY key; rlm@1: LONG res = RegCreateKeyEx(HKEY_CLASSES_ROOT, rlm@1: ext, rlm@1: 0, rlm@1: "", rlm@1: REG_OPTION_NON_VOLATILE, rlm@1: KEY_ALL_ACCESS, rlm@1: NULL, rlm@1: &key, rlm@1: &disp); rlm@1: if (res == ERROR_SUCCESS) rlm@1: { rlm@1: res = RegSetValueEx(key, rlm@1: "", rlm@1: 0, rlm@1: REG_SZ, rlm@1: (const UCHAR *)type, rlm@1: strlen(type)+1); rlm@1: RegCloseKey(key); rlm@1: return true; rlm@1: } rlm@1: return false; rlm@1: } rlm@1: rlm@1: bool regAssociateType(const char *type, const char *desc, const char *application) rlm@1: { rlm@1: DWORD disp = 0; rlm@1: HKEY key; rlm@1: LONG res = RegCreateKeyEx(HKEY_CLASSES_ROOT, rlm@1: type, rlm@1: 0, rlm@1: "", rlm@1: REG_OPTION_NON_VOLATILE, rlm@1: KEY_ALL_ACCESS, rlm@1: NULL, rlm@1: &key, rlm@1: &disp); rlm@1: if (res == ERROR_SUCCESS) rlm@1: { rlm@1: res = RegSetValueEx(key, rlm@1: "", rlm@1: 0, rlm@1: REG_SZ, rlm@1: (const UCHAR *)desc, rlm@1: strlen(desc)+1); rlm@1: HKEY key2; rlm@1: res = RegCreateKeyEx(key, rlm@1: "Shell\\Open\\Command", rlm@1: 0, rlm@1: "", rlm@1: REG_OPTION_NON_VOLATILE, rlm@1: KEY_ALL_ACCESS, rlm@1: NULL, rlm@1: &key2, rlm@1: &disp); rlm@1: if (res == ERROR_SUCCESS) rlm@1: { rlm@1: res = RegSetValueEx(key2, rlm@1: "", rlm@1: 0, rlm@1: REG_SZ, rlm@1: (const UCHAR *)application, rlm@1: strlen(application)+1); rlm@1: RegCloseKey(key2); rlm@1: RegCloseKey(key); rlm@1: return true; rlm@1: } rlm@1: rlm@1: RegCloseKey(key); rlm@1: } rlm@1: return false; rlm@1: } rlm@1: rlm@1: static void regExportSettingsToINI(HKEY key, const char *section) rlm@1: { rlm@1: char valueName[256]; rlm@1: int index = 0; rlm@1: while (1) rlm@1: { rlm@1: DWORD nameSize = 256; rlm@1: DWORD size = 2048; rlm@1: DWORD type; rlm@1: LONG res = RegEnumValue(key, rlm@1: index, rlm@1: valueName, rlm@1: &nameSize, rlm@1: NULL, rlm@1: &type, rlm@1: (LPBYTE)buffer, rlm@1: &size); rlm@1: rlm@1: if (res == ERROR_SUCCESS) rlm@1: { rlm@1: switch (type) rlm@1: { rlm@1: case REG_DWORD: rlm@1: { rlm@1: char temp[256]; rlm@1: wsprintf(temp, "%u", *((DWORD *)buffer)); rlm@1: WritePrivateProfileString(section, rlm@1: valueName, rlm@1: temp, rlm@1: regVbaPath); rlm@1: break; rlm@1: } rlm@1: case REG_SZ: rlm@1: WritePrivateProfileString(section, rlm@1: valueName, rlm@1: buffer, rlm@1: regVbaPath); rlm@1: break; rlm@1: case REG_BINARY: rlm@1: { rlm@1: char temp[256]; rlm@1: rlm@1: wsprintf(temp, "%u", size); rlm@1: CString k = valueName; rlm@1: k += "Count"; rlm@1: WritePrivateProfileString(section, rlm@1: k, rlm@1: temp, rlm@1: regVbaPath); rlm@1: WritePrivateProfileStruct(section, rlm@1: valueName, rlm@1: buffer, rlm@1: size, rlm@1: regVbaPath); rlm@1: break; rlm@1: } rlm@1: } rlm@1: index++; rlm@1: } rlm@1: else rlm@1: break; rlm@1: } rlm@1: } rlm@1: rlm@1: void regExportSettingsToINI() rlm@1: { rlm@1: if (vbKey != NULL) rlm@1: { rlm@1: regExportSettingsToINI(vbKey, VBA_PREF); rlm@1: } rlm@1: rlm@1: HKEY key; rlm@1: rlm@1: if (RegOpenKey(HKEY_CURRENT_USER, rlm@1: "Software\\Emulators\\VisualBoyAdvance\\Viewer", &key) == rlm@1: ERROR_SUCCESS) rlm@1: { rlm@1: regExportSettingsToINI(key, "Viewer"); rlm@1: RegCloseKey(key); rlm@1: } rlm@1: } rlm@1: