Bonjour à tous,

J'ai des données utilisateur couplées à des données application que je ne peux pas stocker dans les ApplicationSettings pas plus que dans la base de registre. Il y en a trop et elle n'est pas faite pour ça. Je souhaite donc en revenir à mon bon vieux fichier ini.

Mais tous les scripts que je trouve de lecture, soit pour lire la collection de Sections, soit pour lire la collection de Clés, soit pour lire la valeur d'une clé, ne fonctionnent pas. Même ceux du tutorial de Plasserre. J'ai trouvé une dizaine de classes mais chaque fois le debogeur coince et affiche, en vert, :

PInvokeStackImbalance a été détecté
Un appel à la fonction PInvoke 'ASE!ASE.Functions::GetPrivateProfileString' a déséquilibré la pile. Cela peut se produire, car la signature PInvoke managée ne correspond pas à la signature cible non managée. Vérifiez que la convention d'appel et les paramètres de la signature PInvoke correspondent à la signature non managée cible.
où ASE est le nom de l'appli.

La seule ligne que j'ai changée sur les scripts trouvés c'est celle là :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
strReturn = String(255, chr(0))
Mon VB ne prend pas la fonction String et j'y pallie en écrivant soit :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
Dim i as integer=0
Dim strReturn as string = string.Empty
 
For i = 1 to 255
    strReturn = strReturn & chr(0)
