Api GetPrivateProfileString

Le
Fournier Raymond
Bonjour a tous,
Quelqu'un sait comment utiliser cette API.
Elle permet de lire une section d'un fichier.ini,
mais qu'est que une section et comment l'utilisé ?

Merci de votre aide
Raymond Fournier
Vos réponses
Gagnez chaque mois un abonnement Premium avec GNT : Inscrivez-vous !
Trier par : date / pertinence
ng
Le #14657291
Salut,

Un ini se présente ainsi :

[Section1]
key1=value1
key2=value2

[Section2]
key1=value1
key2=value2

Voici un exemple :

'Example by Antti Häkkinen ()
'Visit his website at http://www.theredstar.f2s.com/
'require variable declaration
Option Explicit

'declares for ini controlling
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias
"GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal
lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String)
As Long
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 WritePrivateProfileSection Lib "kernel32" Alias
"WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As
String, 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

'when form is loaded
Private Sub Form_Load()

'if error occures resume still
On Error Resume Next

'local variables
Dim File As String, OFLen As Double, _
Str As String

'set our varibles
File = "C:temp.txt"
OFLen = FileLen(File)

'write few example sections:
WriteIniSection File, "Test1", ""
WriteIniSection File, "Test2", "Here shoud be found some text"

'write few ini strings
WriteIni File, "Test3", "Ini1", "This is ini 1"
WriteIni File, "Test1", "Ini2", "This is ini 2"

'inform we're written the data
MsgBox Format((FileLen(File) - OFLen) / 1024, "0.00") & " KB data written to
" & Chr(34) & File & Chr(34)

'read the ini file
Str = Str & "Test2 section: " & vbTab & ReadIniSection(File, "Test2") &
vbCrLf
Str = Str & "Test1 section: " & vbTab & ReadIniSection(File, "Test1") &
vbCrLf
Str = Str & "Ini1 string: " & vbTab & ReadIni(File, "Test3", "Ini1") &
vbCrLf
Str = Str & "Ini2 string: " & vbTab & ReadIni(File, "Test1", "Ini2") &
vbCrLf

'show data
MsgBox Str


End Sub

'// INI CONTROLLING PROCEDURES

'reads ini string
Public Function ReadIni(Filename As String, Section As String, Key As
String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileString(Section, Key, "", RetVal, 255, Filename)
ReadIni = Left(RetVal, v - 1)
End Function

'reads ini section
Public Function ReadIniSection(Filename As String, Section As String) As
String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileSection(Section, RetVal, 255, Filename)
ReadIniSection = Left(RetVal, v - 1)
End Function

'writes ini
Public Sub WriteIni(Filename As String, Section As String, Key As String,
Value As String)
WritePrivateProfileString Section, Key, Value, Filename
End Sub

'writes ini section
Public Sub WriteIniSection(Filename As String, Section As String, Value As
String)
WritePrivateProfileSection Section, Value, Filename
End Sub


--
Nicolas G.
FAQ VB : http://faq.vb.free.fr
API Guide : http://www.allapi.net
Google Groups : http://groups.google.fr/
MZ-Tools : http://www.mztools.com/
http://apisvb.europe.webmatrixhosting.net/

Fournier Raymond
Bonjour a tous,
Quelqu'un sait comment utiliser cette API.
Elle permet de lire une section d'un fichier.ini,
mais qu'est que une section et comment l'utilisé ?

Merci de votre aide
Raymond Fournier


Fournier Raymond
Le #14657281
"ng" news::

Ok, Alors quel est la différence entre GetPrivateProfileSection et
GetPrivateProfileString

Merci

Salut,

Un ini se présente ainsi :

[Section1]
key1=value1
key2=value2

[Section2]
key1=value1
key2=value2

Voici un exemple :

'Example by Antti Häkkinen ()
'Visit his website at http://www.theredstar.f2s.com/
'require variable declaration
Option Explicit

'declares for ini controlling
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias
"GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal
lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As
String) As Long
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 WritePrivateProfileSection Lib "kernel32"
Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal
lpString As String, 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

