Option Explicit


'以下是声明读写WinINI文件的三个API函数据

'GetPrivateProfileInt 用于读取INI文件整型数据 (返回所读取的长整型值)
                                               'lpApplicationName 要读取的段 (Section) 名称
                                               'lpKeyName 要读取的键 (Key) 名称
                                               'nDefault 指定默认值,如果读取时出错,则返回该值
                                               'lpFileName 指定要读的 INI 文件名
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
    
    

'GetPrivateProfileString 用于读取INI文件字符串型数据 (返回所读取的字符串值的真实长度)
                                              'lpApplicationName 要读取的段 (Section) 名称
                                              'lpKeyName 要读取的的键 (Key) 名称
                                              'lpDefault 指定默认值,如果读取时出错,则返回该值
                                              'lpReturnedString 指定接收返回值的字符串变量
                                              'nSize 指定允许字符串值的最大长度
                                              'lpFileName 指定要读的 INI 文件名
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
    
    

'WritePrivateProfileString 是用来向 INI 文件写信息 (如果成功返回非 0 值,失败返回 0)
                                              'lpApplicationName 要写入的段 (Section) 名称
                                              'lpKeyName 要写入的的键 (Key) 名称
                                              'lpString 要写入的值 (Value),以字符串表示
                                              'lpFileName 指定要写的 INI 文件名
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 iniFileName As String       '声明API所用的文件名属性
Public ErrorMsg As String           '定义一个保存错误信息的变量作为 Cla_RWiniFile 类的第二个属性

'Cla_RWiniFile 类的初始化
Private Sub Class_Initialize()
    
    iniFileName = vbNullString      '在定义方法之前还需要对属性进行初始化
    ErrorMsg = vbNullString         '在定义方法之前还需要对属性进行初始化
    
End Sub


'为了指定 INI 文件名给 CIniFile,需要定义一个方法:
Public Sub SpecifyIni(FilePathName)
    iniFileName = Trim(FilePathName)
End Sub

'在每次读写值之前还需要先判断是否已经指定了 INI 文件名
Private Function NoIniFile() As Boolean
    NoIniFile = True
    If iniFileName = vbNullString Then
        ErrorMsg = "没有指定 INI 文件"
        Exit Function
    End If
    ErrorMsg = vbNullString
    NoIniFile = False
End Function


'编写一个INI文件数据写入的接口(成功返回 True,失败返回 False)
Public Function WriteString(Section As String, key As String, value As String) As Boolean
    WriteString = False
    If NoIniFile() Then
        Exit Function
    End If
    
    If WritePrivateProfileString(Section, key, value, iniFileName) = 0 Then
        ErrorMsg = "数据写入失败!"
    End If
    
    WriteString = True
End Function

'编写一个INI文件读取字符串的接口
Public Function ReadString(Section As String, key As String, Size As Long) As String
    Dim ReturnStr As String
    Dim ReturnLng As Long
    ReadString = vbNullString
    
    If NoIniFile() Then      '如果文件读取失败那么将退出函数
        Exit Function
    End If
    
    ReturnStr = Space(Size)   'Space 函数在格式输出固定长度字符串数据
    ReturnLng = GetPrivateProfileString(Section, key, vbNullString, ReturnStr, Size, iniFileName)
    ReadString = Left(ReturnStr, 255)
    
End Function

请回复LFW168@163.COM谢谢!!!