1 #include "stdafx.h" 2 3 // Returns key for HKEY_CURRENT_USER\"Software"\Company\AppName 4 // creating it if it doesn't exist 5 // responsibility of the caller to call RegCloseKey() on the returned HKEY 6 // 7 HKEY GetAppKey (char* AppName) 8 { 9 HKEY hAppKey = NULL; 10 HKEY hSoftKey = NULL; 11 if (RegOpenKeyEx (HKEY_CURRENT_USER, "Software", 0, KEY_WRITE | KEY_READ, 12 &hSoftKey) == ERROR_SUCCESS) 13 { 14 DWORD Dummy; 15 RegCreateKeyEx (hSoftKey, AppName, 0, REG_NONE, 16 REG_OPTION_NON_VOLATILE, KEY_WRITE | KEY_READ, NULL, 17 &hAppKey, &Dummy); 18 } 19 if (hSoftKey) 20 RegCloseKey (hSoftKey); 21 22 return hAppKey; 23 } 24 25 // Returns key for 26 // HKEY_CURRENT_USER\"Software"\RegistryKey\AppName\Section 27 // creating it if it doesn't exist. 28 // responsibility of the caller to call RegCloseKey () on the returned HKEY 29 // 30 HKEY GetSectionKey (HKEY hAppKey, LPCTSTR Section) 31 { 32 HKEY hSectionKey = NULL; 33 DWORD Dummy; 34 RegCreateKeyEx (hAppKey, Section, 0, REG_NONE, 35 REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL, 36 &hSectionKey, &Dummy); 37 return hSectionKey; 38 } 39 40 int GetRegistryInt (HKEY hSectionKey, LPCTSTR Entry, int Default) 41 { 42 DWORD Value; 43 DWORD Type; 44 DWORD Count = sizeof (DWORD); 45 if (RegQueryValueEx (hSectionKey, (LPTSTR) Entry, NULL, &Type, 46 (LPBYTE) &Value, &Count) == ERROR_SUCCESS) 47 return Value; 48 return Default; 49 } 50 51 bool WriteRegistryInt (HKEY hSectionKey, char* Entry, int nValue) 52 { 53 return RegSetValueEx (hSectionKey, Entry, NULL, REG_DWORD, 54 (LPBYTE) &nValue, sizeof (nValue)) == ERROR_SUCCESS; 55 } 56 57