'when form is loaded
Private Sub Form_Load()

'if error occures resume still
On Error Resume Next

'local variables
Dim File As String, OFLen As Double, _
Str As String

'set our varibles
File = "C:temp.txt"
OFLen = FileLen(File)

'write few example sections:
WriteIniSection File, "Test1", ""
WriteIniSection File, "Test2", "Here shoud be found some text"

'write few ini strings
WriteIni File, "Test3", "Ini1", "This is ini 1"
WriteIni File, "Test1", "Ini2", "This is ini 2"

'inform we're written the data
MsgBox Format((FileLen(File) - OFLen) / 1024, "0.00") & " KB data
written to " & Chr(34) & File & Chr(34)

'read the ini file
Str = Str & "Test2 section: " & vbTab & ReadIniSection(File, "Test2")
& vbCrLf
Str = Str & "Test1 section: " & vbTab & ReadIniSection(File, "Test1")
& vbCrLf
Str = Str & "Ini1 string: " & vbTab & ReadIni(File, "Test3", "Ini1") &
vbCrLf
Str = Str & "Ini2 string: " & vbTab & ReadIni(File, "Test1", "Ini2") &
vbCrLf

'show data
MsgBox Str


End Sub

'// INI CONTROLLING PROCEDURES

'reads ini string
Public Function ReadIni(Filename As String, Section As String, Key As
String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileString(Section, Key, "", RetVal, 255, Filename)
ReadIni = Left(RetVal, v - 1)
End Function

'reads ini section
Public Function ReadIniSection(Filename As String, Section As String)
As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileSection(Section, RetVal, 255, Filename)
ReadIniSection = Left(RetVal, v - 1)
End Function

'writes ini
Public Sub WriteIni(Filename As String, Section As String, Key As
String, Value As String)
WritePrivateProfileString Section, Key, Value, Filename
End Sub

'writes ini section
Public Sub WriteIniSection(Filename As String, Section As String,
Value As String)
WritePrivateProfileSection Section, Value, Filename
End Sub




François Picalausa
Le #14657261
Bonjour/Soir,

ReadIni ne fonctionne dans ce cas que pour des chaines de 255 caractères.
Voici un bout de code pour effectuer quelques opérations supplémentaires
(énumération des clés, des sections, lecture, écriture, suppression) sur le
fichier ini sans limite de 255 caractères:
Option Explicit

Private Declare Function GetPrivateProfileString _
Lib "kernel32" _
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 GetPrivateProfileSectionNames _
Lib "kernel32" _
Alias "GetPrivateProfileSectionNamesA" _
( _
ByVal lpszReturnBuffer 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 Sub SetIniEntry(FileName As String, _
Section As String, _
Key As String, _
Value As String)
If WritePrivateProfileString(Section, _
Key, _
Value, _
FileName) = 0 Then

MsgBox "Une erreur s'est produite " & _
"lors de l'écriture des données"
End If
End Sub


Public Function GetIniEntry( _
FileName As String, _
Section As String, _
Key As String, _
Optional DefaultValue As String = "" _
) _
As String

Dim ReturnCode As Long, bContinue As Boolean, buffersize As Long

bContinue = True
buffersize = 256

Do While bContinue
GetIniEntry = String$(buffersize, vbNullChar)
ReturnCode = GetPrivateProfileString( _
Section, _
Key, _
DefaultValue, _
GetIniEntry, _
buffersize, _
FileName _
)

'The return value is the number of characters copied to the buffer,
' not including the terminating null character.
'
'If neither lpAppName nor lpKeyName is NULL
' and the supplied destination buffer is too small
' to hold the requested string, the string is truncated
' and followed by a null character,
' and the return value is equal to nSize minus one.

If ReturnCode = buffersize - 1 _
And Right(GetIniEntry, 1) = vbNullChar Then 'buffer trop court
buffersize = buffersize + 256 'incrémente par pas de 256
Else
GetIniEntry = Left$(GetIniEntry, ReturnCode)
bContinue = False
End If
Loop
End Function

Sub RemoveIniKey(strFile As String, strSection As String, strKey As String)
WritePrivateProfileString strSection, strKey, vbNullString, strFile
End Sub

Sub RemoveIniSection(strFile As String, strSection As String)
WritePrivateProfileString strSection, _
vbNullString, vbNullString, strFile
End Sub

Public Function GetIniKeys(FileName As String, Section As String) As String
Dim ReturnCode As Long, bContinue As Boolean, buffersize As Long

bContinue = True
buffersize = 256

Do While bContinue
GetIniKeys = String$(buffersize, vbNullChar)
ReturnCode = GetPrivateProfileString( _
Section, _
ByVal vbNullString, _
"", _
GetIniKeys, _
buffersize, _
FileName _
)

'If either lpAppName or lpKeyName is NULL and the
'supplied destination buffer is too small to hold
'all the strings, the last string is truncated and
'followed by two null characters. In this case,
'the return value is equal to nSize minus two.

If ReturnCode = buffersize - 2 Then 'buffer trop court
buffersize = buffersize + 256 'incrémente par pas de 256
Else
If ReturnCode = 0 Then
GetIniKeys = ""
Else
GetIniKeys = Left$(GetIniKeys, ReturnCode - 1)
End If
bContinue = False
End If
Loop
End Function

Public Function GetIniSections2(FileName As String) As String
Dim ReturnCode As Long, bContinue As Boolean, buffersize As Long

bContinue = True
buffersize = 256

Do While bContinue
GetIniSections2 = String$(buffersize, vbNullChar)
ReturnCode = GetPrivateProfileString( _
ByVal vbNullString, _
ByVal vbNullString, _
"", _
GetIniSections2, _
buffersize, _
FileName _
)

'If either lpAppName or lpKeyName is NULL and the
'supplied destination buffer is too small to hold
'all the strings, the last string is truncated and
'followed by two null characters. In this case,
'the return value is equal to nSize minus two.

If ReturnCode = buffersize - 2 Then 'buffer trop court
buffersize = buffersize + 256 'incrémente par pas de 256
Else
If ReturnCode = 0 Then
GetIniSections2 = ""
Else
GetIniSections2 = Left$(GetIniSections2, ReturnCode - 1)
End If
bContinue = False
End If
Loop
End Function
Public Function GetIniSections(strFileName As String) As String
Dim ReturnCode As Long, bContinue As Boolean, buffersize As Long

bContinue = True
buffersize = 256

Do While bContinue
GetIniSections = String$(buffersize, vbNullChar)
ReturnCode = GetPrivateProfileSectionNames( _
GetIniSections, _
buffersize, _
strFileName _
)

'The return value specifies the number of characters
'copied to the specified buffer, not including the
'terminating null character. If the buffer is not large
'enough to contain all the section names associated
'with the specified initialization file, the return
'value is equal to the size specified by nSize minus two.

If ReturnCode = buffersize - 2 Then 'buffer trop court
buffersize = buffersize + 256 'incrémente par pas de 256
Else
'on vire les caractères en trop
If ReturnCode = 0 Then
GetIniSections = ""
Else
GetIniSections = Left$(GetIniSections, ReturnCode - 1)
End If
bContinue = False
End If
Loop
End Function

--
François Picalausa (MVP VB)
http://faq.vb.free.fr --- http://msdn.microsoft.com
http://apisvb.europe.webmatrixhosting.net

"ng" news:
Salut,
'reads ini string
Public Function ReadIni(Filename As String, Section As String, Key As
String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileString(Section, Key, "", RetVal, 255, Filename)
ReadIni = Left(RetVal, v - 1)
End Function


François Picalausa
Le #14657251
Bonjour/soir,

Voici ce que dit la MSDN à propos de GetPrivateProfileSection:
The GetPrivateProfileSection function retrieves all the keys and values for
the specified section of an initialization file.


Windows Me/98/95: The specified profile section must not exceed 32K.

Donc GetPrivateProfileString pour retrouver une valeur et
GetPrivateProfileSection pour retrouver toutes les valeurs et clés d'une
section
(GetPrivateProfileSection renvoie valeur et clés sous le format au format
key=string)

--
François Picalausa (MVP VB)
http://faq.vb.free.fr --- http://msdn.microsoft.com
http://apisvb.europe.webmatrixhosting.net

"Fournier Raymond" le message de news:
"ng" news::

Ok, Alors quel est la différence entre GetPrivateProfileSection et
GetPrivateProfileString


Fournier Raymond
Le #14657221
"François Picalausa" news::


Merci pour ces informations, je vais étudier cela avec un grand intérêt.

Raymond Fournier

ReadIni ne fonctionne dans ce cas que pour des chaines de 255
caractŠres. Voici un bout de code pour effectuer quelques op‚rations
suppl‚mentaires (‚num‚ration des cl‚s, des sections, lecture,
‚criture, suppression) sur le fichier ini sans limite de 255
caractŠres: Option Explicit

Private Declare Function GetPrivateProfileString _
Lib "kernel32" _
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 GetPrivateProfileSectionNames _
Lib "kernel32" _
Alias "GetPrivateProfileSectionNamesA" _
( _
ByVal lpszReturnBuffer 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 Sub SetIniEntry(FileName As String, _
Section As String, _
Key As String, _
Value As String)
If WritePrivateProfileString(Section, _
Key, _
Value, _
FileName) = 0 Then

MsgBox "Une erreur s'est produite " & _
"lors de l'‚criture des donn‚es"
End If
End Sub


Public Function GetIniEntry( _
FileName As String, _
Section As String, _
Key As String, _
Optional DefaultValue As String = "" _
) _
As String

Dim ReturnCode As Long, bContinue As Boolean, buffersize As Long

bContinue = True
buffersize = 256

Do While bContinue
GetIniEntry = String$(buffersize, vbNullChar)
ReturnCode = GetPrivateProfileString( _
Section, _
Key, _
DefaultValue, _
GetIniEntry, _
buffersize, _
FileName _
)

'The return value is the number of characters copied to the
buffer, ' not including the terminating null character.
'
'If neither lpAppName nor lpKeyName is NULL
' and the supplied destination buffer is too small
' to hold the requested string, the string is truncated
' and followed by a null character,
' and the return value is equal to nSize minus one.

If ReturnCode = buffersize - 1 _
And Right(GetIniEntry, 1) = vbNullChar Then 'buffer trop
court buffersize = buffersize + 256 'incr‚mente par pas de
256
Else
GetIniEntry = Left$(GetIniEntry, ReturnCode)
bContinue = False
End If
Loop
End Function

Sub RemoveIniKey(strFile As String, strSection As String, strKey As
String)
WritePrivateProfileString strSection, strKey, vbNullString,
strFile
End Sub

Sub RemoveIniSection(strFile As String, strSection As String)
WritePrivateProfileString strSection, _
vbNullString, vbNullString, strFile
End Sub

Public Function GetIniKeys(FileName As String, Section As String) As
String
Dim ReturnCode As Long, bContinue As Boolean, buffersize As Long

bContinue = True
buffersize = 256

Do While bContinue
GetIniKeys = String$(buffersize, vbNullChar)
ReturnCode = GetPrivateProfileString( _
Section, _
ByVal vbNullString, _
"", _
GetIniKeys, _
buffersize, _
FileName _
)

'If either lpAppName or lpKeyName is NULL and the
'supplied destination buffer is too small to hold
'all the strings, the last string is truncated and
'followed by two null characters. In this case,
'the return value is equal to nSize minus two.

If ReturnCode = buffersize - 2 Then 'buffer trop court
buffersize = buffersize + 256 'incr‚mente par pas de 256
Else
If ReturnCode = 0 Then
GetIniKeys = ""
Else
GetIniKeys = Left$(GetIniKeys, ReturnCode - 1)
End If
bContinue = False
End If
Loop
End Function

Public Function GetIniSections2(FileName As String) As String
Dim ReturnCode As Long, bContinue As Boolean, buffersize As Long

bContinue = True
buffersize = 256

Do While bContinue
GetIniSections2 = String$(buffersize, vbNullChar)
ReturnCode = GetPrivateProfileString( _
ByVal vbNullString, _
ByVal vbNullString, _
"", _
GetIniSections2, _
buffersize, _
FileName _
)

'If either lpAppName or lpKeyName is NULL and the
'supplied destination buffer is too small to hold
'all the strings, the last string is truncated and
'followed by two null characters. In this case,
'the return value is equal to nSize minus two.

If ReturnCode = buffersize - 2 Then 'buffer trop court
buffersize = buffersize + 256 'incr‚mente par pas de 256
Else
If ReturnCode = 0 Then
GetIniSections2 = ""
Else
GetIniSections2 = Left$(GetIniSections2, ReturnCode -
1)
End If
bContinue = False
End If
Loop
End Function
Public Function GetIniSections(strFileName As String) As String
Dim ReturnCode As Long, bContinue As Boolean, buffersize As Long

bContinue = True
buffersize = 256

Do While bContinue
GetIniSections = String$(buffersize, vbNullChar)
ReturnCode = GetPrivateProfileSectionNames( _
GetIniSections, _
buffersize, _
strFileName _
)

'The return value specifies the number of characters
'copied to the specified buffer, not including the
'terminating null character. If the buffer is not large
'enough to contain all the section names associated
'with the specified initialization file, the return
'value is equal to the size specified by nSize minus two.

If ReturnCode = buffersize - 2 Then 'buffer trop court
buffersize = buffersize + 256 'incr‚mente par pas de 256
Else
'on vire les caractŠres en trop
If ReturnCode = 0 Then
GetIniSections = ""
Else
GetIniSections = Left$(GetIniSections, ReturnCode - 1)
End If
bContinue = False
End If
Loop
End Function

--


Fournier Raymond
Le #14654111
Bonjour,
Est-ce que c'est possible de connaître le nombre de clé dans une section
d'un fichier.ini ?

Ceci dans le but d'extraire la valeur de chaque clé de la section.

Merci Raymond Fournier
François Picalausa
Le #14654081
Hello,

Je ne connais pas de fonction qui fasse celà et en regardant rapidement dans
le platform SDK, je n'en trouve pas.
Une solution serait de splitter la chaine renvoyée par GetIniKeys et de
retrouver toutes les valeurs.
Le séparateur est vbNullChar

La taille est donc égale à
Dim strSplitted() As String

strSplitted = Split(GetIniKeys(...))
Msgbox "Nombre de clés : " & (Ubound(strSplitted) - LBound(strSplitted) +1)

Le même type de code doit être utilisable avec GetPrivateProfileSection (mis
à part que celui ci renverrait dirrectement les valeurs associées en plus
des noms de clé, moyennant un travail sur les chaines pour retrouver le
"=" ).

--
François Picalausa (MVP VB)
http://faq.vb.free.fr --- http://msdn.microsoft.com
http://apisvb.europe.webmatrixhosting.net

"Fournier Raymond" le message de news:
Bonjour,
Est-ce que c'est possible de connaître le nombre de clé dans une
section d'un fichier.ini ?

Ceci dans le but d'extraire la valeur de chaque clé de la section.

Merci Raymond Fournier


xyzDaniel
Le #14734461
Bonjour,
Oui, cela est possible.
La solution que j'utilise est d'ajouter une cle de type
<nbr_suite> qui contient ce nombre.
Daniel

-----Message d'origine-----
Bonjour,
Est-ce que c'est possible de connaître le nombre de clé


dans une section
d'un fichier.ini ?

Ceci dans le but d'extraire la valeur de chaque clé de la


section.

Merci Raymond Fournier
.



Publicité
Poster une réponse
Anonyme