GetPrivateProfileString &&WritePrivateProfileString 调用系统读写

这两个函数主要用于与16位Windows应用兼容,但现在推荐使用注册表存储初始化信息。GetPrivateProfileString从指定段中获取字符串,WritePrivateProfileString将字符串写入初始化文件的指定段。若提供的缓冲区太小,字符串会被截断并添加空字符。函数失败或刷新缓存时返回值为0,成功则非0。

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

GetPrivateProfileString Function

Retrieves a string from the specified section in an initialization file.

Note  This function is provided only for compatibility with 16-bit Windows-based applications. Applications should store initialization information in the registry.

DWORD WINAPI GetPrivateProfileString(
  __in          LPCTSTR lpAppName,
  __in          LPCTSTR lpKeyName,
  __in          LPCTSTR lpDefault,
  __out         LPTSTR lpReturnedString,
  __in          DWORD nSize,
  __in          LPCTSTR lpFileName
);
Parameters
lpAppName

The name of the section containing the key name. If this parameter is NULL, the GetPrivateProfileString function copies all section names in the file to the supplied buffer.

lpKeyName

The name of the key whose associated string is to be retrieved. If this parameter is NULL, all key names in the section specified by the lpAppName parameter are copied to the buffer specified by the lpReturnedString parameter.

lpDefault

A default string. If the lpKeyName key cannot be found in the initialization file, GetPrivateProfileString copies the default string to the lpReturnedString buffer. If this parameter is NULL, the default is an empty string, "".

Avoid specifying a default string with trailing blank characters. The function inserts a null character in the lpReturnedString buffer to strip any trailing blanks.

Windows Me/98/95:  Although  lpDefault is declared as a constant parameter, the system strips any trailing blanks by inserting a null character into the  lpDefault string before copying it to the  lpReturnedString buffer.
lpReturnedString

A pointer to the buffer that receives the retrieved string.

Windows Me/98/95:  The string cannot contain control characters (character code less than 32). Strings containing control characters may be truncated.
nSize

The size of the buffer pointed to by the lpReturnedString parameter, in characters.

lpFileName

The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.

Return Value

The return value is the number of characters copied to the buffer, not including the terminating null character.

If neither lpAppName nor lpKeyName is NULL and the supplied destination buffer is too small to hold the requested string, the string is truncated and followed by a null character, and the return value is equal to nSize minus one.

If either lpAppName or lpKeyName is NULL and the supplied destination buffer is too small to hold all the strings, the last string is truncated and followed by two null characters. In this case, the return value is equal to nSize minus two.




WritePrivateProfileString Function

Copies a string into the specified section of an initialization file.

Note  This function is provided only for compatibility with 16-bit versions of Windows. Applications should store initialization information in the registry.

BOOL WINAPI WritePrivateProfileString(
  __in          LPCTSTR lpAppName,
  __in          LPCTSTR lpKeyName,
  __in          LPCTSTR lpString,
  __in          LPCTSTR lpFileName
);
Parameters
lpAppName

The name of the section to which the string will be copied. If the section does not exist, it is created. The name of the section is case-independent; the string can be any combination of uppercase and lowercase letters.

lpKeyName

The name of the key to be associated with a string. If the key does not exist in the specified section, it is created. If this parameter is NULL, the entire section, including all entries within the section, is deleted.

lpString

A null-terminated string to be written to the file. If this parameter is NULL, the key pointed to by the lpKeyName parameter is deleted.

Windows Me/98/95:  The system does not support the use of the TAB (\t) character as part of this parameter.
lpFileName

The name of the initialization file.

If the file was created using Unicode characters, the function writes Unicode characters to the file. Otherwise, the function writes ANSI characters.

Return Value

If the function successfully copies the string to the initialization file, the return value is nonzero.

If the function fails, or if it flushes the cached version of the most recently accessed initialization file, the return value is zero. To get extended error information, callGetLastError.

Remarks

A section in the initialization file must have the following form:

[section]
key=string
      .
      .
      .

If the lpFileName parameter does not contain a full path and file name for the file, WritePrivateProfileString searches the Windows directory for the file. If the file does not exist, this function creates the file in the Windows directory.

If lpFileName contains a full path and file name and the file does not exist, WritePrivateProfileString creates the file. The specified directory must already exist.

The system keeps a cached version of the most recent registry file mapping to improve performance. If all parameters are NULL, the function flushes the cache. While the system is editing the cached version of the file, processes that edit the file itself will use the original file until the cache has been cleared.

The system maps most .ini file references to the registry, using the mapping defined under the following registry key:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值