Classic VB - How can I read/write an .Ini file?-VBForums
Results 1 to 3 of 3

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

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    10,818

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

    There are several functions built into the Windows API to help work with Ini files, the code below includes "wrapper" functions which you can use to work with them.

    This code should be pasted into the General Declarations section of your form/module
    VB Code:
    1. 'declarations for working with Ini files
    2. Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias _
    3.     "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, _
    4.     ByVal nSize As Long, ByVal lpFileName As String) As Long
    5.  
    6. Private Declare Function GetPrivateProfileString Lib "kernel32" Alias _
    7.     "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, _
    8.     ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, _
    9.     ByVal lpFileName As String) As Long
    10.  
    11. Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias _
    12.     "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, _
    13.     ByVal lpFileName As String) As Long
    14.  
    15. Private Declare Function WritePrivateProfileString Lib "kernel32" Alias _
    16.     "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, _
    17.     ByVal lpString As Any, ByVal lpFileName As String) As Long
    18.  
    19. '// INI CONTROLLING PROCEDURES
    20. 'reads an Ini string
    21. Public Function ReadIni(Filename As String, Section As String, Key As String) As String
    22. Dim RetVal As String * 255, v As Long
    23.   v = GetPrivateProfileString(Section, Key, "", RetVal, 255, Filename)
    24.   ReadIni = Left(RetVal, v - 1)
    25. End Function
    26.  
    27. 'reads an Ini section
    28. Public Function ReadIniSection(Filename As String, Section As String) As String
    29. Dim RetVal As String * 255, v As Long
    30.   v = GetPrivateProfileSection(Section, RetVal, 255, Filename)
    31.   ReadIniSection = Left(RetVal, v - 1)
    32. End Function
    33.  
    34. 'writes an Ini string
    35. Public Sub WriteIni(Filename As String, Section As String, Key As String, Value As String)
    36.   WritePrivateProfileString Section, Key, Value, Filename
    37. End Sub
    38.  
    39. 'writes an Ini section
    40. Public Sub WriteIniSection(Filename As String, Section As String, Value As String)
    41.   WritePrivateProfileSection Section, Value, Filename
    42. End Sub

    Example usage:
    VB Code:
    1. Private Sub Form_Load()
    2.  
    3. Dim File As String, OFLen As Double,  Str As String
    4.  
    5.   File = "C:\temp.ini"
    6.   OFLen = FileLen(File)
    7.  
    8.     'write a few example sections:
    9.   WriteIniSection File, "Test1", ""
    10.   WriteIniSection File, "Test2", "Here shoud be found some text"
    11.  
    12.     'write a few ini strings
    13.   WriteIni File, "Test3", "Ini1", "This is ini 1"
    14.   WriteIni File, "Test1", "Ini2", "This is ini 2"
    15.  
    16.     'test message - inform we've written the data
    17.   MsgBox Format((FileLen(File) - OFLen) / 1024, "0.00") & " KB data written to " & Chr(34) & File & Chr(34)
    18.  
    19.     'read the ini file
    20.   Str = Str & "Test2 section: " & vbTab & ReadIniSection(File, "Test2") & vbCrLf
    21.   Str = Str & "Test1 section: " & vbTab & ReadIniSection(File, "Test1") & vbCrLf
    22.   Str = Str & "Ini1 string: " & vbTab & ReadIni(File, "Test3", "Ini1") & vbCrLf
    23.   Str = Str & "Ini2 string: " & vbTab & ReadIni(File, "Test1", "Ini2") & vbCrLf
    24.  
    25.     'show the data from the file
    26.   MsgBox Str
    27.  
    28. End Sub
    Last edited by si_the_geek; Jul 18th, 2005 at 05:20 PM. Reason: added explanation & tidied up

  2. #2
    Hyperactive Member
    Join Date
    Aug 2008
    Posts
    348

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

    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. ..
    Thanks for helping me out.

  3. #3
    VBaholic & Loving It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    GetWindowRect()
    Posts
    12,429

    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"

    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} {GDI+ Classes/Samples} {Unicode Open/Save Dialog} {Icon Organizer/Extractor}
    {VBA Control Arrays} {XP/Vista 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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.