Author Topic: 存取 ini 設定檔的程序函數庫  (Read 9305 times)

admin

  • Administrator
  • *****
  • Posts: 0
    • View Profile
存取 ini 設定檔的程序函數庫
« on: October 18, 2010, 03:04:50 AM »
Code: [Select]
Attribute VB_Name = "modInIFunct"

Option Explicit
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long '修改System.ini 宣告
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 GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long '讀取System.ini宣告
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

Public Function ReadINIFile(ByVal strIniFile As String, ByVal strSection As String, ByVal strKey As String, Optional strDefault As String) As String

On Error GoTo ErrorAction

Dim strBuffer As String
Dim intPos As Integer
Dim lngRet As Long
Dim tmpResult As String

    strBuffer = String(255, 0)
    lngRet = GetPrivateProfileString(strSection, strKey, strDefault, strBuffer, 256, strIniFile)
    'tmpResult = Trim(Left$(strBuffer, lngRet))
    tmpResult = Left(strBuffer, InStr(strBuffer, Chr(0)) - 1)       '這個才能正確讀取中文字,不會產生多餘的空格
   
    If Len(tmpResult) > 0 Then
        ReadINIFile = tmpResult
    Else
        ReadINIFile = strDefault
    End If
   
    Exit Function
   
ErrorAction:

    ReadINIFile = strDefault
   
End Function

Public Sub WriteINIFile(ByVal strIniFile As String, ByVal strSection As String, ByVal strKey As String, ByVal SaveValue As String)

    WritePrivateProfileString strSection, strKey, SaveValue, strIniFile
   
End Sub

Private Function ClearNull(tmpString As String) As String

Dim tmpResult As String

    tmpResult = tmpString
    While Asc(Right(tmpResult, 1)) = 0
        tmpResult = Left(tmpResult, Len(tmpResult) - 1)
    Wend
    ClearNull = tmpResult
   
End Function

Public Sub FillStrAry(ByRef InputAryKeys() As String, tmpSection As String, tmpIniFile As String, Optional tmpBuffer As Integer = 4096)

Dim intX As Integer
Dim intY As Integer
Dim tmpAryKeys() As String
Dim tmpArySection() As String
Dim tmpKeyName As String
Dim lngRet As Long
Dim strBuffer As String
   
    strBuffer = Space(tmpBuffer)
    lngRet = GetPrivateProfileSection(tmpSection, strBuffer, Len(strBuffer), tmpIniFile)
    If lngRet > 0 Then
        strBuffer = Left$(strBuffer, lngRet)
        tmpArySection = Split(strBuffer, Chr$(0))
    Else
        Exit Sub
    End If
    ReDim InputAryKeys(0 To UBound(tmpArySection), 0 To 1)
    For intY = 0 To UBound(tmpArySection) - 1
        For intX = 1 To Len(tmpArySection(intY))
            If Mid(tmpArySection(intY), intX, 1) <> "=" Then
                tmpKeyName = tmpKeyName & Mid(tmpArySection(intY), intX, 1)
            Else
                InputAryKeys(intY, 0) = tmpKeyName
                InputAryKeys(intY, 1) = Mid(tmpArySection(intY), intX + 1)
                Exit For
            End If
        Next intX
        tmpKeyName = ""
    Next intY

End Sub