Results 1 to 3 of 3

Thread: Classic VB - How can I read/write an .Ini file?

Threaded View

  1. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Classic VB - How can I read/write an .Ini file?

    Quote Originally Posted by batori View Post
    this code does work but it does not give back the entire string form the ini...one last char is missing most of the times. ..
    The problem here is that if more than 254 characters were written to the INI then the ReadINI function will truncate to 254 characters, as the function is written. Also the return value (if successful) is not one less than the length of the string, so subtracting 1 from it is inappropriate.

    The API returns Len(String)-1 if Section & Key parameters are passed and the string sent to the API isn't big enough for the returned value. The workaround is to test the return value and resize the passed string until the API return value meets a specific criteria...
    Code:
    Public Function ReadIni(Filename As String, Section As String, Key As String) As String
    
        Dim RetVal As String, v As Long
        Dim retLen As Long
        
        Do
            retLen = retLen + 260 ' arbitrary - can be set higher/lower if desired
            RetVal = Space(retLen)
            v = GetPrivateProfileString(Section, Key, "", RetVal, retLen, Filename)
        Loop Until v < retLen - 1 
        
        ReadIni = Left$(RetVal, v)
    
    End Function
    Last edited by LaVolpe; Aug 23rd, 2010 at 03:50 PM. Reason: clarified comments/remarks
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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