Private 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
Private 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
' Functions
Function GetFromINI(sSection As String, sKey As String, sDefault As String, sIniFile As String)
Dim sBuffer As String, lRet As Long
' Fill String with 255 spaces
sBuffer = String$(255, 0)
' Call DLL
lRet = GetPrivateProfileString(sSection, sKey, "", sBuffer, Len(sBuffer), sIniFile)
If lRet = 0 Then
' DLL failed, save default
If sDefault <> "" Then AddToINI sSection, sKey, sDefault, sIniFile
GetFromINI = sDefault
Else
' DLL successful
' return string
GetFromINI = Left(sBuffer, InStr(sBuffer, Chr(0)) - 1)
End If
End Function
' Returns True if successful. If section does not
' exist it creates it.
Function AddToINI(sSection As String, sKey As String, sValue As String, sIniFile As String) As Boolean
Dim lRet As Long
' Call DLL
lRet = WritePrivateProfileString(sSection, sKey, sValue, sIniFile)
AddToINI = (lRet)
End Function