新系统环境下读写注册表

       当前的主流系统以及不再是winXP系统,大部分都已经是win7,win8,而且都是64位的。之前一直用来读写注册表的函数 GetProfileString WriteProfileString,现在已经不像之前用的那么方便了,有些限制了。

       如果只是读写INI文件,当然是自定义路径,直线使用原来的函数还是可以的。我也一直这么用的,没看到有什么问题。但是今天遇到读写注册表的事情时,发现不容易实现了。查阅资源,发现如下内容:


该函数有两个同名原型,一个是windows API,一个是CWinApp的成员函数。
作为API的情况,MSDN的说明如下:
The WriteProfileString function copies a string into the specified section of the Win.ini file. If Win.ini uses Unicode characters, the function writes Unicode characters to the file. Otherwise, the function writes ANSI characters.
Note This function is provided only for compatibility with 16-bit versions of Windows. Applications should store initialization information in the registry.
BOOL WriteProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpString
);
总的来说,也就是,这个函数在32位机以后不再使用,存在的目的只是为了与16位机兼容。
作为CWinApp成员函数的情况:
MSDN说明:
Call this member function to write the specified string into the specified section of the application's registry or .INI file.
BOOL WriteProfileString(
LPCTSTR lpszSection,
LPCTSTR lpszEntry,
LPCTSTR lpszValue);
简而言之,就是说这个函数写的内容可能是在注册表中,也可能是在.ini文件中,那么究竟怎么判断呢?
先来看一个函数MSDN说明:
Causes application settings to be stored in the registry instead of INI files.
void SetRegistryKey(
LPCTSTR lpszRegistryKey
);
void SetRegistryKey(
UINT nIDRegistryKey
);
翻译过来很明了,这个函数就是用来控制程序初始化信息时存储位置的,是注册表,或者.ini文件。
如果想存到注册表中,就先调用一下这个函数就OK了,默认是存储在.ini文件中的。那么问题又来了,这个.ini文件存在什么地方?又叫什么名字呢?
调试跟踪到CWinApp::WriteProfileString中间,发现了这个函数:return ::WritePrivateProfileString(lpszSection, lpszEntry, lpszValue,m_pszProfileName);
然后根据m_pszProfileName的名字RPT.ini(因为我的应用程序名字为RPT.exe)查找,最后在C:\WINDOWS下查找到了RPT.ini的文件,打开看下,结果不错。


以上为网络转载内容,说的很明白了。我是只用了CWinApp::WriteProfileString,就可以成功实现。实现时调用AfxGetApp()->GetProfileString,就可以了,但是注意GetProfileString函数和API函数的参数有变化,读取出来的值是返回值,第三个参数是默认值,查阅MSDN,即可明了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章