Tag Archives: vb5

Visual Basic 5 & 6 INI Module

The following is a Visual Basic module to read and write from a Windows INI file. This makes use of the Windows API’s GetPrivateProfileString and WritePrivateProfileString to handle fast parsing and writing to the files.

VB Code

Attribute VB_Name = "modIni"
'*************************
'  Module: INI Processing
'*************************

' Windows API to read/write from the INI File
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
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Public Function GetFromIni(mySection As String, myEntry As String, iniFile As String) As String
    Dim buf As String
    Dim myLength As Long
    
    buf = String$(256, 0)   '256 null characters    
    myLength = GetPrivateProfileString(mySection, myEntry, "", buf, 256, iniFile)    
    getFromINI = Left$(buf, myLength)
End Function

Public Function WriteToIni(mySection As String, myEntry As String, buf As String, iniFile As String) As Boolean
    Dim x As Long
    x = WritePrivateProfileString(mySection, myEntry, buf, iniFile)

    If x = vbTrue Then
        writeToINI = True
    Else
        writeToINI = False
    End If
End Function