我总结了一些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提供的这个类的时候,我们要首先在你的头文件(.h)或者是源文件(.cpp)里面包含atlbase.h,也就是添加上一句:#include <atlbase.h>
我们这里是建立了一个SetReg成员,然后通过SetReg.Open()和SetReg.SetValue()来打开和设置相应的键值,最后我们调用SetReg.Close()来释放句柄。
CWinApp::SetRegistryKey Causes application settings to be stored in the registry instead of INI files. void SetRegistryKey( ParameterslpszRegistryKey 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 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文件的函数就被映射到进行读写注册表了:
大家可以看到,本例子是在InitInstance()事件中加入了SetRegistryKey(_T("ZIEMONITOR")),这个代表什么意思呢? 意思就是说,我们将会在HKEY_CURRENT_USER//software//项下面创建一个名为 ZIEMONITOR 的分支,说他是准备创建,是因为,如果我们不调用上面说过的6个函数的话,就不会创建。 下面是调用相应的函数来读写注册表的代码:
这里用WriteProfileString和WriteProfileInt来对注册表进行了写入操作 这里呢,是用GetProfileInt和GetProfileString对注册表进行了读取操作 |