Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Reading all section names from ini file.

I am getting the first section name alone when i use GetPrivateProfileSectionNames. How can i get the next section name ? Please help

C++
// reading each section from ini file

  LPWSTR pSecNames= new WCHAR[1024];

  int nSectionNum = 0;
  wstring strSecName;

  DWORD retVal = GetPrivateProfileSectionNames(pSecNames,1024,profileFilePath.c_str());

  if (retVal)
  {
    strSecName.assign(pSecNames,retVal);
  }
  delete pSecNames;

  string sectionName = ToString(strSecName.c_str(),CP_UTF8);
Posted
Updated 17-Sep-22 13:35pm

From MSDN:
TXT
lpszReturnBuffer [out]
A 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.

So, if your INI file is:
[Hello]
a=1
b=2
[World]
c=3
d=4

pSecNames will be "Hello\0World\0\0", and retVal will be 13.
You'll need some kind of loop to read them all:

C++
// This is pseudo-code, I didn't compile it.
std::list<std::wstring> l;
for (wchar_t * p = pSecNames; *p; ++p)
{
    std::wstring ws(p);
    l.push_back(ws);
    p += ws.size();
    ASSERT(0 == *p);
}


Hope this helps,

Pablo.
 
Share this answer
 
v2
Comments
infant_coder 22-Mar-12 3:03am    
Hi Pablo,

This is my ini file
[General]
StartWithLastProfile=1

[Profile0]
Name=default
IsRelative=1
Path=Profiles/sg3i9urv.default
Default=1

I am able to read only "General". I cannot get the next section name "Profile0". Pls help
Pablo Aliskevicius 22-Mar-12 8:55am    
You should get a string with "General\0Profile0\0\0". So, pSecNames + 9 should point to "Profile0".
C++
LPTSTR lpszReturnBuffer;     
lpszReturnBuffer = new TCHAR[MAX];     
char* pNextSection = NULL;        
GetPrivateProfileSectionNames(lpszReturnBuffer,MAX,IniPath);     
pNextSection = lpszReturnBuffer;     
printf("%s\n", pNextSection);           
CString csAllSections;
while (*pNextSection != 0x00)     
{         
    pNextSection = pNextSection + strlen(pNextSection) + 1;         
    if(*pNextSection != 0x00)         
    {             
        csAllSections += pNextSection;
    }     
} 



csAllSections will have all sections name. but not separted. You have to handle that logic
 
Share this answer
 
Good soln, but minor mistake must be corrected as below in order to catch from the
first section name.

.... same as above

//can use vector<string> to place strings
vector<string> vsAllSections;

while (*pNextSection != 0x00)     
{         
    vsAllSections.push_back(pNextSection);
    pNextSection = pNextSection + strlen(pNextSection) + 1;         
} 
 
Share this answer
 
v2
Comments
Richard MacCutchan 4-Jun-20 9:15am    
That solution is eight years old. I doubt that it has much relevance any more.
CHill60 4-Jun-20 9:46am    
I had to do a lot of scrolling up and down to try to work out which solution you are referring to (I'm still not sure). If you are going to comment on a solution then either use the "Have a Question or Comment?" link next to it - which means the poster will be informed, or provide the full solution in your own post. At the very least mention which solution you are referring to!
char * lpszReturnBuffer;
lpszReturnBuffer = new char[MAX];
int iCnt = GetPrivateProfileSectionNames(lpszReturnBuffer,MAX,strFileName.c_str());  

for(int i=0;i<iCnt;i++)
{
  if(lpszReturnBuffer[i] == '\0') //find null char
  {
    lpszReturnBuffer[i] = '^';//and replace it to separator.. whatever you want...
  }
}
	
string strT = lpszReturnBuffer;//convert to string
string strR;
iPosEnd = strT.find('^');//find first separator
while(iPosEnd != string::npos)
{
  strR = strT.substr(iPosStart, iPosEnd-iPosStart);//sub string..
  cb->AddString(strR.c_str());//this is my combobox.. replace code whatever you want....
  iPosStart = iPosEnd+1;
  iPosEnd = strT.find('^',iPosStart);
}
 
Share this answer
 
Comments
CHill60 24-Jun-20 0:08am    
If you are going to resurrect old questions with a new solution please add some commentary (words) to explain why your solution is better than the one the questioner accepted as the solution 8 years ago.
Member 13258320 24-Jun-20 1:19am    
Nothing better.... just the other way I understand....
vector<string> IniGetSections(string path)
{
	// converting string to widestring, since the GetPrivateProfile use widestrings
	wstring wpath = wstring(path.begin(), path.end());

	TCHAR csSections[1024]; // buffer for sections string return
	GetPrivateProfileSectionNames(csSections, sizeof(csSections) / sizeof(csSections[0]), wpath.c_str()); // calling the function from windows.h

	vector<string> Sections;             // vector to return
	TCHAR* pCurrentSection = csSections; // pointer to first wchar
	while (*pCurrentSection != 0x00)     // loop until double 0x00
	{
		wstring wstrval(pCurrentSection);                           // converting wchar[] to wstring
		Sections.push_back(string(wstrval.begin(), wstrval.end())); // converting wstring to string and adding section name to return vector
		pCurrentSection += wcslen(pCurrentSection) + 1;             // shifting pointer to the first symbol of the next section
	}

	return Sections; // result string vector
}
 
Share this answer
 
v2
Comments
CHill60 22-Jul-22 12:50pm    
A code dump without commentary is just that - a dump. It does not make for a solution
Member 15714669 22-Jul-22 16:12pm    
Added a cooments
This is my first post here ))
I was looking for such solution, found it here and make a function with approach found, then shared here the result.
thank you for the feedback )
It works in MSVSC 2019
wchar_t buf[1024];
DWORD r = GetPrivateProfileSectionNamesW(buf, 1024, LR"(C:\Windows\win.ini)");
wchar_t *p = buf;
while (*p) p += wprintf(L"%s\n", p);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900