■  INIファイル読み込み

VB6とはちょっとだけ違います。


Module xxxxx
    'INIファイル取得API
    Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
                           (ByVal lpAppName As String, _
                            ByVal lpKeyName As String, _
                            ByVal lpDefault As String, _
                            ByVal lpReturnedString As String, _
                            ByVal nSize As Integer, _
                            ByVal inifilename As String) As Integer

    Function func_GetINI() As Boolean
        Dim intRet As Integer
        Dim intDefault_Renamed As Integer
        Dim strBuff As String

        On Error GoTo Err

         'バッファを確保
        strBuff = New String(CChar(Chr(0)), 100)

        intRet = GetPrivateProfileString("YYYYY", _
                                "ZZZ", _
                                CStr(intDefault_Renamed), _
                                strBuff, _
                                Len(strBuff), _
                                "c:\TEST.ini")

        If (Left(strBuff, 1)) = CChar("0") Then    'セクション、キーワードなし
            GoTo Err
        End If

        MessageBox.Show(Left(strBuff, InStr(strBuff, Chr(0)) - 1))

        func_GetINI = True
        Exit Function

Err:
        func_GetINI = False
    End Function

※VB6では、受け取りバッファのクリアーをString(100, Chr(0))などとしていましたが、そうはいきません。

※VB6のLongと.NETのIntegerに注意してください。

※この例では、C:\TEST.iniファイルの[YYYYY]セクションのZZZ=のキーワードをサーチしています。

GetPrivateProfileStringはTRY/Catchはできません。


BEFORE PAGE

TOP PAGE