Click here to Skip to main content
15,889,595 members
Articles / Desktop Programming / ATL
Article

User-settings class for ATL/WTL projects

Rate me:
Please Sign up or sign in to vote.
4.33/5 (2 votes)
3 Apr 2001 96.6K   458   38   12
A helper class for storing user settings in the Registry, similar to MFC's CWinApp

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
New Zealand New Zealand
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralUpdated for VC++ .NET Pin
negotiator22-Jun-03 19:50
negotiator22-Jun-03 19:50 
GeneralRe: Updated for VC++ .NET Pin
Rob Staveley7-May-04 2:14
Rob Staveley7-May-04 2:14 
GeneralMFC Pin
2-Oct-01 0:27
suss2-Oct-01 0:27 
GeneralOoouuuccchhh Pin
Tomaz Stih4-Mar-01 23:33
Tomaz Stih4-Mar-01 23:33 
GeneralRe: Ooouuuccchhh - damn parser ate most important characters Pin
Tomaz Stih4-Mar-01 23:36
Tomaz Stih4-Mar-01 23:36 
GeneralRe: Ooouuuccchhh Pin
Peter Kenyon5-Mar-01 11:57
Peter Kenyon5-Mar-01 11:57 
GeneralRe: Ooouuuccchhh Pin
Tomaz Stih5-Mar-01 23:52
Tomaz Stih5-Mar-01 23:52 
> I like this idea. Why not have something like this:
> UserSetting<int, _t("mycompany\\screen")=""> iNrOfOpenedWindows
> Then put the code to load the value in the constructor and the
> code to save the value in the destructor. That way the
> value is loaded when the app starts up and saved when it terminates.

And it would work for stack variables just as well (as stack is
unwinded the vars are saved). But I'd prefer to assign key at
runtime (templates are expanded at compile time). Something like:

MachineSetting<int> iNrOfOpenedWindows(_T("path\to\value"));

Same thing, but you can provide variable for the key.

Note however, that in real life automatic Load and Save are not always desireable. Imagine a classic configuration dialog (i.e. Control Panel applet). It comes in the form of a wizard or a property sheet. You store values to member variables. If the user cancels the wizard or the sheet then values should not be written to the registry.

For this case I would have a base class Setting with Load and Save virtuals. Then derive MachineSetting : public Setting.
A class called SettingsManager would have methods Append(Setting &) and SaveAll(). You append your settings to the setting manager and on Ok or Apply call sm.SaveAll().

If you hate to loose automation you could have traits for AutoSave
and AutoLoad and for ManualSave and ManualLoad. Default to manual...

How'bout someone implementing our wonderful design now that we did "all the work" Cool | :cool: .

Tomaz

Tomaz Stih, B.Sc.SE
GeneralRe: Ooouuuccchhh Pin
Peter Kenyon6-Mar-01 11:51
Peter Kenyon6-Mar-01 11:51 
QuestionArchaic? Pin
DBJ22-Aug-00 10:03
DBJ22-Aug-00 10:03 
AnswerRe: Archaic? Pin
Peter31-Aug-00 19:11
Peter31-Aug-00 19:11 
AnswerRe: Archaic? Pin
aaa28-Oct-00 10:54
aaa28-Oct-00 10:54 
GeneralHas a dependency on CString Pin
Peter Kenyon6-Jul-00 20:02
Peter Kenyon6-Jul-00 20:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.