next
soit celle-ci (j'ai testé les deux... :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
Dim strReturn as string = space(chr(0))
Je mets ici les 6 cas possibles qui, tous, coincent...

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
 
     Private Declare Function GetPrivateProfileSection Lib "kernel32.dll" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    Private Declare Function GetPrivateProfileSectionNames Lib "kernel32.dll" Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    Private Declare Function GetPrivateProfileString Lib "kernel32.dll" 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
    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
 
 
'premier cas
'---------------
 
Public Function GetSectionsCollection(ByVal strINIFile As String) As Collection
 
        Dim strBuf As String = = Space(500)
        Dim col As Object = Nothing
        Dim bufCol As Collection = Nothing
        Dim I As Long = 0
        Dim lngRet As Long = 0
 
        lngRet = GetPrivateProfileSectionNames(strBuf, Len(strBuf), strINIFile)
 
        If lngRet <> 0 Then
            strBuf = Microsoft.VisualBasic.Left(strBuf, lngRet)
        Else
            GetSectionsCollection = Nothing
            Exit Function
        End If
 
        col = Split(strBuf, Chr(0))
        bufCol = New Collection
 
        For I = 0 To UBound(col)
            If (col(I)).Trim IsNot String.Empty Then bufCol.Add(col(I))
        Next
 
        col = Nothing
        GetSectionsCollection = bufCol
        bufCol = Nothing
 
    End Function
 
    Private Sub lireIni()
 
        Dim FichierIni As String = CheminConfig & "Repart.ini"        
        Dim Coll As Collection
        Dim msg As String = String.Empty
        Dim i As Integer = 0
 
        Coll = GetSectionsCollection(FichierIni)
 
        For i = 1 To Coll.Count
            msg = msg & Coll(i)
        Next
 
        MessageBox.Show(msg)
 
    End Sub
 
'---------------------------------------------------------
 
'deuxième cas :
'----------------
 
Private Function ListeSectionIni(ByVal Path As String, Section() As String)
    Dim strReturn As String
    strReturn = String(8192, 0)
 
    GetPrivateProfileSectionNames strReturn, Len(strReturn), Path
 
    Section = Split(Left(strReturn, InStr(1, strReturn, vbNullChar & vbNullChar) - 1), vbNullChar)
End Function
 
    Private Sub LireIni()
 
        Dim strINIFile As String = CheminConfig & "Repart.ini"
        Dim strSection As String = "Marchés français"    'Nom de la rubrique        
        Dim Coll As Collection
        Dim msg As String = String.Empty
        Dim i As Integer = 0
 
        Coll=ListeSectionKey(strINIFile, "", Key() As String)
        For i = 1 To Coll.Count
            msg = msg & Coll(i)
        Next
 
        MessageBox.Show(msg)
 
    End Sub
 
 
'---------------------------------------------------------
 
'troisième cas :
'----------------
 
Public Function GetKeysCollection(ByVal strINIFile As String, ByVal strSection As String, Optional ByVal bWithValues As Boolean = True) As Collection
 
        Dim strBuf As String = Space(500)
        Dim col As Object = Nothing
        Dim Col2 As Object = Nothing
        Dim bufCol As Collection = Nothing
        Dim I As Long = 0
        Dim lngRet As Long = 0
 
        lngRet = GetPrivateProfileSection(strSection, strBuf, Len(strBuf), strINIFile)
 
        If lngRet <> 0 Then
            strBuf = Microsoft.VisualBasic.Left(strBuf, lngRet)
        Else
            GetKeysCollection = Nothing
            Exit Function
        End If
 
        col = Split(strBuf, Chr(0))
        bufCol = New Collection
 
        For I = 0 To UBound(col)
            If (col(I)).Trim IsNot String.Empty Then
                If Microsoft.VisualBasic.Left(col(I), 1) <> ";" Then
                    If bWithValues = True Then
                        bufCol.Add(col(I))
                    Else
                        Col2 = Split(col(I), "=", 2)
                        bufCol.Add(Col2(0))
                    End If
                End If
            End If
        Next
 
        col = Nothing
        Col2 = Nothing
        GetKeysCollection = bufCol
        bufCol = Nothing
 
    End Function
 
    Private Sub LireIni()
 
        Dim FichierIni As String = CheminConfig & "Repart.ini"
        Dim strSection As String = "Marchés français"
        Dim Coll As Collection
        Dim msg As String = String.Empty
        Dim i As Integer = 0
 
        Coll = GetKeysCollection(FichierIni, strSection)
 
        For i = 1 To Coll.Count
            msg = msg & Coll(i)
        Next
 
        MessageBox.Show(msg)
 
    End Sub
 
 
'---------------------------------------------------------
 
'quatrième cas :
'----------------
 
    Public Function ListeSectionKey(ByVal Path As String, ByVal Section As String, Key() As String)
 
        Dim strReturn As String = Space(8192)
 
        strReturn = GetPrivateProfileSection(Section, strReturn, 8192, Path)
 
        ListeSectionKey = Split(Left(strReturn, InStr(1, strReturn, vbNullChar & vbNullChar) - 1), vbNullChar)
 
    End Function
 
    Private Sub LireIni()
 
        Dim FichierIni As String = CheminConfig & "Repart.ini"
        Dim strSection As String = "Marchés français"
        Dim Key() As String
        Dim msg As String = String.Empty
        Dim i As Integer = 0
 
        ListeSectionKey(FichierIni, strSection, Key)
 
        For i = LBound(Key) To UBound(Key)
            msg = msg & Key(i)
        Next
 
        MessageBox.Show(msg)
 
    End Sub
 
'------------------------------------------------------------
 
' et de façon générale
'-----------------------
'
'1er Cas
'-----------
 
    Public Function GetINIKeyValue(ByVal strINIFile As String, ByVal strSection As String, ByVal strKey As String) As String
 
        Dim strBuf As String = Space(255)
        Dim lngRet As Long = 0
 
        lngRet = GetPrivateProfileString(strSection, strKey, "", strBuf, Len(strBuf), strINIFile)
        If lngRet <> 0 Then
            GetINIKeyValue = Left$(strBuf, lngRet)
        Else
            GetINIKeyValue = String.Empty
        End If
 
    End Function
 
    Private Sub LireIni()
 
        Dim FichierIni As String = CheminConfig & "Repart.ini"
        Dim strSection As String = "Marchés français"
        Dim strKey As String = "Categ5"
        Dim msg As String = String.Empty
        Dim i As Integer = 0
 
        msg = GetINIKeyValue(FichierIni, strSection, strKey)
 
        MessageBox.Show(msg)
 
    End Sub
 
'---------------------------------
 
'second cas
'--------------
 
    Public Function LitDansFichierIni(Section As String, Cle As String, Fichier As String, Optional ValeurParDefaut As String = "") As String
 
        Dim strReturn As String = Space(255)
 
        GetPrivateProfileString(Section, Cle, ValeurParDefaut, strReturn, Len(strReturn), Fichier)
        LitDansFichierIni = Left(strReturn, InStr(strReturn, Chr(0)) - 1)
 
    End Function
 
    Private Sub LireIni()
 
        Dim FichierIni As String = CheminConfig & "Repart.ini"
        Dim strSection As String = "Marchés français"
        Dim strKey As String = "Categ5"
        Dim msg As String = String.Empty
        Dim i As Integer = 0
 
        msg = LitDansFichierIni(strSection, strKey, FichierIni, "")
 
        MessageBox.Show(msg)
 
    End Sub
J'espère que ça aura éclairé quelqu'un...