GetPrivateProfileSectionNames
The GetPrivateProfileSectionNames function retrieves the names of all sections 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.
获取ini文件的section。
DWORD GetPrivateProfileSectionNames(
LPTSTR lpszReturnBuffer, // address of return buffer
DWORD nSize, // size of return buffer
LPCTSTR lpFileName // address of initialization filename
);
Parameters
-
lpszReturnBuffer
- Pointer to a buffer that receives the section names associated with the named file. The buffer is filled with one or more null-terminated strings; the last string is followed by a second null character.指向一个缓冲,用来保存返回的所有section。 nSize
- Specifies the size, in characters, of the buffer pointed to by the lpszReturnBuffer parameter. lpszReturnBuffer的大小。 lpFileName
- Pointer to a null-terminated string that names the initialization file. If this parameter is NULL, the function searches the WIN.INI file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.读取的ini文件名。
Return Values
The return value specifies the number of characters copied to the specified buffer, not including the terminating null character. If the buffer is not large enough to contain all the section names associated with the specified initialization file, the return value is equal to the length specified by nSize minus two.装载到lpszReturnBuffer
缓冲区的字符数量。
Remarks
This operation is atomic; no updates to the initialization file are allowed while the section names are being copied to the buffer.
Calls to profile functions might be mapped to the registry instead of to the initialization files. When the operation has been mapped, the GetPrivateProfileSectionNames function retrieves information from the registry, not from the initialization file; the change in the storage location has no effect on the function's behavior.
e.g:
#include "stdafx.h"
#include "GetPrivateProfileSectionName.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[1024];
char strVaule[256];
int iReturnSize = 0;
int iCountTotal = 0;
int iCountPrivate = 0;
int iBufSize = 1 * 1024;
memset(strRetunrStringBuf, 0, sizeof(strRetunrStringBuf));
iReturnSize = GetPrivateProfileSectionNames(strRetunrStringBuf,
iBufSize,
csFileName);
while(iCountTotal < iReturnSize)
{
memset(strVaule, 0, sizeof(strVaule));
memcpy(strVaule, &strRetunrStringBuf[iCountTotal],strlen(&strRetunrStringBuf[iCountTotal]));
iCountPrivate = strlen(&strRetunrStringBuf[iCountTotal]) + 1;
iCountTotal += iCountPrivate;
printf("%s\n", strVaule);
}
}
return nRetCode;
}