• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2367
  • Last Modified:

Reading .ini section

Whenever I try to read a section from .ini file using the GetPrivateProfileSection function, only the first line is returned.  See code snippet below

sString = String$(230, "*")
lSize = Len(sString)
lReturn = GetPrivateProfileSection("INFORMIX CONFIG", sString, lSize, lpFileName)

MsgBox sString

Sample of .ini file
[INFORMIX CONFIG]
DRIVER={INFORMIX 3.34 32 BIT}
HOST=172.20.11.12
SERVER=net_se
PROTOCOL=sqlexec
DATABASE=/harris/52/data/pubs

** VB6 SP6 on WinXP SP2
0
stino
Asked:
stino
  • 3
  • 3
  • 2
1 Solution
 
zzzzzoocCommented:
It'll return all of the keys for "[INFORMIX CONFIG]" delimited by null-characters. You should read the keys by specifying them.

Private 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
Private Sub Form_Load()
    Dim strBuff As String * 255, intLen As Integer
    intLen = GetPrivateProfileString("INFORMIX CONFIG", "Driver", vbNullString, strBuff, 255, "c:\test.ini")
    MsgBox Left(strBuff, intLen), , "Driver"
    intLen = GetPrivateProfileString("INFORMIX CONFIG", "Host", vbNullString, strBuff, 255, "c:\test.ini")
    MsgBox Left(strBuff, intLen), , "Host"
End Sub
0
 
Joachim CarreinSr. Software DeveloperCommented:
Private Declare Function WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _
                                                                                                          ByVal lpKeyName As Any, _
                                                                                                          ByVal lpString As Any, _
                                                                                                          ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
                                                                                                      ByVal lpKeyName As String, _
                                                                                                      ByVal lpDefault As String, _
                                                                                                      ByVal lpReturnedString As String, _
                                                                                                      ByVal nSize As Long, _
                                                                                                      ByVal lpFileName As String) As Long

Public Function ReadString(Section As String, _
                           Key As String, _
                           strFileName As String) As String

    On Error Resume Next
    s = String$(255, vbNullChar)
    GetPrivateProfileString Section, Key, "", s, Len(s), strFileName
    s = Left$(s, InStr(s, vbNullChar) - 1)
    ReadString = s
    On Error GoTo 0

End Function

Public Sub WriteString(Section As String, _
                       Key As String, _
                       strValue As String, _
                       File As String)

    On Error Resume Next
    WritePrivateProfileString Section, Key, strValue, File
    On Error GoTo 0

End Sub
0
 
stinoAuthor Commented:
the problem is that it's not reading all the keys.  It's only reading the first key.  Are there any known bugs with the function.  All the examples say that the GetPrivateProfileSection should return all the keys null terminated but mine doesn't.  The GetPrivateProfileString works well but then I would have to execute that function many times to get all the key values while I would just have to execute the GetPrivateProfileSection a few times then parse the result.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Joachim CarreinSr. Software DeveloperCommented:
http://www.vbforums.com/attachment.php?s=&postid=1560500

this is an ini class i once posted a long time ago.
Maybe this one is userfull

or below all the code of the class, just paste it in there


Option Explicit
'
'   This class provides a basic interface to the ini file API
'
'   Written by Peter Nish   4/9/2000
'
'Private Declare Function ReadINT Lib "kernel32" Alias "GetPrivateProfileInt" (ByVal AppName As String, ByVal KeyName As String, ByVal DEFAULT As Integer, ByVal Filename As String) As Long
Private Declare Function ReadSTR Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal AppName As String, ByVal KeyName As String, ByVal DEFAULT As String, ByVal ReturnedString As String, ByVal MaxSize As Long, ByVal Filename As String) As Long
Private Declare Function WriteSTR Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal AppName As String, ByVal KeyName As String, ByVal NewValue As String, ByVal lpFileName As String) As Long

Private mStrFileName As String          ' path & filename of .ini file
Private mStrApplication As String       ' name of section in .ini file
Private mStrParameter As String         ' name of key to read from .ini file
Private mStrValue As String             ' value of key read from .ini file

Public Property Let Filename(ByVal inifile As String)
    '
    '   Set the path & name of the .ini file to use (Write only)
    '
    mStrFileName = inifile
   
End Property

Public Property Let Application(ByVal section As String)
    '
    '   Set the section to read from (Write only)
    '
    mStrApplication = section
   
End Property

Public Property Let Value(ByVal sNewValue As String)
    '
    '   Write a new value to the file
    '
    mStrValue = sNewValue
    WriteINIFile
   
End Property

Public Property Get Value() As String
    '
    '   Return the value read from the file (Read only)
    '
    ReadINIFile
    Value = mStrValue
   
End Property

Public Property Let Parameter(ByVal key As String)
    '
    '   Set the parameter (key) to read from the file (Write only)
    '
    mStrParameter = key
   
End Property

Private Sub ReadINIFile()
    '
    '   Read the .ini file and set the Value property
    '
    Dim keyvalue As String
    keyvalue = String$(128, 0)      ' Allocate a buffer of 128 bytes
   
    Dim CharCount As Long           ' The number of chars in the returned string
    mStrValue = ""
    CharCount = ReadSTR(mStrApplication, mStrParameter, " ", keyvalue, 127, mStrFileName)
    If CharCount > 0 Then
        mStrValue = Left$(keyvalue, CharCount)
    End If
   
End Sub

Private Sub WriteINIFile()
    '
    '   Write a new value to the ini file
    '
    Dim str As String, i As Integer, x() As String
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    x = Split(mStrFileName, "\")
    str = str & x(0)
    For i = 1 To UBound(x) - 1
        str = str & "\" & x(i)
        If Not fso.FolderExists(str) Then fso.createFolder str
    Next
    WriteSTR mStrApplication, mStrParameter, mStrValue, mStrFileName
   
End Sub

0
 
zzzzzoocCommented:
I believe you're mistaken if you're wondering why it only returns the keys. The function returns all of the keys within a section and it does do that correctly. However, it does not return their values.

0
 
stinoAuthor Commented:
wouldn't that be the GetPrivateProfileSectionNames function?  What I am saying is that when I use the GetPrivateProfileSection function it only returns the first key and value.  It seems to stop at the end of the first line.
0
 
zzzzzoocCommented:
Oops! I thought you were using the function I demonstrated. The same case applies though... multiple keys are terminated by null-characters (which will not show anything after them if you MsgBox). You may want to increase your buffer as well.


Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Sub Form_Load()
    Dim strBuff As String, intLen As Integer
    Dim strArr() As String, intLoop As Integer
    strBuff = Space$(32767)
    intLen = GetPrivateProfileSection("INFORMIX CONFIG", strBuff, Len(strBuff), "c:\test.ini")
    strBuff = Left(strBuff, intLen)
    'string is delimited by null-characters.. split it..
    strArr = Split(strBuff, vbNullChar)
    'loop through each pair of key/value..
    For intLoop = LBound(strArr) To UBound(strArr) - 1
        MsgBox strArr(intLoop)
    Next
End Sub
0
 
stinoAuthor Commented:
now you tell me!!!! I realized that the null characters so I am working on that.  I see it working already though.  Just let me finish up :-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now