欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

GetPrivateProfileString

程序员文章站 2022-06-12 09:02:01
...

GetPrivateProfileString

The GetPrivateProfileString function retrieves a string from the specified section in an initialization file. This function is provided for compatibility with 16-bit Windows-based applications. Win32-based applications should store initialization information in the registry.

DWORD GetPrivateProfileString(
  LPCTSTR lpAppName,        // points to section name不区分大小写
  LPCTSTR lpKeyName,        // points to key name不区分大小写
  LPCTSTR lpDefault,        // points to default string
  LPTSTR lpReturnedString,  // points to destination buffer
  DWORD nSize,              // size of destination buffer
  LPCTSTR lpFileName        // points to initialization filename
);
 

Parameters

lpAppName
Pointer to a null-terminated string that specifies 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
Pointer to the null-terminated string containing the key name 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
Pointer to a null-terminated default string. If the lpKeyName key cannot be found in the initialization file, GetPrivateProfileString copies the default string to the lpReturnedString buffer. This parameter cannot be NULL.

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 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.

Windows NT: The system does not modify the lpDefault string. This means that if the default string contains trailing blanks, the lpReturnedString and lpDefault strings will not match when compared using the lstrcmp function.

lpReturnedString
Pointer to the buffer that receives the retrieved string.
nSize
Specifies the size, in characters, of the buffer pointed to by the lpReturnedString parameter.
lpFileName
Pointer to a null-terminated string that names 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 Values

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. 

e.g:

#include "stdafx.h"
#include "GetPrivateProfileString.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		// TODO: code your application's behavior here.
		CString csFileName = "E:\\VC++\\GetPrivateProfileSection\\sention.ini";
		char strRetunrStringBuf[128];
		int iReturnSize = 0;
		CString csValue;
		memset(strRetunrStringBuf, 0, sizeof(strRetunrStringBuf));

		iReturnSize = GetPrivateProfileString("PYTHON","anguage","NotFound",strRetunrStringBuf,128,csFileName);
		cout <<"iReturnSize: " << iReturnSize << endl;
		cout << "strRetunrStringBuf: " << strRetunrStringBuf << endl;

		iReturnSize = GetPrivateProfileString("PYTHON","author","NotFound",strRetunrStringBuf,128,csFileName);
		cout <<"iReturnSize: " << iReturnSize << endl;
		cout << "strRetunrStringBuf: " << strRetunrStringBuf << endl;

		iReturnSize = GetPrivateProfileString("python","YEAR","NotFound",strRetunrStringBuf,128,csFileName);
		cout <<"iReturnSize: " << iReturnSize << endl;
		cout << "strRetunrStringBuf: " << strRetunrStringBuf << endl;

		iReturnSize = GetPrivateProfileString("PYTHON","price","NotFound",strRetunrStringBuf,128,csFileName);
		cout <<"iReturnSize: " << iReturnSize << endl;
		cout << "strRetunrStringBuf: " << strRetunrStringBuf << endl;

		iReturnSize = GetPrivateProfileString("PYTHON","666","NotFound",strRetunrStringBuf,128,csFileName);
		cout <<"iReturnSize: " << iReturnSize << endl;
		cout << "strRetunrStringBuf: " << strRetunrStringBuf << endl;
	}

	return nRetCode;
}
GetPrivateProfileString