Solved

Ini problem, sections.

Posted on 1997-12-19
1
157 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
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now