INIファイル読み取り関数

広告

広告

解説

Iniファイルの内容を抽出する関数 数値、文字列両対応版

引数・戻値

第一引数INIファイルフルパス(String)
第二引数セクション名(String)
第三引数Key名(String)
第四引数取得失敗時の戻り値(String)
第五引数True:文字列 / False:数値(Boolean)
戻り値結果(Variant)

ソース

Option Compare Database

Public 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
                          
Public 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

Public Function ReadIni(strIni As String, strSec As String, strKey As String, _
                        strDef As String, blnStr As Boolean) As Variant
    If blnStr Then
        ReadIni = ReadStr(strIni, strSec, strKey, strDef)
    Else
        If Not IsNumeric(strDef) Then
            strDef = "0"
        End If
        ReadIni = GetPrivateProfileInt(strSec, strKey, CLng(strDef), strIni)
    End If
End Function

Public Function ReadStr(ByVal strFileIni As String, ByVal strSectName As String, _
           ByVal strKeyName As String, ByVal strDefault As String) As String
    Dim lngRtn As Long
    Dim strRtn As String
    
    strRtn = Space$(256)
    lngRtn = GetPrivateProfileString(strSectName, strKeyName, strDefault, strRtn, 255, strFileIni)
    
    If lngRtn > 0 Then
        ReadStr = strRtn
    Else
        ReadStr = strDefault
    End If
End Function

広告

Copyright (C) 2003-2006 七鍵 key@do.ai 初版:2003年07月07日 最終更新:2006年08月08日