WriteProfileString的问题

本文介绍了Windows API中的WriteProfileString函数及其在CWinApp中的应用。解析了该函数如何将字符串写入应用程序的注册表或.ini文件,并通过实例展示了.ini文件的默认存储位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该函数有两个同名原型,一个是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的文件,打开看下,结果不错。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值