Option Explicit
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 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 String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long
Public ErrorMsg As String
Private Sub Class_Initialize()
ErrorMsg = vbNullString
End Sub
'写入----------------------------------
Public Function WriteString(iniFileName As String, Section As String, key As String, Value As String) As Boolean
WriteString = False
ErrorMsg = vbNullString
If iniFileName = "" Then
ErrorMsg = "INI file has not been specifyed!"
Exit Function
End If
If WritePrivateProfileString(Section, key, Value, iniFileName) = 0 Then
ErrorMsg = "Failed to write to the ini file!"
Exit Function
End If
WriteString = True
End Function
'读出字符串----------------------------
Public Function ReadString(iniFileName As String, Section As String, key As String, Size As Long) As String
Dim ReturnStr As String
Dim ReturnLng As Long
ErrorMsg = vbNullString
ReadString = vbNullString
If iniFileName = "" Then
ErrorMsg = "INI file has not been specifyed!"
Exit Function
End If
ReturnStr = Space(Size)
ReturnLng = GetPrivateProfileString(Section, key, vbNullString, ReturnStr, Size, iniFileName)
ReadString = Left(ReturnStr, ReturnLng)
End Function
'读出数值-----------------------------
Public Function ReadInt(iniFileName As String, Section As String, key As String) As Long
Dim ReturnLng As Long
ReadInt = 0
ErrorMsg = vbNullString
If iniFileName = "" Then
ErrorMsg = "INI file has not been specifyed!"
Exit Function
End If
ReturnLng = GetPrivateProfileInt(Section, key, 0, iniFileName)
If ReturnLng = 0 Then
ReturnLng = GetPrivateProfileInt(Section, key, 1, iniFileName)
If ReturnLng = 1 Then
ErrorMsg = "Can not read the ini file!"
Exit Function
End If
End If
ReadInt = ReturnLng
End Function