Click here to Skip to main content
12,625,694 members (35,731 online)
Click here to Skip to main content
Add your own
alternative version

Stats

87.6K views
433 downloads
38 bookmarked
Posted

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.

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

Share

About the Author

Peter Kenyon
Web Developer
New Zealand New Zealand
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
GeneralUpdated for VC++ .NET Pin
negotiator22-Jun-03 20:50
membernegotiator22-Jun-03 20:50 
GeneralRe: Updated for VC++ .NET Pin
Rob Staveley7-May-04 3:14
memberRob Staveley7-May-04 3:14 
Your link is broken, Dave. But I've done the same as follows:
// appsettings.h
//
// Utility class for storing/reading profile values from the Registry
// (C) 2000 Peter Kenyon
//
// You may freely distribute this source code, providing that you do not charge for it
// and that this copyright notice remains intact.
//
// Please report any bugs to Peter@bizinf.co.nz
//
// Edited for VC 7.1 to avoid ATL_DEPRECATED warnings
//

#ifndef _APPSETTINGS_H
#define _APPSETTINGS_H

class CAppSettings
{
public:
	// Must be called once to set the name of the key under HKLM\\Software
	// or HKCU\\Software.
	// Normally this will be of the form "[company name]\\[app name]"
	void SetRegKey(LPCTSTR pszKey)
	{
		m_strRegKeyName = _T("Software\\");
		m_strRegKeyName += pszKey;
	}
 
	CString GetUserProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault = NULL)
	{
		CString strKey = CatKeyAndSubkeyNames(lpszSection);
		CRegKey key;
 
		if(key.Open(HKEY_CURRENT_USER, strKey, KEY_READ) == ERROR_SUCCESS)
		{
			DWORD dwcbNeeded = 0;
			if(key.QueryValue(lpszEntry, NULL, NULL, &dwcbNeeded) == ERROR_SUCCESS)
			{
				CString strVal;
				LPTSTR pszVal = strVal.GetBuffer(dwcbNeeded);
				if(key.QueryStringValue(lpszEntry, pszVal, &dwcbNeeded) == ERROR_SUCCESS) {
					strVal.ReleaseBuffer();
					key.Close();
					return strVal;
				}
				else {
					strVal.ReleaseBuffer(0);
				}
			}
		}
 
		key.Close();
		return CString(lpszDefault);
	}
 
	CString GetMachineProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault = NULL)
	{
		CString strKey = CatKeyAndSubkeyNames(lpszSection);
		CRegKey key;
 
		if(key.Open(HKEY_LOCAL_MACHINE, strKey, KEY_READ) == ERROR_SUCCESS)
		{
			DWORD dwcbNeeded = 0;
			if(key.QueryValue(lpszEntry, NULL, NULL, &dwcbNeeded) == ERROR_SUCCESS)
			{
				CString strVal;
				LPTSTR pszVal = strVal.GetBuffer(dwcbNeeded);
 
				if(key.QueryStringValue(lpszEntry, pszVal, &dwcbNeeded) == ERROR_SUCCESS)
				{
					strVal.ReleaseBuffer();
					key.Close();
					return strVal;
				}
				else
				{
					strVal.ReleaseBuffer(0);
				}
			}
		}
 
		key.Close();
		return CString(lpszDefault);
	}
 
	DWORD GetUserProfileDword(LPCTSTR lpszSection, LPCTSTR lpszEntry, DWORD dwDefault = 0)
	{
		CString strKey = CatKeyAndSubkeyNames(lpszSection);
		CRegKey key;
 
		if(key.Open(HKEY_CURRENT_USER, strKey, KEY_READ) == ERROR_SUCCESS)
		{
			DWORD dwVal;
			if(key.QueryDWORDValue(lpszEntry, dwVal) == ERROR_SUCCESS)
			{
				key.Close();
				return dwVal;
			}
		}
 
		key.Close();
		return dwDefault;;
	}
 
	DWORD GetMachineProfileDword(LPCTSTR lpszSection, LPCTSTR lpszEntry, DWORD dwDefault = 0)
	{
		CString strKey = CatKeyAndSubkeyNames(lpszSection);
		CRegKey key;
 
		if(key.Open(HKEY_LOCAL_MACHINE, strKey, KEY_READ) == ERROR_SUCCESS)
		{
			DWORD dwVal;
			if(key.QueryDWORDValue(lpszEntry, dwVal) == ERROR_SUCCESS)
			{
				key.Close();
				return dwVal;
			}
		}
 
		key.Close();
		return dwDefault;;
	}
 
	template <class T> BOOL GetMachineProfileStruct(LPCTSTR lpszSection, LPCTSTR lpszEntry, T& obj)
	{
		CString strKey = CatKeyAndSubkeyNames(lpszSection);
		CRegKey key;
 
		if(key.Open(HKEY_LOCAL_MACHINE, strKey, KEY_READ) == ERROR_SUCCESS)
		{
			DWORD dwcbSize = sizeof(T);
			if(::RegQueryValueEx(key, lpszEntry, NULL, NULL, (LPBYTE)&obj, &dwcbSize) == ERROR_SUCCESS)
			{
				key.Close();
				return TRUE;
			}
		}
 
		key.Close();
		return FALSE;
	}
 
	template <class T> BOOL GetUserProfileStruct(LPCTSTR lpszSection, LPCTSTR lpszEntry, T& obj)
	{
		CString strKey = CatKeyAndSubkeyNames(lpszSection);
		CRegKey key;
 
		if(key.Open(HKEY_CURRENT_USER, strKey, KEY_READ) == ERROR_SUCCESS)
		{
			DWORD dwcbSize = sizeof(T);
			if(::RegQueryValueEx(key, lpszEntry, NULL, NULL, (LPBYTE)&obj, &dwcbSize) == ERROR_SUCCESS)
			{
				key.Close();
				return TRUE;
			}
		}
 
		key.Close();
		return FALSE;
	}
 
	BOOL WriteMachineProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszVal)
	{
		CString strKey = CatKeyAndSubkeyNames(lpszSection);
		CRegKey key;
 
		if(key.Create(HKEY_LOCAL_MACHINE, strKey, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_WRITE) == ERROR_SUCCESS)
		{
			if(key.SetStringValue(lpszEntry, lpszVal) == ERROR_SUCCESS)
			{
				key.Close();
				return TRUE;
			}
		}
 
		key.Close();
		return FALSE;
	}
 
	template <class T> BOOL WriteMachineProfileStruct(LPCTSTR lpszSection, LPCTSTR lpszEntry, T& obj)
	{
		CString strKey = CatKeyAndSubkeyNames(lpszSection);
		CRegKey key;
 
		if(key.Create(HKEY_LOCAL_MACHINE, strKey, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_WRITE) == ERROR_SUCCESS)
		{
			if(::RegSetValueEx(key, lpszEntry, 0, REG_BINARY, (LPBYTE)&obj, sizeof(T)) == ERROR_SUCCESS)
			{
				key.Close();
				return TRUE;
			}
		}
 
		key.Close();
		return FALSE;
	}
 
	template <class T> BOOL WriteUserProfileStruct(LPCTSTR lpszSection, LPCTSTR lpszEntry, T& obj)
	{
		CString strKey = CatKeyAndSubkeyNames(lpszSection);
		CRegKey key;
 
		if(key.Create(HKEY_CURRENT_USER, strKey, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_WRITE) == ERROR_SUCCESS)
		{
			if(::RegSetValueEx(key, lpszEntry, 0, REG_BINARY, (LPBYTE)&obj, sizeof(T)) == ERROR_SUCCESS)
			{
				key.Close();
				return TRUE;
			}
		}
 
		key.Close();
		return FALSE;
	}
 
	BOOL WriteMachineProfileDword(LPCTSTR lpszSection, LPCTSTR lpszEntry, DWORD dwVal)
	{
		CString strKey = CatKeyAndSubkeyNames(lpszSection);
		CRegKey key;
 
		if(key.Create(HKEY_LOCAL_MACHINE, strKey, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_WRITE) == ERROR_SUCCESS)
		{
			if(key.SetDWORDValue(lpszEntry, dwVal) == ERROR_SUCCESS)
			{
				key.Close();
				return TRUE;
			}
		}
 
		key.Close();
		return FALSE;
	}
 
	BOOL WriteUserProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszVal)
	{
		CString strKey = CatKeyAndSubkeyNames(lpszSection);
		CRegKey key;
 
		if(key.Create(HKEY_CURRENT_USER, strKey, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_WRITE) == ERROR_SUCCESS)
		{
			if(key.SetStringValue(lpszEntry, lpszVal) == ERROR_SUCCESS)
			{
				key.Close();
				return TRUE;
			}
		}
 
		key.Close();
		return FALSE;
	}
 
	BOOL WriteUserProfileDword(LPCTSTR lpszSection, LPCTSTR lpszEntry, DWORD dwVal)
	{
		CString strKey = CatKeyAndSubkeyNames(lpszSection);
		CRegKey key;
 
		if(key.Create(HKEY_CURRENT_USER, strKey, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_WRITE) == ERROR_SUCCESS)
		{
			if(key.SetDWORDValue(lpszEntry, dwVal) == ERROR_SUCCESS)
			{
				key.Close();
				return TRUE;
			}
		}
 
		key.Close();
		return FALSE;
	}
 
