5 * DEFINITIONS ________________________________________________________________
14 #define HKCR HKEY_CLASSES_ROOT
18 #define HKCU HKEY_CURRENT_USER
22 #define HKLM HKEY_LOCAL_MACHINE
26 #define HIBYTE(_w) (BYTE)(((_w) >> 8) & 0x00FF)
30 #define LOBYTE(_w) (BYTE)((_w) & 0x00FF)
34 #define MAKEVERSION(_h,_l) ( ((((WORD)(_h)) << 8) & 0xFF00) | (((WORD)(_l)) & 0xFF) )
40 * PROTOTYPES _________________________________________________________________
44 * Store/RestoreSettings() are pretty simple--they're just convenience
45 * wrappers for providing version-controlled storage of a structure in
46 * the registry. You provide the structure and supply a version number;
47 * when your program loads, call RestoreSettings() to see if you've saved
48 * anything previously. When your program exits, call StoreSettings()
49 * so you don't lose anything--effectively, you'll have global variables
50 * that won't change from one run of your program to the next. Again,
51 * this is really pretty elementary stuff, but I find I write it a lot
52 * so I stuck it in a common source file.
55 * About version numbers:
57 * Don't just pick them at random--there's actually an algorithm here.
58 * A typical version number is composed of two parts--a major and a
59 * minor component. Start with a version of 1.0, just for grins.
60 * Whenever you *add* something to the structure, increment the minor
61 * version--and make sure you add to the end of the structure. If you
62 * have to do anything else to the structure--get rid of an element,
63 * reorder things, change the size of an element--increment the major
66 * Why? Because it provides backward compatibility. StoreSettings()
67 * always writes out the version number that you provide it... but
68 * RestoreSettings() is picky about what versions it will load:
70 * (1) Restore fails if the stored major version != the expected version
71 * (2) Restore fails if the stored minor version < the expected version
73 * Another way of looking at that is,
75 * (1) Restore succeeds only if the major versions are identical
76 * (2) Restore succeeds only if the stored version is higher or equal
78 * So if you run a 1.4 program and it tries to read a 1.7 level stored
79 * structure, it will succeed!! Why? Because a 1.4 structure is just
80 * like a 1.7 structure--only missing some stuff at the end, which
81 * RestoreSettings will just ignore (the 1.4 program didn't know about
82 * it anyway, and obviously did just fine without it).
89 * TCHAR szLastDirectory[ MAX_PATH ];
90 * TCHAR szUserName[ MAX_PATH ];
93 * #define wVerGLOBALS MAKEVERSION(1,0) // major version 1, minor version 0
95 * #define cszPathGLOBALS TEXT("Software\\MyCompany\\MyProgram")
96 * #define cszValueGLOBALS TEXT("Settings")
100 * if (!RestoreSettings (HKLM, cszPathGLOBALS, cszValueGLOBALS,
101 * &globals_restored, sizeof(globals_restored),
104 * memset (&globals_restored, 0x00, sizeof(globals_restored));
105 * lstrcpy (globals_restored.szUserName, TEXT("unknown"));
106 * // set any other default, first-time-run values here
111 * StoreSettings (HKLM, cszPathGLOBALS, cszValueGLOBALS,
112 * &globals_restored, sizeof(globals_restored),
117 EXPORTED void EraseSettings (HKEY hkParent,
121 EXPORTED BOOL RestoreSettings (HKEY hkParent,
128 EXPORTED BOOL StoreSettings (HKEY hkParent,
135 EXPORTED size_t GetRegValueSize (HKEY hk,
139 EXPORTED BOOL GetBinaryRegValue (HKEY hk,
145 EXPORTED BOOL SetBinaryRegValue (HKEY hk,
151 EXPORTED BOOL RegDeltreeKey (HKEY hk, LPTSTR pszKey);