Link to home
Start Free TrialLog in
Avatar of stino
stino

asked on

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
Avatar of zzzzzooc
zzzzzooc

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
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
Avatar of stino

ASKER

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.
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

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.

Avatar of stino

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of zzzzzooc
zzzzzooc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stino

ASKER

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 :-)