读取 INI 文件的模块儿

2019-04-14 12:45发布


'*************************************************
'*    Author:  mokton           ********************
'*    Edition: 1.0.0            *****          *****
'*    Date:    2008-02-29       ********************
'*    Email:   mokton@gmail.com ********************
'*************************************************
Option Explicit
Private Declare Function GetPrivateProfileString Lib "Kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, lpKeyName As Any, ByVal lpDefault As String, ByVal lpRetunedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
'GetPrivateProfileString(Section , KeyWord , 读取失败的代替值 ,KeyWordValue返回值 , Value值缓冲区大小 , INI路径)
Private Declare Function WritePrivateProfileString Lib "Kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lplFileName As String) As Long
'WritePrivateProfileString(Section , KeyWord ,KeyWordValue ,INI路径)
Public IniPath As String 'INI文件所在的路径 和 文件名
Public Variable1 As String '变量1 字符串型
Public Variable2 As Integer
'变量2 整型
Public Variable3 As Boolean
'变量3 布尔型
Public Function Loadings()
As Boolean
    On Error GoTo Err
    IniPath = App.Path + "/Net.ini"
    Variable1 = GetFromINI("SectionName", "Variable1Name", IniPath)
    sEnableVerb = CInt(GetFromINI("SectionName", "Variable2Name", IniPath))
    sDisableVerb = CBool(GetFromINI("SectionName", "Variable3Name", IniPath))
    Loadings = True
    Exit Function
Err:
    Loadings = False
End Function
Private Function GetFromINI(AppName As String, KeyName As String, FileName As String) As String
   Dim RetStr
As String
   Dim Result As Long
   RetStr = String(255, Chr$(0))
   Result = GetPrivateProfileString(AppName, ByVal KeyName, "", RetStr, Len(RetStr), FileName)
   RetStr = TrimNull(RetStr)
   GetFromINI = RetStr
End Function
Private Function TrimNull(Item As String)
As String
    Dim pos As Integer
    pos = InStr(Item, Chr$(0))
    If pos Then Item = Left$(Item, pos - 1)
    TrimNull = Item
End Function