Click to See Complete Forum and Search --> : GetPrivateProfileString


bikdave
October 16th, 2002, 12:43 PM
Does anyone know how to use GetPrivateProfileStringA correctly?

I'm trying to use this to read in all of the key names of a section in an INI file. All key names in a section will be read if a Null value is passed in for the KeyName (see declaration below)

In my code (upgraded from VB 6), when I make this function call, it does not return the string of key names.

Here is my function declaration:
Declare Function GetPrivateProfileStringNullKey Lib "kernel32" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, ByVal lpKeyName As Integer, ByVal lpDefault As String, ByVal lpReturnedString As String, _
ByVal nSize As Long, ByVal lpFileName As String) As Integer
and here is my code related to the function call

Dim sKeyNames As New VB6.FixedLengthString(30002)
Dim sDefault As New VB6.FixedLengthString(1)
Dim nLen As Integer

' Get all key names for a section in file
' Keys names are seperated by Chr(0),
' last key name is followed two Chr(0) characters
sDefault.Value = ""

nLen = GetPrivateProfileStringNullKey(sSectionName, 0, sDefault.Value, sKeyNames.Value, 30000, sFileName)

I've tried just about everything I can think of and nothing works. Nothing ever gets returned. I have a similar call in another function where I'm retrieving the value associated with a particular key. The only difference in that call is that I pass a Key Name instead of Null to the function. That works fine.

Any help in this matter would be greatly appreciated! Thanks!

--Dave

bikdave
October 16th, 2002, 01:33 PM
I knew as soon as I posted a message, I'd make some headway.

I used to have to function declarations, one for the Null value and one to accept a key name (GetPrivateProfileStringNullKey and GetPrivateProfileKey) but I found out I could eliminate the Null one and just use GetPrivateProfileKey:
Declare Function GetPrivateProfileStringKey Lib "kernel32" _
Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As StringBuilder, ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer
If I pass in a key name for lpKeyName, I get the corresponding value of that key in lpReturnedString. For the Null case, I'm passing vbNullString instead of a key name. I also decided to use the StringBuilder class instead of string. My problem now is that when I pass vbNullString to the function, it returns the proper length in nLen (see below), but it only returns the first key (only 4 characters) in lpReturntedString. I should be getting 612 characters in that string for my particualr case. Here's the code I now have:

Dim sKeyNames As New StringBuilder(30002)
Dim sDefault As String
Dim nLen As Integer

' Get all key names for a section in file
' Keys names are seperated by Chr(0),
' last key name is followed two Chr(0) characters
sDefault = ""

nLen = GetPrivateProfileStringKey(sSectionName, vbNullString, sDefault, _
sKeyNames, 30000, sFileName)

Any thoughts on why it's only returning the first key and not all of the keys?

Thanks!

Dave

bikdave
October 24th, 2002, 08:52 AM
In case anyone wants to know how to use that function correctly, read http://archive.devx.com/dotnet/discussions/040902/cominterop.asp

vb.net_user
December 4th, 2008, 06:38 AM
Hi,

I searched also for the error, because I always got back an empty string. I have tried a little bit:


Just let the first line of the .ini-file empty to avoid this error.


After that It worked fine (I also used the long->integer exchange, string->
System.Text.StringBuilder).

Hope that helped...