//the buffer for "String" or "SectionNames" return
CString CIniEntry::GetString(LPCTSTR lpIniFileName,LPCTSTR lpSectionName, LPCTSTR lpKeyName)
{
LPCTSTR lpDefault="";
char RetString[MAX_BUFFER]="\0";
LPTSTR lpRetString=RetString;
DWORD dwChar;
dwChar=GetPrivateProfileString(lpSectionName,lpKeyName,lpDefault,lpRetString,
sizeof(RetString),lpIniFileName);
if(!dwChar)
return "";
return (CString)lpRetString;
}
BOOL CIniEntry::WriteString(LPCTSTR lpIniFileName, LPCTSTR lpSectionName, LPCTSTR lpKeyName, LPCTSTR lpString)
{
ASSERT(lpIniFileName);
if(!lpSectionName)
return FALSE;
return(::WritePrivateProfileString(lpSectionName,lpKeyName,lpString,lpIniFileName));
}
CString CIniEntry::EnumSectionNames(LPCTSTR lpIniFileName,char chSpaceSign)
{
ASSERT(lpIniFileName);
if(!lpIniFileName)
return "";
DWORD dwSize;
char tsn[MAX_BUFFER]="\0";
LPTSTR lptsn=tsn;
CString strSn;
dwSize=::GetPrivateProfileSectionNames(lptsn,sizeof(tsn),lpIniFileName);
DWORD dwI=dwSize-2;
if(dwSize)
{
char sn[MAX_BUFFER]="\0";
strncpy(sn,&tsn[0],1);
for(DWORD i=1;i<dwI;i++)
{
if(tsn[i])
strncat(sn,&tsn[i],1);
else
strncat(sn,&chSpaceSign,1);
}
if(dwSize==MAX_BUFFER-2)
AfxMessageBox("Not All of the Sections Returned!",MB_ICONINFORMATION
------解决方案--------------------
MB_OK);
strSn=(CString) sn;
return strSn;
}
return "";
}
CString CIniEntry::GetSection(LPCTSTR lpIniFileName, LPCTSTR lpSectionName,char chSpaceSign)
{
ASSERT(lpIniFileName);
ASSERT(lpSectionName);
if(!lpSectionName
------解决方案--------------------
!lpIniFileName)
return "";
char tkn[MAX_BUFFER]="\0";
LPTSTR lptkn=tkn;
DWORD dwSize,dwI;
CString strSection;
dwSize=::GetPrivateProfileSection(lpSectionName,lptkn,sizeof(tkn),lpIniFileName);
dwI=dwSize-2;
if(dwSize)
{
char kn[MAX_BUFFER]="\0";
strncpy(kn,&tkn[0],1);
for(DWORD i=1;i<dwI;i++)
{
if(tkn[i])
strncat(kn,&tkn[i],1);
else
strncat(kn,&chSpaceSign,1);
}
if(dwSize==MAX_BUFFER-2)
AfxMessageBox("Not All of the Keys Returned!",MB_ICONINFORMATION
------解决方案--------------------
MB_OK);
strSection=(CString) kn;
return strSection;
}
return "";
}
CString CIniEntry::EnumKeys(LPCTSTR lpIniFileName, LPCTSTR lpSectionName,char chSpaceSign)
{
CString strKn;
strKn=GetSection(lpIniFileName,lpSectionName,chSpaceSign);
if(!strKn.IsEmpty())
{
CString kn;
int dwSize=strKn.GetLength();
// Find the first key of the section (before the first "=");
int nEqual=strKn.Find('='); // Index of the "=";
kn=strKn.Left(nEqual);
int nPos=nEqual+1;
//t1=12345,t2=67890,t3=fffff,t4=aaaaa,t5=asss
while((nEqual=strKn.Find('=',nPos))!=-1)
{
kn+=chSpaceSign;
int nSpaceSign=strKn.Find(chSpaceSign,nPos);
kn+=strKn.Mid(nSpaceSign+1,nEqual-nSpaceSign-1);
nPos=nEqual+1;
}
return kn;
}
return "";
}
------解决方案--------------------
#define MAX_BUFFER 1024 //the buffer for "String" or "SectionNames" return CString CIniEntry::GetString(LPCTSTR lpIniFileName,LPCTSTR lpSectionName, LPCTSTR lpKeyName) { LPCTSTR lpDefault=""; char RetString[MAX_BUFFER]="\0"; LPTSTR lpRetString=RetString; DWORD dwChar; dwChar=GetPrivateProfileString(lpSectionName,lpKeyName,lpDefault,lpRetString, sizeof(RetString),lpIniFileName); if(!dwChar) return ""; return (CString)lpRetString; } BOOL CIniEntry::WriteString(LPCTSTR lpIniFileName, LPCTSTR lpSectionName, LPCTSTR lpKeyName, LPCTSTR lpString) { ASSERT(lpIniFileName); if(!lpSectionName) return FALSE; return(::WritePrivateProfileString(lpSectionName,lpKeyName,lpString,lpIniFileName)); } CString CIniEntry::EnumSectionNames(LPCTSTR lpIniFileName,char chSpaceSign) { ASSERT(lpIniFileName); if(!lpIniFileName) return ""; DWORD dwSize; char tsn[MAX_BUFFER]="\0"; LPTSTR lptsn=tsn; CString strSn; dwSize=::GetPrivateProfileSectionNames(lptsn,sizeof(tsn),lpIniFileName); DWORD dwI=dwSize-2; if(dwSize) { char sn[MAX_BUFFER]="\0"; strncpy(sn,&tsn[0],1); for(DWORD i=1;i<dwI;i++) { if(tsn[i]) strncat(sn,&tsn[i],1); else strncat(sn,&chSpaceSign,1); } if(dwSize==MAX_BUFFER-2) AfxMessageBox("Not All of the Sections Returned!",MB_ICONINFORMATION ------解决方案-------------------- MB_OK); strSn=(CString) sn; return strSn; } return ""; } CString CIniEntry::GetSection(LPCTSTR lpIniFileName, LPCTSTR lpSectionName,char chSpaceSign) { ASSERT(lpIniFileName); ASSERT(lpSectionName); if(!lpSectionName ------解决方案-------------------- !lpIniFileName) return ""; char tkn[MAX_BUFFER]="\0"; LPTSTR lptkn=tkn; DWORD dwSize,dwI; CString strSection; dwSize=::GetPrivateProfileSection(lpSectionName,lptkn,sizeof(tkn),lpIniFileName); dwI=dwSize-2; if(dwSize) { char kn[MAX_BUFFER]="\0"; strncpy(kn,&tkn[0],1); for(DWORD i=1;i<dwI;i++) { if(tkn[i]) strncat(kn,&tkn[i],1); else strncat(kn,&chSpaceSign,1); } if(dwSize==MAX_BUFFER-2) AfxMessageBox("Not All of the Keys Returned!",MB_ICONINFORMATION ------解决方案-------------------- MB_OK); strSection=(CString) kn; return strSection;
}
return "";
}
CString CIniEntry::EnumKeys(LPCTSTR lpIniFileName, LPCTSTR lpSectionName,char chSpaceSign)
{
CString strKn;
strKn=GetSection(lpIniFileName,lpSectionName,chSpaceSign);
if(!strKn.IsEmpty())
{
CString kn;
int dwSize=strKn.GetLength();
// Find the first key of the section (before the first "=");
int nEqual=strKn.Find('='); // Index of the "=";
kn=strKn.Left(nEqual);
int nPos=nEqual+1;
//t1=12345,t2=67890,t3=fffff,t4=aaaaa,t5=asss
while((nEqual=strKn.Find('=',nPos))!=-1)
{
kn+=chSpaceSign;
int nSpaceSign=strKn.Find(chSpaceSign,nPos);
kn+=strKn.Mid(nSpaceSign+1,nEqual-nSpaceSign-1);
nPos=nEqual+1;
}
return kn;
}
return "";
}
------解决方案-------------------- 遍历一遍就可以了吧 ------解决方案-------------------- http://download.csdn.net/download/mail_cm/4023315 ------解决方案-------------------- GetPrivateProfileSection/GetPrivateProfileSectionNames ------解决方案-------------------- 如果ini是自己写的,总数还是自己搞个键数统计一下。 API没有直接提供接口,要自己遍历和解析。 ------解决方案--------------------
#define MAX_SECTION 1000//section的最大字长度 wchar_t filetemp[MAX_PATH + 1]; ::GetCurrentDirectory(MAX_PATH,filetemp);//取当前路径 wcscat(filetemp,L"\\setup.ini");//配置文件 wchar_t configStr[MAX_SECTION + 1]; DWORD sectionNum = GetPrivateProfileString(NULL,NULL,L"",configStr,MAX_SECTION,filetemp);//读取所有的section保存,section之间用NULL隔开 int posNum = 0;//标记遍历字符串时NULL的位置 wchar_t *temp = configStr + posNum; for(posNum; posNum < sectionNum;)//configStr以两个NULL作为结束 { m_funcNameList->AddString(temp); posNum += wcslen(temp) + 1; temp = configStr + posNum ; }
代码不好,莫笑