![]() |
General Reading »
Hardware & System »
Registry
Intermediate
CRegSettings - registry helper classBy Magomed AbdurakhmanovSimple class to store application settings in registry |
VC6, VC7, VC7.1, VC8.0Win2K, WinXP, ATL, WTL, STL, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
It's so tedious to use Win32 API or even CRegKey helper class to save/load configuration values to/from registry. This class uses DDX-like metaphors to map class member variables to registry data. It is very simple to use and smart enough for most typical registry usage.
BEGIN_REG_MAP and END_REG_MAP: // Sample application configuration class CMySettings : public CRegSettings { public: DWORD Value1; // DWORD option CString Value2; // String option DWORD RequiredValue; BEGIN_REG_MAP(CMySettings) REG_ITEM(Value1, 1) REG_ITEM(Value2, "Default Value") REG_ITEM_REQUIRE(RequiredValue) END_REG_MAP() };
CMySettings settings(HKEY_CURRENT_USER,
"Software\\My Company\\Application\\1.0");
settings.Load(); // Load configuration
... // use values settings.Value1 etc.
settings.Save(); // Save configuration
CRegSettings constructor can be called with variable number of parameters:
CMySettings settings(HKEY_CURRENT_USER, "Software\\%s\\%s\\%i", "My Company", "My Application", Version);
Variables mapped to corresponding keys and values in registry:
| Variable type | Registry data type |
DWORD |
REG_DWORD |
int |
REG_DWORD |
long |
REG_DWORD |
bool |
REG_DWORD |
char |
REG_DWORD |
wchar_t |
REG_DWORD |
TCHAR* |
REG_SZ |
void* (structs, arrays etc.) |
REG_BINARY |
CString |
REG_SZ |
CSimpleArray<T>* |
Sub-keys |
std::string |
REG_SZ |
std::vector<T>* |
Sub-keys |
std::list<T>* |
Sub-keys |
T* |
Sub-key |
* - T must be inherited from CRegSettings and must contain map declared with BEGIN_REG_MAP - END_REG_MAP
BEGIN_REG_MAP(Name of class) - Marks the beginning of the registry map.
END_REG_MAP() - Marks the end of the registry map.
REG_ITEM(VarName, DefaultValue) - Maps variable to registry value. Registry value will be named "VarName". If the value doesn't exist in registry when loading then variable will be assigned to DefaultValue. Variable can be of one of the following types: DWORD, int, long, bool, char, wchar_t, CString.
REG_ITEM_REQUIRE(VarName) - Same as REG_ITEM, but you cannot specify default value. And Load() call will fail if the value doesn't exist in registry.
REG_ITEM_SUBKEY(VarName) - Maps class inherited from a CRegSettings to sub-key. The class must contain map declared with BEGIN_REG_MAP - END_REG_MAP. See sample application.
REG_ITEM_SIMPLE_ARRAY(VarName) - Maps ATL template class CSimpleArray<T> to registry. T must be inherited from CRegSettings and must have map declared with BEGIN_REG_MAP - END_REG_MAP. Array items will be saved under sub-keys in registry. See sample application.
REG_ITEM_VECTOR(VarName) - Same as REG_ITEM_SIMPLE_ARRAY, but forstd::vector type.
REG_ITEM_LIST(VarName) - Same as REG_ITEM_SIMPLE_ARRAY, but for std::list type.
REG_ITEM_SZ(VarName, DEFAULT_VALUE) - Maps C string (TCHAR*) to registry value (REG_SZ). The registry value will be named "VarName". If the value doesn't exist in registry when loading then variable will be assigned to DefaultValue.
REG_ITEM_SZ_REQUIRE(VarName) - Same as REG_ITEM_SZ, but you cannot specify default value. And Load() call will fail if the value doesn't exist in registry.
REG_ITEM_SZ_LEN(VarName, DEFAULT_VALUE, VarLen) - Same as REG_ITEM_SZ with the additional parameter VarLen used to specify buffer size in TCHARs.
REG_ITEM_SZ_REQUIRE_LEN(VarName, VarLen) - Same as REG_ITEM_SZ_REQUIRE with the additional parameter VarLen used to specify buffer size in TCHARs.
REG_ITEM_BINARY(VarName) - Maps any type to registry value (REG_BINARY). The registry value will be named "VarName". Useful with structures, arrays etc. Size of binary data is calculated through sizeof(VarName).
REG_ITEM_BINARY_SIZE(VarName, VarSize) - Same as REG_ITEM_BINARY with the additional parameter VarSize which specifies variable size.
REG_ITEM_STL(VarName, DefaultValue) and REG_ITEM_STL_REQUIRE(VarName) - Same as REG_ITEM and REG_ITEM_REQUIRE. Maps std::string to REG_SZ.
OnBeforeSave, OnAfterLoad virtual methods;
REG_ITEM_SUBKEY: store data in sub keys
REG_ITEM_BINARY,REG_ITEM_BINARY_SIZE: store binary data (void*, structs, etc.)
REG_ITEM_SZ, REG_ITEM_SZ_REQUIRE, REG_ITEM_SZ_LEN,
REG_ITEM_SZ_REQUIRE_LEN: store C strings (TCHAR*)
REG_ITEM and REG_ITEM_REQUIRED enhanced to support: bool, int, char and wchar_t | You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 7 Oct 2002 Editor: Nishant Sivakumar |
Copyright 2002 by Magomed Abdurakhmanov Everything else Copyright © CodeProject, 1999-2009 Web12 | Advertise on the Code Project |