MFC API——》 SetRegistryKey
2014-04-10 09:16
295 查看
SetRegistryKey导致CWinApp::WriteProfileInt等操作注册表而不是INI文件
在利用mfc框架的时候,在App应用类的InitInstance()函数中,初始化时总有一个 SetRegistryKey("字符串XXX"),不知道究竟有何用处,这天仔细查看了一下,发现如果你使用注册表,则它为你提供了很便利的方法,当然如果不使用系统注册表,这句可以注释掉。以下是我在网上找到的一些资料:
SetRegistryKey | Causes application settings to be stored in the registry instead of .INI files. |
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. |
用VC++的向导建立MFC项目之后,在InitInstance中可以看到这样的语句:
SetRegistryKey(_T("应用程序向导生成的本地应用程序"));
该函数将为以上提到的几个方法建立工作环境,此时如果用WriteProfileInt写入数据,将会
被写入到如下注册表位置:
HKEY_CURRENT_USER\Software\应用程序向导生成的本地应用程序\应用程序名称\
如果在InitInstance中不执行SetRegistryKey,则用WriteProfileInt写入数据时,将写入到
%windir%\应用程序名称.ini中
例子如下:
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");
如果在InitInstance中没执行SetRegistryKey
则对于:
WriteProfileInt("section","val1",10);
将在“%windir%\测试应用程序.ini”中写入:
[section]
val1=10
UINT GetProfileInt(LPCTSTR lpszSection,LPCTSTR lpszEntry,int nDefault);
参数:
lpszSection 指向一个null结尾的字符串,指明包含入口的部分
lpszEntry 指向一个null结尾的字符串,包含了要获取值的入口
nDefault 指明当框架找不到入口时的默认返回值
相关文章推荐
- MFC函数—SetRegistryKey
- MFC常见函数详解:SetRegistryKey
- SetRegistryKey
- 过SetRegistrykey的作用
- SetWindowPos cannot overload 7个参数? (SDK API 与 MFC API的区别)
- SetRegistryKey的作用
- SetRegistryKey函数
- SetRegistryKey(_T("Local AppWizard-Generated Applications")); 和 LoadStdProfileSettings
- SetRegistryKey
- Please set registry key HKLM\Microsoft\.NET Framework\InstallRoot to point to the .NET Framework
- SetRegistryKey的作用
- RegistryKey.SetValue 方法作为字符串存储整数值
- SetRegistryKey
- SetRegistryKey的作用(转载)
- SetRegistryKey的作用
- SetRegistryKey的作用(转载)
- SetRegistryKey的作用
- SetRegistryKey的作用 [转]
- SetRegistryKey的作用
- SetRegistryKey