CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2002
    Location
    Cleveland, OH
    Posts
    7

    Question GetPrivateProfileString

    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:
    Code:
        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
    Code:
            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

  2. #2
    Join Date
    Oct 2002
    Location
    Cleveland, OH
    Posts
    7

    Partially fixed...now a new problem.

    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:
    Code:
    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:
    Code:
    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

  3. #3
    Join Date
    Oct 2002
    Location
    Cleveland, OH
    Posts
    7

    Answer

    In case anyone wants to know how to use that function correctly, read http://archive.devx.com/dotnet/discu...cominterop.asp

  4. #4
    Join Date
    Dec 2008
    Posts
    1

    Re: GetPrivateProfileString - found another error

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured