Click here to Skip to main content
Click here to Skip to main content

User-settings class for ATL/WTL projects

, 3 Apr 2001
Rate this:
Please Sign up or sign in to vote.
A helper class for storing user settings in the Registry, similar to MFC's CWinApp
<!-- Link to source file download --> <!-- Add the rest of your HTML here -->

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.


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.


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


About the Author

Peter Kenyon
Web Developer
New Zealand New Zealand
No Biography provided

Comments and Discussions

GeneralUpdated for VC++ .NET Pinmembernegotiator22-Jun-03 20:50 
GeneralRe: Updated for VC++ .NET PinmemberRob Staveley7-May-04 3:14 
GeneralMFC PinmemberAnonymous2-Oct-01 1:27 
GeneralOoouuuccchhh PinmemberTomaz Stih5-Mar-01 0:33 
GeneralRe: Ooouuuccchhh - damn parser ate most important characters PinmemberTomaz Stih5-Mar-01 0:36 
GeneralRe: Ooouuuccchhh PinmemberPeter Kenyon5-Mar-01 12:57 
Thanks for your comments.
> Using inheritance in a way you do in C++ means establishing IS-A relationship. This
> means that your main frame is an app setting. Nedless to say, this is extremely ugly,
> plus you use multiple inheritance.
> Instead you should use HAS-A relationship and bind it to your application, not your main
> frame.
Actually it doesn't matter what class you bind it to, it doesn't have to be the main frame at all. You could derive a class from CComModule and associate it with that.
You don't need to use multi inheritance, although if you are using ATL/WTL most of your classes will be multi inheritance anyway. You can make CAppSettings a member of a containing class. So you can use whatever methodology you prefer.
> I had this neat idea about implementing registry values based on templates (if anyone
> cares to play with it?). Compliant with ATL/WTL philosophy one could have something like:
> UserSetting<int> iNrOfOpenedWindows;
> MachineSetting<std::string> sCustomerIdForAutoUpdate;
> Then one could say iNrOfOpenedWindows=1 (treat it as integer) and
> iNrOfOpenedWindows.Save("MyCompanyName/MyAppKey"). You could provide different Save/Load
> pre-implementations for some known types using C++ traits...
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.

GeneralRe: Ooouuuccchhh PinmemberTomaz Stih6-Mar-01 0:52 
GeneralRe: Ooouuuccchhh PinmemberPeter Kenyon6-Mar-01 12:51 
QuestionArchaic? Pinsussdbj22-Aug-00 11:03 
AnswerRe: Archaic? PinsussPeter31-Aug-00 20:11 
AnswerRe: Archaic? Pinsussaaa28-Oct-00 11:54 
GeneralHas a dependency on CString PinsussPeter Kenyon6-Jul-00 21:02 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150326.1 | Last Updated 4 Apr 2001
Article Copyright 2000 by Peter Kenyon
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid