Solved

Ini problem, sections.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

912 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

28 Experts available now in Live!

Get 1:1 Help Now