Valhalla Legends Forums Archive | Visual Basic Programming | GetPrivateProfileString -- section keys

AuthorMessageTime
Grok
I am having trouble getting section keys using GetPrivateProfileString(), and the following function. Help if you can.

[code]'This Function retrieves all entries in a [Section] of INI file, returning an array of string.
Public Function GetSectionKeys() As String()

Dim strTemp As String * MAXENTRYBUFFER
Dim lret As Long
Dim entries() As String

lret = GetPrivateProfileString(m_SectionName, ByVal 0&, "", strTemp, Len(strTemp), m_INIFileName)
entries = Split(strTemp, Chr(0))
GetSectionKeys = entries

End Function[/code]

m_SectionName is "ManagedPasswords" & Chr(0)
GetPrivateProfileString always returns 0 when I leave the second parameter null.

Ideas?
June 8, 2004, 4:02 PM
Eli_1
Try GetPrivateProfileSection instead.

[code]
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
[/code]

[quote]
The GetPrivateProfileSection function retrieves all the keys and values for the specified section of an initialization file.

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

Windows NT/2000 or later: The specified profile section has no size limit.

Note This function is provided only for compatibility with 16-bit applications written for Windows. Applications should store initialization information in the registry.
[/quote]

Is this what you're looking for? Hope it helps.

[Edit]
Just a little more information.
[quote]
· lpReturnedString
[out] Pointer to a buffer that receives the key name and value pairs associated with the named section. The buffer is filled with one or more null-terminated strings; the last string is followed by a second null character.
[/quote]
June 8, 2004, 4:34 PM
Grok
Thank you. I am aware of GetPrivateProfileSection. To use its output for getting the keys, I would have to parse the returned values twice. First into arrays of key-value pairs, then into a key and value entry, respectively.

For now, I would like to troubleshoot and solve the problem with GetPrivateProfileString, where the keyname is null. MSDN indicates this will return null-delimited list of keys within the named section. My code is buggy somewhere, for it is not working.
June 8, 2004, 5:47 PM
LoRd
There's a number of things wrong with your code (From most important to least important):

.GetPrivateProfileSection() isn't returning the right values because you are not sending the values as null. They should be sent using the vbNullString operator.

.entries = Split(strTemp, Chr(0)): Since the last string in the list will be followed by two nulls, this will add one extra empty item to your array.

.Public Function GetSectionKeys() As String(): Due to an unpatched bug in Visual Basic 6.0, functions that return an array of values will be left in memory even after you're done using them. It's best to return it in the same fashion as it is sent to you and then parse it when needed.
June 8, 2004, 6:36 PM
Eli_1
[quote author=LoRd[nK] link=board=31;threadid=7154;start=0#msg64188 date=1086719796]
There's a number of things wrong with your code (From most important to least important):

.GetPrivateProfileSection() isn't returning the right values because you are not sending the values as null. They should be sent using the vbNullString operator.

.entries = Split(strTemp, Chr(0)): Since the last string in the list will be followed by two nulls, this will add one extra empty item to your array.
[/quote]

GetPrivateProfileSection is what I suggested, it's not what he's using.
June 8, 2004, 7:03 PM
Eli_1
I wrote a quick one that was almost identical to your's, and it worked. Perhaps one of your variables is empty.

On a side note, 'lpReturnedString' will contain a list of null-terminated keys. It is then padded with more null characters.
Example: A section that contains two keys.

Key1 & Chr(0) & Key2 & Chr(0) & Chr(0) & Chr(0) & Chr(0) ...

Here's a quick test one that works:
[code]
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 Sub Form_Load()
Dim test As String * 512
Dim parse() As String

GetPrivateProfileString "Colors", ByVal 0&, "", test, Len(test), "win.ini"
parse = Split(test, Chr(0))
For i = 0 To UBound(parse)
If parse(i) = "" Then Exit For
MsgBox parse(i)
Next i
End Sub
[/code]

June 8, 2004, 7:18 PM
Grok
Question: "Can someone give me directions to New York?"
Answer: "Go to Kansas, it's better there."

Thank you both, but I already have a working GetPrivateProfileSection.

Lord, however, it was the vbNullString that was at fault. Fixed.
Already fixed the return values too, with:
[code] strTemp = Split(strTemp, Chr(0) & Chr(0))(0)
entries = Split(strTemp, Chr(0))[/code]

As these are library routines, I actually needed it to work the way I requested, not in some other way. Final code:

[code]Public Function GetSectionKeys() As String()

Dim strTemp As String
Dim lret As Long
Dim entries() As String

strTemp = Space(MAXSECTIONBUFFER)
lret = GetPrivateProfileString(m_SectionName, vbNullString, "", strTemp, MAXSECTIONBUFFER, m_INIFileName)
strTemp = Split(strTemp, Chr(0) & Chr(0))(0)
entries = Split(strTemp, Chr(0))
GetSectionKeys = entries

End Function[/code]
June 8, 2004, 7:21 PM
UserLoser.
[quote author=LoRd[nK] link=board=31;threadid=7154;start=0#msg64188 date=1086719796]
There's a number of things wrong with your code (From most important to least important):

.GetPrivateProfileSection() isn't returning the right values because you are not sending the values as null. They should be sent using the vbNullString operator.

.entries = Split(strTemp, Chr(0)): Since the last string in the list will be followed by two nulls, this will add one extra empty item to your array.

.Public Function GetSectionKeys() As String(): Due to an unpatched bug in Visual Basic 6.0, functions that return an array of values will be left in memory even after you're done using them. It's best to return it in the same fashion as it is sent to you and then parse it when needed.
[/quote]

vbNullString is a string constant equal to "" in VB, not an operator
June 8, 2004, 8:11 PM
LoRd
[quote author=Eli_1 link=board=31;threadid=7154;start=0#msg64193 date=1086721430]
[quote author=LoRd[nK] link=board=31;threadid=7154;start=0#msg64188 date=1086719796]
There's a number of things wrong with your code (From most important to least important):

.GetPrivateProfileSection() isn't returning the right values because you are not sending the values as null. They should be sent using the vbNullString operator.

.entries = Split(strTemp, Chr(0)): Since the last string in the list will be followed by two nulls, this will add one extra empty item to your array.
[/quote]

GetPrivateProfileSection is what I suggested, it's not what he's using.
[/quote]

I meant PrivateProfileString, I was sleepy. :p
June 8, 2004, 8:15 PM

Search