Click here to Skip to main content
15,894,405 members
Articles / Desktop Programming / WTL

CRegSettings - registry helper class

Rate me:
Please Sign up or sign in to vote.
4.97/5 (15 votes)
7 Oct 20023 min read 97.1K   2.5K   44  
Simple class to store application settings in registry
// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "rsettings.h"

// Sample application configuration item
class CMySettingsItem : public CRegSettings
{
public:
	DWORD Id;
	CString Name;

	BEGIN_REG_MAP(CMySettingsItem)
		REG_ITEM(Id, 1)
		REG_ITEM(Name, "Default Name Value")
	END_REG_MAP()
};

// Sample application configuration
class CMySettings : public CRegSettings
{
public:

	DWORD RootId; // DWORD option
	CString RootName; // String option
	std::string FullName;

	// list of options (CMySettingsItem)
	CSimpleArray<CMySettingsItem> MyItems;	// ATL CSimpleArray
	std::vector<CMySettingsItem> MyItems2;	// STL vector
	std::list<CMySettingsItem> MyItems3;	// STL list

	CMySettingsItem SubItem; // Subitem test

	BEGIN_REG_MAP(CMySettings)
		REG_ITEM(RootId, 1)
		REG_ITEM(RootName, "Default Root Name")
		REG_ITEM_STL(FullName, "Full Name")
		REG_ITEM_SIMPLE_ARRAY(MyItems)
		REG_ITEM_VECTOR(MyItems2)
		REG_ITEM_LIST(MyItems3)
		REG_ITEM_SUBKEY(SubItem)
	END_REG_MAP()
};


int _tmain(int argc, _TCHAR* argv[])
{
	CMySettings configuration(HKEY_CURRENT_USER, "Software\\RegSetings Test\\test\\%i.%i", 1, 0);

	// Load configuration
	if(configuration.Load() != ERROR_SUCCESS)
		printf("failed to load\n");
	
	// Use loaded configuration
	configuration.RootId++;
	
	CMySettingsItem item;
	item.Id = configuration.RootId;
	
	configuration.MyItems.Add(item); // CSimpleArray
	configuration.MyItems2.push_back(item); // vector
	configuration.MyItems3.push_back(item); // list

	// Save new configuration
	if(configuration.Save() != ERROR_SUCCESS)
		printf("failed to save\n");
	return 0;
}

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
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions