• Welcome to Valhalla Legends Archive.
 

GetPrivateProfileString -- section keys

Started by Grok, June 08, 2004, 11:02 AM

Previous topic - Next topic

Grok

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

'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


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

Ideas?

Eli_1

#1
Try GetPrivateProfileSection instead.


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


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.

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.

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.

Eric

#3
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.

Eli_1

#4
Quote from: LoRd[nK] on June 08, 2004, 01:36 PM
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.

GetPrivateProfileSection is what I suggested, it's not what he's using.

Eli_1

#5
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:

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



Grok

#6
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:
   strTemp = Split(strTemp, Chr(0) & Chr(0))(0)
   entries = Split(strTemp, Chr(0))


As these are library routines, I actually needed it to work the way I requested, not in some other way.  Final 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

UserLoser.

#7
Quote from: LoRd[nK] on June 08, 2004, 01:36 PM
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.

vbNullString is a string constant equal to "" in VB, not an operator

Eric

Quote from: Eli_1 on June 08, 2004, 02:03 PM
Quote from: LoRd[nK] on June 08, 2004, 01:36 PM
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.

GetPrivateProfileSection is what I suggested, it's not what he's using.

I meant PrivateProfileString, I was sleepy. :p