Solved

Ini problem, sections.

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

774 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