MFC对注册表的操作

    1 、MFC对注册表操作

    在新建的MFC应用程序的CXXXXXApp::InitInstance()函数中通常都会存在这样一条语句:SetRegistryKey(_T("XXXXXXX"))。

    根据MSDN,SetRegistryKey函数的功能是设置MFC应用程序的注册表访问键,并把读写.ini文件的成员函数映射到读写注册表,只需要调用SetRegistryKey函数设置注册表键值(这个键值通常是Company的名字),那么下面6个成员函数就被映射到注册表读取了!但是有一点需要注意:这个六个函数是CWinApp的成员函数,如果在对话框中调用这6个函数,要写出以下形式: AfxGetApp()->GetProfileString("XXX","XXXX");

WriteProfileBinary  Writes binary data to an entry in the application's .INI file.
WriteProfileInt        Writes an integer to an entry in the application's .INI file.
WriteProfileString  Writes a string to an entry in the application's .INI file.
GetProfileBinary    Retrieves binary data from an entry in the application's .INI file.
GetProfileInt          Retrieves an integer from an entry in the application's .INI file.
GetProfileString    Retrieves a string from an entry in the application's .INI file.

    数据在注册表的存储形式就如下所示:

    HKEY_CURRENT_USER\Software\<company name>\<application name>\<section name>\<value name>. 

    <section name>是字段名,比如我们要存储登录信息,我们就可以建一个字段“LogInfo",然后再往这个字段里面写数据,比如登录名和密码,这些数据是以键值对的形式存放。

     下面上代码:   

     SetRegistryKey(_T("HIT EasyLiu"));//设置注册表键值
CWinApp* pApp = AfxGetApp();     //获取应用程序类,用来调用注册表操作函数

CString strSection       = _T("LogInfo");
CString strStringItem    = _T("UserName");
CString strIntItem       = _T("PassWord");

pApp->WriteProfileString(strSection, strStringItem, _T("EasyLiu")); //往注册表对应字段写入字符串键值对

CString strValue;
strValue = pApp->GetProfileString(strSection, strStringItem); //获取对应字段里面,对应键的键值
ASSERT(strValue == _T("EasyLiu"));                            //判断读取和写入的字符串是否一致

pApp->WriteProfileInt(strSection, strIntItem, 1234);                //往注册表对应字段写入整数

int nValue;
nValue = pApp->GetProfileInt(strSection, strIntItem, 0);
ASSERT(nValue == 1234);
然后查看注册表,快捷键Win+R,然后输入regedit就可以打开注册表,切换到支 HKEY_CURRENT_USER\Software\下面就看到如下结果:其中HIT EasyLiu是我设置的注册表键值,TeeChartTest是MFC应用程序的名字,LogInfo是上面程序里建的字段,说明对注册表读写成功!


    当然,MFC对注册表的操作其实是对Win32 API的封装,对于我们使用者来说只需要直接调用MFC API就行了。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值