SetRegistryKey的作用 [转]

SetRegistryKey的作用 [转]  

2010-04-14 20:02:40|  分类: vc学习|举报|字号 订阅

1)WIN-API

这个是当然的了,MFC也是基于WIN-API的,所以我们直接在MFC里面应用WIN-API的注册表操作函数来对操作注册表,是没有一点问题的。WIN-API的注册表操作大家可以去参考这3篇文章。在利用mfc框架的时候,在App应用类的InitInstance()函数中,初始化时总有一个 SetRegistryKey("字符串XXX"),不知道究竟有何用处,这天仔细查看了一下,发现如果你使用注册表,则它为你提供了很便利的方法,当然如果不使用系统注册表,这句可以注释掉。

以下是我在网上找到的一些资料:

Registry Functions(注册表操作API)

C语言注册表操作实例

Win32_API注册表类的编制以及使用

这3篇文章基本上概括了WIN-API注册表操作的一些方法和实例,大家看过之后应该就可以很顺利的利用API来顺利的操作注册表了。

2)用MFC提供的 CRegKey 类

既然我们是用MFC框架来写程序,那么用MFC封装好的一些类来操作注册表也就理所当然了。

CRegKey类的详细介绍(方法、成员)大家可以点击下面链接查看:CRegKey Class(MFC 注册表操作)

这里我们要注意的是,当我们使用MFC提供的这个类的时候,我们要首先在你的头文件(.h)或者是源文件(.cpp)里面包含atlbase.h,也就是添加上一句:#include <atlbase.h>

下面是我在 Z-IEMONITOR 里面添加了#include <atlbase.h>的地方(.h)

下面我们要做的就是建立一个CRegKey类的成员,来调用CRegKey里面的函数,下面是 Z-IEMONITOR 里面设置开机启动的相关代码:

我们这里是建立了一个SetReg成员,然后通过SetReg.Open()和SetReg.SetValue()来打开和设置相应的键值,最后我们调用SetReg.Close()来释放句柄。

这样,我们就完美的运用CRegKey类来解决了注册表操作的问题。

3)用SetRegistryKey函数

说到这里,我们首先就要讲一下SetRegistryKey函数的作用,通过查询MSDN,我们得到了下面的内容:

CWinApp::SetRegistryKey

Causes application settings to be stored in the registry instead of INI files.

void SetRegistryKey(

      LPCTSTR lpszRegistryKey

);

void SetRegistryKey(

      UINT nIDRegistryKey

);

Parameters

lpszRegistryKey

Pointer to a string containing the name of the key.

nIDRegistryKey

ID/index of a key in the registry.

Remarks

This function sets m_pszRegistryKey, which is then used by the GetProfileInt, GetProfileString, WriteProfileInt, and WriteProfileString member functions of CWinApp. If this function has been called, the list of most recently-used (MRU) files is also stored in the registry. The registry key is usually the name of a company. It is stored in a key of the following form: HKEY_CURRENT_USER\Software\<company name>\<application name>\<section name>\<value name>.

看 了上面的内容,我们知道SetRegistryKey函数的作用就是:将原本应该存放到.ini的程序配置信息,存放到注册表里面。当我们用 SetRegistryKey注册了相应的注册表键值之后,下面的几个原本用于读写.ini文件的函数就被映射到进行读写注册表了:
SetRegistryKey Causes application settings to be stored in the registry instead of .INI files.

SetRegistryKey 这个函数功能是设置MFC程序的注册表访问键,并把读写 ini 文件的成员函数映射到读写注册表。只要调用一下 SetRegistryKey 并指定注册表键值,那么下面6个成员函数,就被映射到进行注册表读取了~

 

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.

MSDN上面写上面6个函数是写到INI文件的。所以俺就忽略了其访问注册表的功能。无意中看了其MFC实现才有所了解。

例子如下:
SetRegistryKey(_T("boli's app")); //这里是准备在注册表HKEY_CURRENT_USER\\software 下面生成一个boli's app 分支~为什么说是准备呢?因为如果不调用相关函数,如上面提到的6个函数,它是不会真正读写注册表的。具体本文最最下面的MFC实现摘录。
CString strUserName,strPassword;
WriteProfileString("LogInfo","UserName",strUserName); //向注册表HKEY_CURRENT_USER\\software\\boli's app\\LogInfo\\分支下写入 UserName 字符串行键值~
WriteProfileString("LogInfo","Password",strPassword);//同上~

 strUserName = GetProfileString("LogInfo","UserName");// 这里是读取HKEY_CURRENT_USER\\software\\boli's app\\LogInfo\\分支下的 UserName 字符串键值到 strUserName~
 strPassword =  GetProfileString("LogInfo","Password");

如果不是在CWinApp 派生的类中读写注册表,可以直接用:
 strUserName = theApp.GetProfileString("LogInfo","UserName");
 strPassword = theApp.GetProfileString("LogInfo","Password");
或 
 strUserName = AfxGetApp()->GetProfileString("LogInfo","UserName");
条条大路通罗马。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值