\t\t孙鑫 第十二课之四 WriteProfileString / GetProfileString用法

来源:互联网 发布:淘宝页头装修素材 编辑:程序博客网 时间:2024/06/06 08:58

1 说明

WriteProfileString / GetProfileString一种是WIN32 API,另一种是CWinApp的成员函数,因此要分清楚。

WIN32 API的功能是写入win.ini文件,CWinApp成员函数的功能根据OS不同而不同,一般是写入注册表,WIN NT写入注册表,WIN 3.X写入win.ini。  WIN7也是写入注册表。

 

2 WIN32 API

①写入win.ini配置文件

BOOL WriteProfileString(

LPCTSTR   lpAppName,// 段名 sectio name

LPCTSTR   lpKeyName,// 键名 key name

LPCTSTR    lpString//  键值  string to write

);

WIN32 API函数的功能是把上述三个参数写如win.ini文件,该文件在windows7下的位置是X:\windows\win.ini。

eg.

//写win.ini文件

::WriteProfileString("SectionName", "KeyName", "ValueofKey");

写完后效果如下图:

  孙鑫 第十二课之四 WriteProfileString / GetProfileString用法 - 大灰狼 - 大灰狼 的博客

②从win.ini文件中读取指定段和键的值

DWORD GetProfileString(

LPCTSTR    lpAppName,// 段名 section name 

LPCTSTR    lpKeyName,// 键名 key name 

LPCTSTR    lpDefault,//  默认字符串,当指定的段名或键名不存在的情况下把该字符串拷贝至第四个参数 。 default string 

LPTSTR     lpReturnedString,// 读到的键值存在该字符串指针中,如果不能取到则为第三个参数的值。destination buffer

DWORD      nSize// buffer大小。  size of destination buffer 

);
eg.

//从win.ini中读出写入的值

CString str;

::GetProfileString("SectionName", "KeyName", "defaultName", str.GetBuffer(100), 100);

执行后,str=="ValueofKey", 如果SectionName 或 KeyName不存在则str=="defaultName" 。

 

3 CWinApp成员函数

一般写入注册表,根据OS内核分。

①写入

BOOL  WriteProfileString(

LPCTSTR   lpszSection,  //段名,对应注册表中目录

LPCTSTR   lpszEntry,    //入口,对应注册表中的"名称"

LPCTSTR   lpszValue  //键值,对应注册表中的"数据"

);

eg.

WriteProfileString("SectionName", "EntryName", "value");

效果如下:

  孙鑫 第十二课之四 WriteProfileString / GetProfileString用法 - 大灰狼 - 大灰狼 的博客

 

②读出指定段名,指定入口的值

CString  GetProfileString(

LPCTSTR   lpszSection, //段名

LPCTSTR   lpszEntry,     //入口名

LPCTSTR   lpszDefault=NULL  //默认值,如果段名或入口不存在则返回该值

);

eg.

CString str;

str = GetProfileString("SectionName", "EntryName", "defaultValue");

执行后str == "value", 否则str=="defaultValue" 。

 

 

  孙鑫 第十二课之四 WriteProfileString / GetProfileString用法 - 大灰狼 - 大灰狼 的博客?

 

原创粉丝点击