GetPrivateProfileString &&WritePrivateProfileString 调用系统读写

来源:互联网 发布:h3c1024交换机端口镜像 编辑:程序博客网 时间:2024/05/17 14:15

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:

原创粉丝点击