|
与INI有关的API函数有:
1. GetPrivateProfileInt函数:从INI文件中指定的节获取一个整数键值
定义:
Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
举例:
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
Private Sub Form_Load()
'Sample for reading a numbers directly on the INI file
'Write a number 55 on the sample.ini to be read
WritePrivateProfileString "Sample", "Sample", "55", App.Path & "\sample.ini"
'Then, read the stored number
'No need to convert the value returned
MsgBox GetPrivateProfileInt("Sample", "Sample", 0, App.Path & "\sample.ini")
End Sub
2. GetPrivateProfileSection函数:从INI文件的指定节中获取所有的键和键值。
定义:
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 Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
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()
' We will create a new section in the INI-file
' It's output will be:
'
' [SectionName]
' Key=Value
'
' Note that used this ONLY if you are creating a new
' section on the INI file, unless you wanted to erase
' its existing keys.
Call WritePrivateProfileSection("SectionName", "Key=Value", App.Path & "\sample.ini")
Dim szBuf As String * 255
Call GetPrivateProfileSection("SectionName", szBuf, 255, App.Path & "\sample.ini")
MsgBox szBuf
End Sub
3. GetPrivateProfileSectionNames函数:从INI文件中获取所有节的名称。
定义:
Declare Function GetPrivateProfileSectionNames Lib "kernel32.dll" Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
举例:
'Note : Need one listbox named List1
Private Declare Function GetPrivateProfileSectionNames Lib "kernel32.dll" Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Sub Form_Load()
Dim szBuf As String, Length As Integer
Dim SectionArr() As String, m As Integer
szBuf = String$(255, 0)
Length = GetPrivateProfileSectionNames(szBuf, 255, vbNullChar)
szBuf = Left$(szBuf, Length)
SectionArr = Split(szBuf, vbNullChar)
For m = 0 To UBound(SectionArr)
List1.AddItem SectionArr(m)
Next m
End Sub
4. GetPrivateProfileString函数:从INI文件中指定的节获取一个字符串键值。
定义:
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 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 Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As Strin |
|