Solved

Ini problem, sections.

Posted on 1997-12-19
1
163 Views
Last Modified: 2012-05-04
I have a problem reading sections in an inifile, I want to read how many, and the
names. I have reached so far yet;


Private Function StripComment$ (ByVal StrIn$)
  Dim nRet%
  'Check for comment
    nRet = InStr(StrIn, ";")

  'Remove it if present
    If nRet = 1 Then
      'Whole string is a comment
        StripComment = ""
        Exit Function
    ElseIf nRet > 1 Then
      'Strip comment
        StrIn = Left$(StrIn, nRet - 1)
    End If
 
  'Trim any trailing space
    StripComment = Trim$(StrIn)
End Function

-----
Sub PrivDeleteSection ()

  'Bail if not initialized
    If Not nmPrivInit Then
      PrivIniNotReg
      Exit Sub
    End If

  'Deletes an *entire* [Section] and all its Entries in Private.Ini
    Dim nRetVal As Integer
    nRetVal = kpWritePrivateProfileString(smSectionName, 0&, 0&, smIniFileName)

  'Now Private.Ini needs to be reinitialized
    smSectionName = ""
    nmPrivInit = False

End Sub

--------

Sub Command3D3_Click ()
  Dim sName, PrivGetSections

  'Store module-level values for future reference
    Dim sSectionName
   
    smSectionName = Trim$(sName)
    smIniFileName = Trim$("tttarot.ini")
    nmPrivInit = True


  'Bail if not initialized
    If Not nmPrivInit Then
      PrivIniNotReg
      Exit Sub
    End If

  'Setup some variables
    Dim sret As String
    Dim sBuff As String
    Dim hfile As Integer

  'Extract all [Section] lines
    hfile = FreeFile
    Open smIniFileName For Input As hfile
    Do While Not EOF(hfile)
      Line Input #hfile, sBuff
      sBuff = StripComment$(sBuff)
      If InStr(sBuff, "[") = 1 And InStr(sBuff, "]") = Len(sBuff) Then
        sret = sret + Mid$(sBuff, 2, Len(sBuff) - 2) + Chr$(0)
      End If
     Loop
   
  'Assign return value
    If Len(sret) Then
      PrivGetSections = sret + Chr$(0)
    Else
      PrivGetSections = String$(2, 0)
    End If

    combo1.AddItem PrivGetSections  'here I try to add sections to a combobox
    Text2.Text = PrivGetSections  'Here I want text2 to give me privgetsections

    Dim ssect, PrivGetSectionsEx
  'Get "normal" list of all [Section]'s
   
    ssect = PrivGetSections
    If Len(ssect) = 0 Then
      PrivGetSectionsEx = 0
     ' Exit Sub
    End If

  'Parse [Section]'s into table
    Dim nentries As Integer
    Dim nNull As Integer
 
    Do While Not EOF(hfile)
      ReDim Preserve sTable(0 To nentries)
      nNull = InStr(ssect, Chr$(0))
      sTable(nentries) = Left$(ssect, nNull - 1)
      ssect = Mid$(ssect, nNull + 1)
      nentries = nentries + 1
    Loop
         
  'Make function assignment
    PrivGetSectionsEx = nentries
    Close hfile

text3.Text = PrivGetSectionsEx  'Also here I try to read how many sections there is
       
End Sub


Can you help on this I will be happy.
Regards
Jacob
0
Comment
Question by:CATT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 5

Accepted Solution

by:
dirtdart earned 60 total points
ID: 1446025
You're on the right track with WritePrivateProfileString.  Now you just need to use GetPrivateProfileString to read the values in.  This will be quite a bit easier that what it seems you're doing.  

Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long


It works in almost exactly the same way as Write does.  lpApplicationName would be your section name (the one enclosed in [square braces]).  lpKeyName is the key name you are looking for.  lpDefault is the default value to be returned if nothing is contained in the key or an error is encountered.  lpReturnedString is the buffer that will receive the data on return.  This buffer needs to be large enough to hold the data before you make the call, so you should set it to lpReturnedString = Space(255) before making the call.  nSize is the size of the buffer (in this case 255).  lpFileName is the name of your ini file.

Upon return, the function will return the size of the data contained in lpReturnedString.  You can use this with the Left function to cut any trailing spaces from your buffer.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question