1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| Option Explicit
'Windows API calls
'This writes an entry in the INI file
'It takes as parameters the section name of the INI file,
'the name of the entry, its value and the INI filename
Private Declare Function WritePrivateProfileString _
Lib "kernel32" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationKey As String, _
ByVal lpKeyName As Any, _
ByVal lsString As Any, _
ByVal lplFilename As String) As Long
'This reads an entry in the INI file
'It takes as parameters the section name of the INI file,
'the name of the entry, its default value
'the string returned, its length and the INI filename.
Private Declare Function GetPrivateProfileString _
Lib "kernel32" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationKey As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Long, _
ByVal lpFileName As String) As Long
'Variable for the filename
Private m_strFile As String
'Variable for the section name
Private m_AppKey As String
Private Sub Class_Initialize()
m_strFile = ""
m_AppKey = ""
End Sub
Public Property Get FileName() As String
FileName = m_strFile
End Property
Public Property Let FileName(ByVal strFile As String)
m_strFile = Trim(strFile)
End Property
Public Property Get ApplicationKey() As String
ApplicationKey = m_AppKey
End Property
Public Property Let ApplicationKey(ByVal strAppKey As String)
m_AppKey = Trim(strAppKey)
End Property
Private Function Asc2String(str As String) As String
'Remove all unnecessary characters from the returned string
Dim I As Integer
Dim strOP As String
Dim intAsc As Integer
str = Trim(str)
strOP = ""
For I = 1 To Len(str)
intAsc = Asc(Mid(str, I, 1))
If intAsc > 31 Then strOP = strOP & Chr(intAsc)
Next
Asc2String = strOP
End Function
Public Function GetValue(ByVal keyName As String, ByVal strDefault As String) As String
'Returns the key value
Dim strVal As String * 1000
Dim ret As Long
ret = GetPrivateProfileString(m_AppKey, keyName, strDefault, strVal, Len(strVal), m_strFile)
If ret <> 0 Then
GetValue = Asc2String(strVal)
Else
GetValue = ""
End If
End Function
Public Function SetValue(ByVal keyName As String, ByVal strValue As String) As Boolean
'Sets the key value, returns True if succeeds
Dim ret As Long
ret = WritePrivateProfileString(m_AppKey, keyName, strValue, m_strFile)
If ret = 0 Then
SetValue = False
Exit Function
End If
SetValue = True
End Function |
Partager