MFC注册表操作详解
2010-01-22 10:55
573 查看
MFC注册表操作详解
出处:Zaroty's Blog 前置知识:VC WIN-API MFC 我总结了一些MFC操作注册表,大致分3种方法: 1)WIN-API 这个是当然的了,MFC也是基于WIN-API的,所以我们直接在MFC里面应用WIN-API的注册表操作函数来对操作注册表,是没有一点问题的。WIN-API的注册表操作大家可以去参考这3篇文章。 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 ); ParameterslpszRegistryKeyPointer 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 WriteProfileStringmember 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函数来设置一下注册表项,大家可以看下图: 大家可以看到,本例子是在InitInstance()事件中加入了SetRegistryKey(_T("ZIEMONITOR")),这个代表什么意思呢? 意思就是说,我们将会在HKEY_CURRENT_USER//software//项下面创建一个名为 ZIEMONITOR 的分支,说他是准备创建,是因为,如果我们不调用上面说过的6个函数的话,就不会创建。 下面是调用相应的函数来读写注册表的代码: 这里用WriteProfileString和WriteProfileInt来对注册表进行了写入操作 这里呢,是用GetProfileInt和GetProfileString对注册表进行了读取操作 这里需要说明的就是,这些函数都属于CWinApp类,所以如果你的程序不是CWinApp的派生类,你就要在前面加上theApp或者AfxGetApp() 。 大家可以感觉到,这种方法来操作注册表有一定的局限性,但是用来实现保存设置到注册表的话,也是绰绰有余的。 |
相关文章推荐
- MFC 文件操作详解
- MFC的注册表操作—CRegKey类的使用
- MFC操作注册表
- 编程实现mscomm32写入注册表,解决写入值乱码问题,mfc中操作注册表操作。
- MFC 操作注册表
- MFC对注册表的操作
- MFC 文件操作详解
- vbs注册表操作详解
- MFC注册表操作
- vs2008环境下MFC对注册表的读写操作
- C#操作注册表的方法详解
- MFC操作--数据输出、注册表、文件统计和获取上级目录
- MFC 文件操作详解
- MFC注册表操作
- MFC数组类CArray的使用的操作详解
- Reg命令使用详解 批处理操作注册表必备
- VC++/MFC操作ini配置文件详解
- MFC数组类CArray的使用的操作详解[转]
- MFC学习笔记之:列表框(ListBox) 常用的的操作详解
- 关于开机启动运行的注册表操作(MFC)