Click here to Skip to main content
15,891,976 members
Articles / Desktop Programming / MFC

S.I.V.: Simple registry config class

Rate me:
Please Sign up or sign in to vote.
3.00/5 (6 votes)
1 Nov 20044 min read 35K   912   15  
Simplicity Is Virtue: How to store your app's settings in Windows registry with minimum effort.
//************************************************************
//*
//*	RegSettings.h
//*
//*	CRegSettings class interface.
//*
//*	DESCRIPTION:
//*		One MFC CObject derived class to implement a simple
//*		interface for storing app settings into registry.
//*
//*	ASSUMES:
//*		Only MFC and an average coder :-)
//*
//*	AUTHOR: T1TAN <t1tan@cmar-net.org>
//*
//*	COPYLEFT:
//*		Copyleft (C) T1TAN 2004 - 3827
//*		Copyleft (C) SprdSoft Inc.2004 - 3827
//*		FREE for (ab)use in any way except selling the code.
//*		Leave the headers be.
//*
//*	VERSIONS:
//*		VERSION		AUTHOR		DATE		NOTES
//*		-------		------		----		-----
//*		1.0			T1TAN		18/10/2004	initial version
//*
//*
//*	OTHER:
//*		<nothing yet>
//*
//************************************************************

#ifndef REGSETTINGS_H
#define REGSETTINGS_H

#ifndef twice
#define twice once
#endif
// there used to be a "#pragma once" here, but thanx to one of
// my friends, we now have a better version
#if _MSC_VER > 1000
#pragma twice
#endif // _MSC_VER > 1000

class CRegSettings : public CObject  
{
public:
	// c'tor and d'tor
	CRegSettings( BOOL bUseFlushing = FALSE );
	virtual ~CRegSettings();

	// inline functions for properties
	void SetUseFlushing( BOOL bUseFlushing = TRUE )
	{ m_bUseFlushing = bUseFlushing; }
	void SetAppName( LPCTSTR szAppName )
	{	// set the name and remove junk
		m_szAppName = szAppName;
		m_szAppName.TrimLeft();
		m_szAppName.TrimRight();
		m_szAppName.Replace( "/", NULL );
		m_szAppName.Replace( "\\", NULL );
	} 
	void SetCompanyName( LPCTSTR szCompanyName )
	{	// same here..
		m_szCompanyName = szCompanyName;
		m_szCompanyName.TrimLeft();
		m_szCompanyName.TrimRight();
		m_szCompanyName.Replace( "/", NULL );
		m_szCompanyName.Replace( "\\", NULL );
	}
	CString GetAppName() const
	{ return m_szAppName; }
	CString GetCompanyName() const
	{ return m_szCompanyName; }
	BOOL GetUseFlushing() const
	{ return m_bUseFlushing; }

	// one static for retrieving last error text
	static CString GetLastErrorText();

	// to remove all settings
	BOOL DeleteAll( BOOL bDeleteCompany = FALSE );

	// overloads for sale
	// you pay for two, but get one extra for your wife and kids
	BOOL Write( LPCTSTR szKey, LPCTSTR szName, UCHAR value );
	BOOL Write( LPCTSTR szKey, LPCTSTR szName, char value );
	BOOL Write( LPCTSTR szKey, LPCTSTR szName, USHORT value );
	BOOL Write( LPCTSTR szKey, LPCTSTR szName, short value );
	BOOL Write( LPCTSTR szKey, LPCTSTR szName, UINT value );
	BOOL Write( LPCTSTR szKey, LPCTSTR szName, int value );
	BOOL Write( LPCTSTR szKey, LPCTSTR szName, ULONG value );
	BOOL Write( LPCTSTR szKey, LPCTSTR szName, long value );
	BOOL Write( LPCTSTR szKey, LPCTSTR szName, float value );
	BOOL Write( LPCTSTR szKey, LPCTSTR szName, double value );
	BOOL Write( LPCTSTR szKey, LPCTSTR szName, LPCTSTR value );
	BOOL Write( LPCTSTR szKey, LPCTSTR szName, const LPVOID value, UINT size );

	BOOL Read( LPCTSTR szKey, LPCTSTR szName, UCHAR& value );
	BOOL Read( LPCTSTR szKey, LPCTSTR szName, char& value );
	BOOL Read( LPCTSTR szKey, LPCTSTR szName, USHORT& value );
	BOOL Read( LPCTSTR szKey, LPCTSTR szName, short& value );
	BOOL Read( LPCTSTR szKey, LPCTSTR szName, UINT& value );
	BOOL Read( LPCTSTR szKey, LPCTSTR szName, int& value );
	BOOL Read( LPCTSTR szKey, LPCTSTR szName, ULONG& value );
	BOOL Read( LPCTSTR szKey, LPCTSTR szName, long& value );
	BOOL Read( LPCTSTR szKey, LPCTSTR szName, float& value );
	BOOL Read( LPCTSTR szKey, LPCTSTR szName, double& value );
	BOOL Read( LPCTSTR szKey, LPCTSTR szName, LPVOID value, ULONG& size );
	BOOL Read( LPCTSTR szKey, LPCTSTR szName, LPTSTR value, ULONG& size );

protected:
	CString m_szCompanyName;
	CString m_szAppName;
	BOOL m_bUseFlushing;

	// our private stuff you don't need and can't have (-:
	BOOL GetHKEY( LPCTSTR szKey, PHKEY hKey, BOOL bRead = FALSE );
	void CloseHKEY( HKEY hKey );
	CString CleanKeyName( LPCTSTR szKey );
};

#endif // REGSETTINGS_H

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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
Croatia Croatia
A software developer for the masses..

Comments and Discussions