Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GetPrivateProfileSection & ..., any easier method than mine?

Status
Not open for further replies.

Karl Blessing

Programmer
Feb 25, 2000
2,936
US
This is what I have written so far, it does get back all the sections, and their values below them, which I soon hope to put each section into a collection, then the items under them. But I was curious, is there a possible shorter method than the one I have going? <br><br>Option Explicit <br><br><br>' From the IniFile class. <br>Private Declare Function GetPrivateProfileSectionNames _ <br>Lib &quot;kernel32&quot; Alias &quot;GetPrivateProfileSectionNamesA&quot; _ <br>(ByVal lpReturnBuffer As String, ByVal nSize As Long, _ <br>ByVal lpName As String) As Long <br><br>' From the IniFile class. <br>Private Declare Function GetPrivateProfileSection _ <br>Lib &quot;kernel32&quot; Alias &quot;GetPrivateProfileSectionA&quot; _ <br>(ByVal lpAppName As String, _ <br>ByVal lpReturnedString As String, ByVal nSize As Long, _ <br>ByVal lpName As String) As Long <br><br>Private Declare Function WritePrivateProfileSection _ <br>Lib &quot;kernel32&quot; Alias &quot;WritePrivateProfileSectionA&quot; _ <br>(ByVal lpAppName As String, ByVal lpString As String, _ <br>ByVal lpName As String) As Long <br><br>' From the IniSection class. <br>Private Declare Function WritePrivateProfileString _ <br>Lib &quot;kernel32&quot; Alias &quot;WritePrivateProfileStringA&quot; _ <br>(ByVal lpApplicationName As String, _ <br>ByVal lpKeyName As Any, ByVal lpString As Any, _ <br>ByVal lpName As String) As Long <br><br><br>Public Sub ReadData() <br>Dim strSections As String <br>Dim lngSize As Long <br>Dim astrSections As Variant <br>Dim astrItems As Variant <br>Dim i As Integer <br>Dim Items As Variant <br><br>On Error GoTo HandleErrors <br>Form1.Label1.Caption = &quot;&quot; <br>' In most cases, 1024 characters is enough, but if it's <br>' not, the code will double that and try again. <br>lngSize = 1024 <br>Do <br>strSections = Space$(lngSize) <br>lngSize = GetPrivateProfileSectionNames(strSections, lngSize, &quot;C:\kbstuff\inistuff\Test.ini&quot;) <br>If lngSize = 0 Then <br>' No sections, so get out of here! <br>GoTo ExitHere <br>ElseIf lngSize = Len(strSections) - 2 Then <br>' That's how the API indicates you didn't allow <br>' enough space, but returning the size you originally <br>' specified, less 2. In that case, just double the <br>' buffer, and try again. <br>lngSize = lngSize * 2 <br>Else <br>' Trim the extra stuff. Use lngSize - 1 because <br>' there's an extra vbNullChar at the end of this <br>' string. <br>strSections = Left$(strSections, lngSize - 1) <br>Exit Do <br>End If <br>Loop <br>' Now strSections contains the section names, separated <br>' with vbNullChar. <br>astrSections = Split(strSections, vbNullChar) <br>For i = LBound(astrSections) To UBound(astrSections) - 1 <br>' Add the section to the collection, indicating that <br>' it's not a NEW section. That is, it's not being added <br>' after the file was read. That way, the code there can <br>' know to not bother looking for items when being added <br>' by code later. <br><br>Form1.Label1.Caption = Form1.Label1.Caption & astrSections(i) & vbCrLf <br>astrItems = GetValues(astrSections(i)) <br>For Each Items In astrItems <br>Form1.Label1.Caption = Form1.Label1.Caption & &quot; &quot; & Items & vbCrLf <br>Next <br><br>'Call AddSection(astrSections(i), False) <br>Next i <br><br>ExitHere: <br>Exit Sub <br><br>HandleErrors: <br>Err.Raise Err.Number, Err.Source, Err.Description <br>End Sub <br><br><br>Public Function GetValues(section As Variant) As Variant <br>Dim strSections As String <br>Dim lngSize As Long <br>Dim astrSections As Variant <br>Dim i As Integer <br><br>On Error GoTo HandleErrors <br><br>' In most cases, 1024 characters is enough, but if it's <br>' not, the code will double that and try again. <br>lngSize = 1024 <br>Do <br>strSections = Space$(lngSize) <br>lngSize = GetPrivateProfileSection(section, strSections, lngSize, &quot;C:\kbstuff\inistuff\Test.ini&quot;) <br>If lngSize = 0 Then <br>' No sections, so get out of here! <br>GoTo ExitHere <br>ElseIf lngSize = Len(strSections) - 2 Then <br>' That's how the API indicates you didn't allow <br>' enough space, but returning the size you originally <br>' specified, less 2. In that case, just double the <br>' buffer, and try again. <br>lngSize = lngSize * 2 <br>Else <br>' Trim the extra stuff. Use lngSize - 1 because <br>' there's an extra vbNullChar at the end of this <br>' string. <br>strSections = Left$(strSections, lngSize - 1) <br>Exit Do <br>End If <br>Loop <br>' Now strSections contains the section names, separated <br>' with vbNullChar. <br>astrSections = Split(strSections, vbNullChar) <br><br>GetValues = astrSections <br><br>ExitHere: <br>Exit Function <br><br>HandleErrors: <br>Err.Raise Err.Number, Err.Source, Err.Description <br>End Function <br> <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
this is my new Module code after adding in some collections (Ini is the main holder of 'Section' which holds a bunch of 'Items')<br><br><FONT FACE=monospace><br>Option Explicit<br><br>Public Ini As New IniFile<br><br>Private Declare Function GetPrivateProfileSectionNames _<br>&nbsp;&nbsp;Lib &quot;kernel32&quot; Alias &quot;GetPrivateProfileSectionNamesA&quot; _<br>&nbsp;&nbsp;(ByVal lpReturnBuffer As String, ByVal nSize As Long, _<br>&nbsp;&nbsp;ByVal lpName As String) As Long<br><br>Private Declare Function GetPrivateProfileSection _<br>&nbsp;&nbsp;Lib &quot;kernel32&quot; Alias &quot;GetPrivateProfileSectionA&quot; _<br>&nbsp;&nbsp;(ByVal lpAppName As String, _<br>&nbsp;&nbsp;ByVal lpReturnedString As String, ByVal nSize As Long, _<br>&nbsp;&nbsp;ByVal lpName As String) As Long<br><br>Private Declare Function WritePrivateProfileSection _<br>&nbsp;&nbsp;Lib &quot;kernel32&quot; Alias &quot;WritePrivateProfileSectionA&quot; _<br>&nbsp;&nbsp;(ByVal lpAppName As String, ByVal lpString As String, _<br>&nbsp;&nbsp;ByVal lpName As String) As Long<br><br>Private Declare Function WritePrivateProfileString _<br>&nbsp;&nbsp;Lib &quot;kernel32&quot; Alias &quot;WritePrivateProfileStringA&quot; _<br>&nbsp;&nbsp;(ByVal lpApplicationName As String, _<br>&nbsp;&nbsp;ByVal lpKeyName As Any, ByVal lpString As Any, _<br>&nbsp;&nbsp;ByVal lpName As String) As Long<br><br><br>Public Sub ReadData()<br>&nbsp;&nbsp;Dim strSections As String, lngSize As Long, astrSections As Variant, astrItems As Variant, i As Integer, Items As Variant<br>&nbsp;&nbsp;Dim ptrSec As New Section<br>&nbsp;&nbsp;Dim ptrItm As New Items<br><br>&nbsp;&nbsp;On Error GoTo HandleErrors<br>&nbsp;&nbsp;&nbsp;Form1.Label1.Caption = &quot;&quot;<br>&nbsp;&nbsp;lngSize = 1024<br>&nbsp;&nbsp;Do<br>&nbsp;&nbsp;&nbsp;&nbsp;strSections = Space$(lngSize)<br>&nbsp;&nbsp;&nbsp;&nbsp;lngSize = GetPrivateProfileSectionNames(strSections, lngSize, &quot;C:\kbstuff\inistuff\Test.ini&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;If lngSize = 0 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GoTo ExitHere<br>&nbsp;&nbsp;&nbsp;&nbsp;ElseIf lngSize = Len(strSections) - 2 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lngSize = lngSize * 2<br>&nbsp;&nbsp;&nbsp;&nbsp;Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strSections = Left$(strSections, lngSize - 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exit Do<br>&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;Loop<br>&nbsp;&nbsp;astrSections = Split(strSections, vbNullChar)<br>&nbsp;&nbsp;For i = LBound(astrSections) To UBound(astrSections) - 1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ptrSec.Name = astrSections(i)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;astrItems = GetValues(astrSections(i))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;For Each Items In astrItems<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ptrItm.Name = Items<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ptrSec.Items.Add ptrItm, ptrSec.Name & ptrItm.Name<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set ptrItm = New Items<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Next<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ini.Section.Add ptrSec, ptrSec.Name<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set ptrSec = New Section<br>&nbsp;&nbsp;Next i<br>ExitHere:<br>&nbsp;&nbsp;Exit Sub<br><br>HandleErrors:<br>&nbsp;&nbsp;Err.Raise Err.Number, Err.Source, Err.Description<br>End Sub<br><br><br>Public Function GetValues(Section As Variant) As Variant<br>&nbsp;&nbsp;Dim strSections As String<br>&nbsp;&nbsp;Dim lngSize As Long<br>&nbsp;&nbsp;Dim astrSections As Variant<br>&nbsp;&nbsp;Dim i As Integer<br>&nbsp;&nbsp;On Error GoTo HandleErrors<br>&nbsp;&nbsp;lngSize = 1024<br>&nbsp;&nbsp;Do<br>&nbsp;&nbsp;&nbsp;&nbsp;strSections = Space$(lngSize)<br>&nbsp;&nbsp;&nbsp;&nbsp;lngSize = GetPrivateProfileSection(Section, strSections, lngSize, &quot;C:\kbstuff\inistuff\Test.ini&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;If lngSize = 0 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GoTo ExitHere<br>&nbsp;&nbsp;&nbsp;&nbsp;ElseIf lngSize = Len(strSections) - 2 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lngSize = lngSize * 2<br>&nbsp;&nbsp;&nbsp;&nbsp;Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strSections = Left$(strSections, lngSize - 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Exit Do<br>&nbsp;&nbsp;&nbsp;&nbsp;End If<br>&nbsp;&nbsp;Loop<br>&nbsp;&nbsp;astrSections = Split(strSections, vbNullChar)<br>&nbsp;&nbsp;GetValues = astrSections<br>ExitHere:<br>&nbsp;&nbsp;Exit Function<br>HandleErrors:<br>&nbsp;&nbsp;Err.Raise Err.Number, Err.Source, Err.Description<br>End Function<br><br>Public Sub CleanUpIni()<br><br>Dim Num As Integer<br>Dim TmpSec As Section, TmpItm As Items<br><br>For Each TmpSec In Ini.Section<br>&nbsp;&nbsp;&nbsp;&nbsp;For Each TmpItm In TmpSec.Items<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TmpSec.Items.Remove TmpSec.Name & TmpItm.Name<br>&nbsp;&nbsp;&nbsp;&nbsp;Next<br>&nbsp;&nbsp;&nbsp;&nbsp;Ini.Section.Remove TmpSec.Name<br>Next<br><br>End Sub<br></font><br><br>Ok so good so far, but I am still in need of being able to make the code much smaller, rather than a bunch of loops, with checks for size. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
Hi Karl,<br><br>I was delighted when I didn't have to do the whole .ini file thing anymore.<br><br>I just use <FONT FACE=monospace><b>GetSetting()</font></b> and <FONT FACE=monospace><b>SaveSetting()</font></b> which read and write the registry nice and safely.<br><br>Is there some reason you can't use the Registry?<br><br> <p>Mike<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>Please don't send me email questions without posting them in Tek-Tips as well. Better yet -- Post the question in Tek-Tips and send me a note saying "Have a look at so-and-so in the thingy forum would you?"
 
Because you dont ship registry settings on a distributed instalation file, its better to keep the instalation/upgrade setting in a file that goes with the shipment, rather than installing something to the registry on every new persons machine, and an ini file is much more modifiable for the WebSaras application developers (I didnt write WebSaras which is a bunch of ASP, ActiveX, etc, basically a web application, I just writing the instalation program) , much easier for them and its much easier for me to debug with tons of data. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
&quot;Because you dont ship registry settings on a distributed instalation file&quot;<br><br>I do. <p>Mike<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= Cargill's Corporate Web Site</a><br>Please don't send me email questions without posting them in Tek-Tips as well. Better yet -- Post the question in Tek-Tips and send me a note saying "Have a look at so-and-so in the thingy forum would you?" My home email is michael.j.lacey@ntlworld.com<br
 
I would, if the other guys wanted to modify their settings that way, but instead they like the idea of using an ini file, easier for them to edit(according to them). <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML,Visual InterDev 6, ASP(WebProgramming), QBasic(least i didnt start with COBOL)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top