1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
Imports System.Object
Imports System.Runtime.InteropServices.Marshal
Public Class ComINI
Private Declare Function GetPrivateProfileSection Lib "kernel32.dll" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedBuffer As IntPtr, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Private Declare Function GetPrivateProfileSectionNames Lib "kernel32.dll" Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer As IntPtr, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Private Declare Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedBuffer As IntPtr, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Private Declare Function WritePrivateProfileSection Lib "kernel32.dll" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32.dll" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
'Retourne un booléen indiquant l'existance ou non d'une section
Public Function ExisteSection(ByVal File As String, ByVal Section As String) As Boolean
Dim PtrCh As IntPtr
Dim Lng As Integer
PtrCh = StringToHGlobalAnsi(New String(vbNullChar, 1024))
Lng = GetPrivateProfileSection(Section, PtrCh, 1024, File)
Return Lng
End Function
'Retourne une collection contenant l'ensemble des sections du fichier "File"
Public Function GetAllSections(ByVal File As String) As Collection
Dim PtrCh As IntPtr
Dim Sections As Collection
Dim I, Lng As Integer
Dim Chaine, SChaine As String
PtrCh = StringToHGlobalAnsi(New String(vbNullChar, 1024))
Lng = GetPrivateProfileSectionNames(PtrCh, 1024, File)
Chaine = PtrToStringAnsi(PtrCh, Lng)
FreeHGlobal(PtrCh)
Sections = New Collection
SChaine = ""
For I = 0 To Lng - 1
If Chaine.Chars(I) = vbNullChar Then
Sections.Add(SChaine)
SChaine = ""
Else
SChaine = SChaine & Chaine.Chars(I)
End If
Next
GetAllSections = Sections
Sections = Nothing
End Function
'Retourne une collection contenant l'ensemble des clés de la section "Section" du fichier "File"
Public Function GetSectionCles(ByVal File As String, ByVal Section As String) As Collection
Dim PtrCh As IntPtr
Dim Cles As Collection
Dim I, Lng As Integer
Dim Chaine, SChaine As String
PtrCh = StringToHGlobalAnsi(New String(vbNullChar, 1024))
Lng = GetPrivateProfileSection(Section, PtrCh, 1024, File)
Chaine = PtrToStringAnsi(PtrCh, Lng)
FreeHGlobal(PtrCh)
Cles = New Collection
SChaine = ""
For I = 0 To Lng - 1
If Chaine.Chars(I) = vbNullChar Then
Cles.Add(SChaine)
SChaine = ""
Else
SChaine = SChaine & Chaine.Chars(I)
End If
Next
GetSectionCles = Cles
Cles = Nothing
End Function
'Retourne la valeur de la clé "Cle" de la section "Section" du fichier "File"
Public Function GetCle(ByVal File As String, ByVal Section As String, ByVal Cle As String) As String
Dim PtrCh As IntPtr
Dim Lng As Integer
Dim Chaine As String
PtrCh = StringToHGlobalAnsi(New String(vbNullChar, 1024))
Lng = GetPrivateProfileString(Section, Cle, "", PtrCh, 255, File)
Chaine = PtrToStringAnsi(PtrCh, Lng)
FreeHGlobal(PtrCh)
GetCle = Chaine
End Function
'Insère une section dans le fichier "File"
Public Function SetSection(ByVal File As String, ByVal Section As String, ByVal Valeur As String) As Boolean
SetSection = WritePrivateProfileSection(Section, Valeur, File)
End Function
'Insère la clé "Cle" dans la section "Section" du fichier "File"
Public Function SetCle(ByVal File As String, ByVal Section As String, ByVal Cle As String, ByVal Valeur As String) As Boolean
SetCle = WritePrivateProfileString(Section, Cle, Valeur, File)
End Function
'Efface toute les clés de la section "Section"
Public Function DelSection(ByVal File As String, ByVal Section As String) As Boolean
DelSection = WritePrivateProfileSection(Section, "", File)
End Function
'Efface la valeur de la clé "Cle" de la section "Section"
Public Function DelCle(ByVal File As String, ByVal Section As String, ByVal Cle As String) As Boolean
DelCle = WritePrivateProfileString(Section, Cle, "", File)
End Function
End Class |
Partager