首页 > 其他分享 >CRegSettings - registry helper class

CRegSettings - registry helper class

时间:2022-12-13 14:34:47浏览次数:64  
标签:SZ helper MAP VarName ITEM registry CRegSettings REG

CRegSettings - registry helper class

Introduction

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.

How to use

  1. Include rsettings.h into your project
  2. Declare class that will contain configuration values and define map with 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()
    };
  3. That’s All! Now you can save or load values:

     

     
    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);

Supported types

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

Macros reference

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: DWORDintlongboolcharwchar_tCString.

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.

 

History

  • 7.10.2002
    • OnBeforeSaveOnAfterLoad virtual methods;
    • Bugs fixed.
  • 25.09.2002
    • 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: boolintchar and wchar_t
  • 19.09.2002
    • Created

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

 

标签:SZ,helper,MAP,VarName,ITEM,registry,CRegSettings,REG
From: https://www.cnblogs.com/qzxff/p/16978665.html

相关文章

  • Registry&Harbor私有仓库问题
    1、按照我对docker的理解,宿主机a,想要访问宿主机机b中的容器,哪怕是局域网访问,一定要让这个容器绑定宿主机b的物理端口,然后通过宿主机b的ip和端口访问但是老师今天的课程中,他......
  • [Typescript] 134. Easy - Identity Helper
    import{Equal,Expect}from"../helpers/type-utils";typeIdentity<T>=T;typetests=[Expect<Equal<Identity<1>,1>>,Expect<Equal<Identity<"1">,"1......
  • [Typescript] 135. Easy - Maybe helper
    import{Equal,Expect}from"../helpers/type-utils";typeMaybe<T>=T|null|undefined;typetests=[Expect<Equal<Maybe<string>,string|null|und......
  • node.js : request to https://registry.npmjs.org/vue failed, reason: connect ETIM
     https://v2.vuejs.org/v2/guide/installation.htmlhttps://v3.router.vuejs.org/installation.htmlhttps://v3.cli.vuejs.org/https://v3.vuex.vuejs.org/installation.......
  • docker 1.13版本修改默认存储路径及添加私有registry
    1、服务器安装dockeryuminstalldocker-y12、修改配置文件方法1:编辑`/etc/sysconfig/docker`文件修改`OPTION`选项添加--insecure-registry=10.0.17.205:4000方法2......
  • 微软通用的DBHelper
    ​​viewplain​​1.usingSystem;2.3.usingSystem.Configuration;4.5.usingSystem.Data;6.7.usingSystem.Data.Common;8.9.usingSystem......
  • framelesshelper框架学习
    目录​​1 运行成功​​​​2 cmakelist.txt适配qt6.5+cmake3.19修改​​​​3 代码学习​​​​4 参考链接​​一套框架,可以生成多平台的无边框窗体,拖动到屏幕底边......
  • Spring Cloud Schema Registry
    介绍当组织具有基于消息传递的发布/订阅体系结构并且多个创建者和使用者微服务相互通信时,所有这些微服务通常需要就基于架构的协定达成一致。当这样的架构需要发展以适应新......
  • Docker私服(Registry)
    DockerRegistry安装#拉取镜像dockerpullregistry#创建文件夹mkdir-p/var/my_registry#启动容器dockerrun-d--namedocker-registry-p5000:5000-v/var/......
  • Spring Boot中使用分页插件PageHelper
    分页一、分页原理页码:pageNumpageNum从1开始,比如第1页、第2页......每页显示的记录条数:pageSize实际上每一次在进行分页请求发送的时候,都是要发送两个数据的:......