Attribute VB_Name = "Ini"
Option Explicit

' Gestion des accès au fichier 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
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

Public Type TIniGetItem         ' Structure pour lecture dans fichier ini
    PrmOk As Boolean
    Available As Boolean        ' True si existant dans le fichier
    PathName As String
    FileName As String
    Section As String
    Key As String
    StringValue As String
    StringLen As Long
End Type

Public Type TIniSetItem         ' Structure pour écriture dans fichier ini
    PathName As String
    FileName As String
    Section As String
    Key As String
    StringValue As String
End Type




Public Sub IniGetString(Prm As TIniGetItem)
' Retourne une chaine provenant d'une clé d'une section d'un fichier ini
' ======================================================================
Dim PathName As String
Dim FileName As String
Dim StringValue As String * 255
Dim StringLen As Long

' Chemin du fichier
PathName = Trim$(Prm.PathName)
If PathName = "" Then
    PathName = App.Path
End If
If Right$(PathName, 1) <> "\" Then PathName = PathName & "\"
FileName = Trim$(Prm.FileName)
    

' On continue si Fichier , Chemin, Section et Cle sont définis
Prm.PrmOk = False
Prm.Available = False
If PathName <> "" Then
    If FileName <> "" Then
        If Trim$(Prm.Section) <> "" Then
            If Trim$(Prm.Key) <> "" Then
                Prm.PrmOk = True
                StringLen = GetPrivateProfileString(Trim$(Prm.Section), Trim$(Prm.Key), "", StringValue, 255, PathName & FileName)
                If StringLen > 0 Then
                    Prm.StringValue = Left$(StringValue, StringLen)
                    Prm.StringLen = StringLen
                    Prm.Available = True
                End If
            End If
        End If
    End If
End If
End Sub

Public Sub IniSetString(Prm As TIniSetItem)
' Ecriture dans le fichier ini
Dim PathName As String
Dim FileName As String
Dim StringValue As String * 255
Dim StringLen As Long

' Chemin du fichier
PathName = Trim$(Prm.PathName)
If PathName = "" Then
    PathName = App.Path
End If
If Right$(PathName, 1) <> "\" Then PathName = PathName & "\"
FileName = Trim$(Prm.FileName)

' On continue si Fichier , Chemin, Section et Cle sont définis
If PathName <> "" Then
    If FileName <> "" Then
        If Trim$(Prm.Section) <> "" Then
            If Trim$(Prm.Key) <> "" Then
                StringValue = Trim$(Prm.StringValue)
                StringValue = Replace(StringValue, Chr$(0), "")
                StringValue = Trim$(StringValue)
                Call WritePrivateProfileString(Trim$(Prm.Section), Trim$(Prm.Key), Trim$(StringValue), PathName & FileName)
            End If
        End If
    End If
End If

End Sub
