INI文件应该使用GetPrivateProfileString,该函数定义如下:
    Declare Function GetPrivateProfileString Lib _
     “kernel32″ Alias “GetPrivateProfileStringA” ( _
     ByVal lpApplicationName As Any, ByVal lpKeyName As Any, _
     ByVal lpDefault As String, ByVal lpReturnedString As String, _
     ByVal nSize As Long, ByVal lpFileName As String) As Long
    请注意这里我们定义前两个参数为Any类型。按照API说明,如果lpApplicationName参数为NULL,则该函数返回所有的段,如:
     Dim s As String
     s = Space(256)
     GetPrivateProfileString 0&, 0&, “”, s, 256, “control.ini”
     Dim v As Variant
     v = Split(s, Chr(0))
     Dim i
     For i = 0 To UBound(v)
     List1.AddItem v(i)
     Next
    这里的Split是VB6的新函数,VB5的用户可以使用:
     Public Function Split(ByVal sIn As String, Optional sDelim As _
     String, Optional nLimit As Long = -1, Optional bCompare As _
     VbCompareMethod = vbBinaryCompare) As Variant
     Dim sRead As String, sOut() As String, nC As Integer
     If sDelim = “” Then
     Split = sIn
     End If
     sRead = ReadUntil(sIn, sDelim, bCompare)
     Do
     ReDim Preserve sOut(nC)
     sOut(nC) = sRead
     nC = nC + 1
     If nLimit <> -1 And nC >= nLimit Then Exit Do
     sRead = ReadUntil(sIn, sDelim)
     Loop While sRead <> “”
     ReDim Preserve sOut(nC)
     sOut(nC) = sIn
     Split = sOut
     End Function
    
     Public Function ReadUntil(ByRef sIn As String, _
     sDelim As String, Optional bCompare As VbCompareMethod _
     = vbBinaryCompare) As String
     Dim nPos As String
     nPos = InStr(1, sIn, sDelim, bCompare)
     If nPos > 0 Then
     ReadUntil = Left(sIn, nPos – 1)
     sIn = Mid(sIn, nPos + Len(sDelim))
     End If
     End Function
    而如果第一个参数不为NULL,第二个参数为NULL,则返回段中所有项的名字,如
     Dim s As String
     s = Space(256)
     GetPrivateProfileString “don’t load”, 0&, “”, s, 256, “control.ini”
     Dim v As Variant
     v = Split(s, Chr(0))
     Dim i
     For i = 0 To UBound(v)
     List1.AddItem v(i)
     Next


评论

该日志第一篇评论

发表评论

评论也有版权!