User-settings class for ATL/WTL Projects
WTL is a great framework for putting together small, lightweight applications, but I miss some of the helpful
features of MFC. One of these is the ability to save user settings in the registry. So I made this class to save
me the hassle of calling the registry API by hand. All you need to do is derive your main user-interface class
(either your frame window or view) from CAppSettings, like so:
class CMainFrame : public CAppSettings, CFrameWindowImpl<CMainFrame>, ...
After that, one call to specify the name of the key your settings will be stored under and you're away. You
can set and retrieve strings, DWORDs and structs, and they can be stored under HKEY_LOCAL_MACHINE
or HKEY_CURRENT_USER
. In either case, your settings will be stored in a subkey of Software
.
Functions
Use this function to specify the root key for your settings. Typically you will want to use something like "companyname\\appname".
- void SetRegKey(LPCTSTR lpszKey)
Use these functions for storing values in the registry. Replace XXX with "Machine" or "User",
depending on whether you want them stored under HKEY_LOCAL_MACHINE
or HKEY_CURRENT_USER
.
In each of the functions below, lpszSection
is the name of a further subkey which will be added to
the string you specified in SetRegKey
. So the entire key path will look like this: [HKLM / HKCU]\Software\keyname\section
name\value
- BOOL WriteXXXProfileDword(LPCTSTR lpszSection, LPCTSTR lpszEntry, DWORD dwVal)
- BOOL WriteXXXProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszVal)
- template <class T> BOOL WriteXXXProfileStruct(LPCTSTR lpszSection, LPCTSTR lpszEntry, T& obj)
Use these functions to retrieve a value from the registry, or a default value if it isn't there. The function
to store a user-defined struct cannot retrieve a default value, and only returns TRUE or FALSE to indicate whether
or not the call succeeded.
- DWORD GetXXXProfileDword(LPCTSTR lpszSection, LPCTSTR lpszEntry, DWORD dwDefault = 0)
- CString GetXXXProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault = NULL)
- template <class T> BOOL GetXXXProfileStruct(LPCTSTR lpszSection, LPCTSTR lpszEntry, T& obj)
That's it! I hope you find it useful.