?
Solved

Ini problem, sections.

Posted on 1997-12-19
1
Medium Priority
?
165 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 180 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

770 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