Attribute VB_Name = "modINI"

Option Explicit

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 GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault 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 WriteStringToIni(lpAppName As String, lpKeyName As String, lpString As String, lpFileName As String) As Long
   Dim rc

   If Right$(lpAppName, 1) <> Chr$(0) Then
      lpAppName = lpAppName & Chr$(0)
   End If

   If Right$(lpKeyName, 1) <> Chr$(0) Then
      lpKeyName = lpKeyName & Chr$(0)
   End If

   If Right$(lpString, 1) <> Chr$(0) Then
      lpString = lpString & Chr$(0)
   End If

   If Right$(lpFileName, 1) <> Chr$(0) Then
      lpFileName = lpFileName & Chr$(0)
   End If

   rc = WritePrivateProfileString(lpAppName, lpKeyName, lpString, lpFileName)
   WriteStringToIni = rc
End Function
Public Function GetIntFromIni(lpAppName As String, lpKeyName As String, nDefault As Long, lpFileName As String) As Long
   Dim rc As Long

   If Right$(lpAppName, 1) <> Chr$(0) Then
      lpAppName = lpAppName & Chr$(0)
   End If

   If Right$(lpKeyName, 1) <> Chr$(0) Then
      lpKeyName = lpKeyName & Chr$(0)
   End If

   If Right$(lpFileName, 1) <> Chr$(0) Then
      lpFileName = lpFileName & Chr$(0)
   End If

   rc = GetPrivateProfileInt(lpAppName, lpKeyName, nDefault, lpFileName)
   GetIntFromIni = rc
End Function

Public Function GetStringFromIni(lpAppName As String, lpKeyName As String, lpDefault As String, lpFileName As String) As String
                                                                                         
   Dim rc As Long
   Dim lpReturnString As String
   Dim nSize As Long

   If Right$(lpAppName, 1) <> Chr$(0) Then
      lpAppName = lpAppName & Chr$(0)
   End If

   If Right$(lpKeyName, 1) <> Chr$(0) Then
      lpKeyName = lpKeyName & Chr$(0)
   End If

   If Right$(lpDefault, 1) <> Chr$(0) Then
      lpDefault = lpDefault & Chr$(0)
   End If

   lpReturnString = String$(300, 0)
   nSize = Len(lpReturnString)

   If Right$(lpFileName, 1) <> Chr$(0) Then
      lpFileName = lpFileName & Chr$(0)
   End If

   rc = GetPrivateProfileString(lpAppName, lpKeyName, lpDefault, lpReturnString, nSize, lpFileName)
   GetStringFromIni = Left$(lpReturnString, InStr(1, lpReturnString, Chr$(0)) - 1)
End Function