private:
	CString CatKeyAndSubkeyNames(LPCTSTR lpszSection)
	{
		ATLASSERT(!m_strRegKeyName.IsEmpty());
		CString strKey = m_strRegKeyName;
 
		if(lpszSection)
		{
			strKey += _T("\\");
			strKey += lpszSection;
		}
 
		return strKey;
	}
 
	CString m_strRegKeyName;
};
 
#endif // _APPSETTINGS_H


Rob
GeneralMFC Pin
Anonymous2-Oct-01 1:27
memberAnonymous2-Oct-01 1:27 
GeneralOoouuuccchhh Pin
Tomaz Stih5-Mar-01 0:33
memberTomaz Stih5-Mar-01 0:33 
GeneralRe: Ooouuuccchhh - damn parser ate most important characters Pin
Tomaz Stih5-Mar-01 0:36
memberTomaz Stih5-Mar-01 0:36 
GeneralRe: Ooouuuccchhh Pin
Peter Kenyon5-Mar-01 12:57
memberPeter Kenyon5-Mar-01 12:57 
GeneralRe: Ooouuuccchhh Pin
Tomaz Stih6-Mar-01 0:52
memberTomaz Stih6-Mar-01 0:52 
GeneralRe: Ooouuuccchhh Pin
Peter Kenyon6-Mar-01 12:51
memberPeter Kenyon6-Mar-01 12:51 
QuestionArchaic? Pin
dbj22-Aug-00 11:03
sussdbj22-Aug-00 11:03 
AnswerRe: Archaic? Pin
Peter31-Aug-00 20:11
sussPeter31-Aug-00 20:11 
AnswerRe: Archaic? Pin
aaa28-Oct-00 11:54
sussaaa28-Oct-00 11:54 
GeneralHas a dependency on CString Pin
Peter Kenyon6-Jul-00 21:02
sussPeter Kenyon6-Jul-00 21: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.

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