-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcINIfile.cls
233 lines (184 loc) · 5.89 KB
/
cINIfile.cls
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "cINIfile"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'
' Returns:None
'
'Assumes:Save the code into a file calle
' d cIniFile.cls and add it to your projec
' t. Follow the sample code in the top com
' ment block to try it out.
'
'Side Effects:None.
'This code is copyrighted and has limite
' d warranties.
'Please see http://www.Planet-Source-Cod
' e.com/xq/ASP/txtCodeId.608/lngWId.1/qx/v
' b/scripts/ShowCode.htm
'for details.
'**************************************
Option Explicit
' **************************************
' ****************************************
' *******
' Description:
' A complete class for access to Ini Fil
' es. Works in
' VB4 16 and 32 and VB5.
'
' Sample code: find out whether we are r
' unning the Windows
' 95 shell or not:
'
' dim cIni as new cIniFile
' with cIni
'.Path = "C:\WINDOWS\SYSTEM.INI" ' Use G
' etWindowsDir() call to find the correct
' dir
'.Section = "boot"
'.Key = "shell"
'if (ucase$(trim$(.Value)) = "EXPLORER.E
' XE") then
'msgbox "Da Shell is here",vbInformation
'
'else
'msgbox "Da Computer is too old..",vbExc
' lamation
'endif
' end with
'
' FileName: cIniFile.Cls
' Author:Steve McMahon (Steve-McMahon@pa
' -consulting.com)
' Date: 30 June 1997
' **************************************
' ****************************************
' *******
' Private variables to store the setting
' s made:
Private m_sPath As String
Private m_sKey As String
Private m_sSection As String
Private m_sDefault As String
Private m_lLastReturnCode As Long
' Declares for cIniFile:
#If Win32 Then
' Profile String functions:
Private Declare Function WritePrivateProfileString Lib "KERNEL32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As String, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileString Lib "KERNEL32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As Any, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
#Else
' Profile String functions:
' If you are developing in VB5, delete t
' his section
' otherwise SetupKit gets **confused**!
Private Declare Function WritePrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Integer
Private Declare Function GetPrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As Any, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
#End If
Property Get LastReturnCode() As Long
' Did the last call succeed?
' 0 if not!
LastReturnCode = m_lLastReturnCode
End Property
Property Let Default(sDefault As String)
' What to return if something goes wrong
' :
m_sDefault = sDefault
End Property
Property Get Default() As String
' What to return if something goes wrong
' :
Default = m_sDefault
End Property
Property Let Path(sPath As String)
' The filename of the INI file:
m_sPath = sPath
End Property
Property Get Path() As String
' The filename of the INI file:
Path = m_sPath
End Property
Property Let Key(sKey As String)
' The KEY= bit to look for
m_sKey = sKey
End Property
Property Get Key() As String
' The KEY= bit to look for
Key = m_sKey
End Property
Property Let Section(sSection As String)
' The [SECTION] bit to look for
m_sSection = sSection
End Property
Property Get Section() As String
' The [SECTION] bit to look for
Section = m_sSection
End Property
Property Get Value() As String
' Get the value of the current Key withi
' n Section of Path
Dim sBuf As String
Dim iSize As String
Dim iRetCode As Integer
sBuf = Space$(255)
iSize = Len(sBuf)
iRetCode = GetPrivateProfileString(m_sSection, m_sKey, m_sDefault, sBuf, iSize, m_sPath)
If (iSize > 0) Then
Value = Left$(sBuf, iRetCode)
Else
Value = ""
End If
End Property
Property Let Value(sValue As String)
' Set the value of the current Key withi
' n Section of Path
Dim iPos As Integer
' Strip chr$(0):
iPos = InStr(sValue, Chr$(0))
Do While iPos <> 0
sValue = Left$(sValue, (iPos - 1)) & Mid$(sValue, (iPos + 1))
iPos = InStr(sValue, Chr$(0))
Loop
m_lLastReturnCode = WritePrivateProfileString(m_sSection, m_sKey, sValue, m_sPath)
End Property
Public Sub DeleteValue()
' Delete the value at Key within Section
' of Path
m_lLastReturnCode = WritePrivateProfileString(m_sSection, m_sKey, 0&, m_sPath)
End Sub
Public Sub DeleteSection()
' Delete the Section in Path
m_lLastReturnCode = WritePrivateProfileString(m_sSection, 0&, 0&, m_sPath)
End Sub
Property Get INISection() As String
' Return all the keys and values within
' the current
' section, separated by chr$(0):
Dim sBuf As String
Dim iSize As String
Dim iRetCode As Integer
sBuf = Space$(255)
iSize = Len(sBuf)
iRetCode = GetPrivateProfileString(m_sSection, 0&, m_sDefault, sBuf, iSize, m_sPath)
If (iSize > 0) Then
INISection = Left$(sBuf, iRetCode)
Else
INISection = ""
End If
End Property
Property Let INISection(sSection As String)
' Set one or more the keys within the cu
' rrent section.
' Keys and Values should be separated by
' chr$(0):
m_lLastReturnCode = WritePrivateProfileString(m_sSection, 0&, sSection, m_sPath)
End Property