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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
| <?
Option Explicit
'******************************************************************************************
' Declarations
'******************************************************************************************
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
Private Declare Function RegQueryValueExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByRef lpData As Long, lpcbData As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
Private Declare Function RegSetValueExA Lib "advapi32.dll" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByRef lpData As Long, ByVal cbData As Long) As Long
Private Declare Function RegSetValueExB Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByRef lpData As Byte, ByVal cbData As Long) As Long
'******************************************************************************************
' Enums
'******************************************************************************************
Const cHKEY_CLASSES_ROOT = &amp;H80000000
Const cHKEY_CURRENT_USER = &amp;H80000001
Const cHKEY_LOCAL_MACHINE = &amp;H80000002
Const cHKEY_USERS = &amp;H80000003
Const cHKEY_PERFORMANCE_DATA = &amp;H80000004
Const cHKEY_CURRENT_CONFIG = &amp;H80000005
Const cHKEY_DYN_DATA = &amp;H80000006
Public Enum RegistryClassConstants
HKEY_CLASSES_ROOT = cHKEY_CLASSES_ROOT
HKEY_CURRENT_USER = cHKEY_CURRENT_USER
HKEY_LOCAL_MACHINE = cHKEY_LOCAL_MACHINE
HKEY_USERS = cHKEY_USERS
HKEY_PERFORMANCE_DATA = cHKEY_PERFORMANCE_DATA
HKEY_CURRENT_CONFIG = cHKEY_CURRENT_CONFIG
HKEY_DYN_DATA = cHKEY_DYN_DATA
End Enum
'******************************************************************************************
' Constants
'******************************************************************************************
'------------------------------------------------------------------------------------------
' Reg Key Errors
'------------------------------------------------------------------------------------------
Private Const ERROR_SUCCESS = 0&amp;
Private Const ERROR_BADDB = 1009&amp;
Private Const ERROR_BADKEY = 1010&amp;
Private Const ERROR_CANTOPEN = 1011&amp;
Private Const ERROR_CANTREAD = 1012&amp;
Private Const ERROR_CANTWRITE = 1013&amp;
Private Const ERROR_OUTOFMEMORY = 14&amp;
Private Const ERROR_INVALID_PARAMETER = 87&amp;
Private Const ERROR_ACCESS_DENIED = 5&amp;
Private Const ERROR_NO_MORE_ITEMS = 259&amp;
Private Const ERROR_MORE_DATA = 234&amp;
'------------------------------------------------------------------------------------------
' Reg Key Types
'------------------------------------------------------------------------------------------
Private Const REG_NONE = 0&amp;
Private Const REG_SZ = 1&amp;
Private Const REG_EXPAND_SZ = 2&amp;
Private Const REG_BINARY = 3&amp;
Private Const REG_DWORD = 4&amp;
Private Const REG_DWORD_LITTLE_ENDIAN = 4&amp;
Private Const REG_DWORD_BIG_ENDIAN = 5&amp;
Private Const REG_LINK = 6&amp;
Private Const REG_MULTI_SZ = 7&amp;
Private Const REG_RESOURCE_LIST = 8&amp;
Private Const REG_FULL_RESOURCE_DESCRIPTOR = 9&amp;
Private Const REG_RESOURCE_REQUIREMENTS_LIST = 10&amp;
'------------------------------------------------------------------------------------------
' Reg Key Security Options
'------------------------------------------------------------------------------------------
Private Const KEY_QUERY_VALUE = &amp;H1&amp;
Private Const KEY_SET_VALUE = &amp;H2&amp;
Private Const KEY_CREATE_SUB_KEY = &amp;H4&amp;
Private Const KEY_ENUMERATE_SUB_KEYS = &amp;H8&amp;
Private Const KEY_NOTIFY = &amp;H10&amp;
Private Const KEY_CREATE_LINK = &amp;H20&amp;
Private Const READ_CONTROL = &amp;H20000
Private Const WRITE_DAC = &amp;H40000
Private Const WRITE_OWNER = &amp;H80000
Private Const SYNCHRONIZE = &amp;H100000
Private Const STANDARD_RIGHTS_REQUIRED = &amp;HF0000
Private Const STANDARD_RIGHTS_READ = READ_CONTROL
Private Const STANDARD_RIGHTS_WRITE = READ_CONTROL
Private Const STANDARD_RIGHTS_EXECUTE = READ_CONTROL
Private Const STANDARD_RIGHTS_ALL = &amp;H1F0000
Private Const KEY_READ = STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY
Private Const KEY_WRITE = STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY
Private Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK) And (Not SYNCHRONIZE))
Private Const KEY_EXECUTE = KEY_READ
Private Const defCompany As String = "Woudloper development" 'The default name...
'******************************************************************************************
' Variables
'******************************************************************************************
Dim hKey As Long, MainKeyHandle As Long
Dim rtn As Long, lBuffer As Long, sBuffer As String
Dim lBufferSize As Long
Dim lDataSize As Long
Dim ByteArray() As Byte
Private m_Company As String
Private m_AppName As String
'******************************************************************************************
' Initialize and Terminate
'******************************************************************************************
Private Sub Class_Initialize()
m_Company = defCompany
m_AppName = App.ProductName
End Sub
'******************************************************************************************
' Properties
'******************************************************************************************
Public Property Let Company(ByVal NewVal As String)
If Len(NewVal) Then
m_Company = Trim(NewVal)
Else
m_Company = defCompany
End If
End Property
Public Property Get Company() As String
Company = m_Company
End Property
Public Property Let ApplicationName(ByVal NewVal As String)
If Len(NewVal) Then
m_AppName = Trim(NewVal)
Else
m_AppName = App.ProductName
End If
End Property
Public Property Get ApplicationName() As String
ApplicationName = m_AppName
End Property
'******************************************************************************************
' Public Functions
'******************************************************************************************
Public Function GetStringValue(Location As RegistryClassConstants, Section As String, Entry As String, Optional ByVal Default As String = "") As String
GetStringValue = Default 'Before a failure happens give the
'default value to the function.
If Location Then
rtn = RegOpenKeyEx(Location, SubKey(Section), 0, KEY_READ, hKey) 'open the key
If rtn = ERROR_SUCCESS Then 'if the key could be opened then
sBuffer = Space(255) 'make a buffer
lBufferSize = Len(sBuffer)
rtn = RegQueryValueEx(hKey, Entry, 0, REG_SZ, sBuffer, lBufferSize) 'get the value from the registry
If rtn = ERROR_SUCCESS Then 'if the value could be retreived then
rtn = RegCloseKey(hKey) 'close the key
sBuffer = Trim(sBuffer)
GetStringValue = Left(sBuffer, Len(sBuffer) - 1) 'return the value to the user
End If
End If
End If
End Function
Public Function SetStringValue(Location As RegistryClassConstants, Section As String, Entry As String, Value As String)
If Location Then
rtn = RegOpenKeyEx(Location, SubKey(Section), 0, KEY_WRITE, hKey) 'Open the key
If rtn = ERROR_SUCCESS Then 'If the key was open successfully then
Value = Value &amp; vbNullChar 'Null-terminate setting, in case it's empty.
rtn = RegSetValueEx(hKey, Entry, 0, REG_SZ, ByVal Value, Len(Value)) 'write the value
rtn = RegCloseKey(hKey) 'close the key
End If
End If
End Function
Public Function GetDWORDValue(Location As RegistryClassConstants, Section As String, Entry As String, Optional ByVal Default As Long = 0) As Long
GetDWORDValue = Default
If Location Then
rtn = RegOpenKeyEx(Location, SubKey(Section), 0, KEY_READ, hKey) 'open the key
If rtn = ERROR_SUCCESS Then 'if the key could be opened then
rtn = RegQueryValueExA(hKey, Entry, 0, REG_DWORD, lBuffer, 4) 'get the value from the registry
If rtn = ERROR_SUCCESS Then 'if the value could be retreived then
rtn = RegCloseKey(hKey) 'close the key
GetDWORDValue = lBuffer 'return the value
End If
End If
End If
End Function
Public Function SetDWORDValue(Location As RegistryClassConstants, Section As String, Entry As String, Value As Long)
'------------------------------------------------------------------------------------------
' Description: This function writes a DWORD value to the users registry.
' Input params: Location; In which part you want to write the key.
' Section; What would the section name be?
' Entry; The entry name..
' Value; The value you want to write...
' Syntax: SetDWORDValue HKEY_CURRENT_USER, "General", "The DWORD Value", 6
'------------------------------------------------------------------------------------------
If Location Then
rtn = RegOpenKeyEx(Location, SubKey(Section), 0, KEY_WRITE, hKey) 'open the key
If rtn = ERROR_SUCCESS Then 'if the key was open successfully then
rtn = RegSetValueExA(hKey, Entry, 0, REG_DWORD, Value, 4) 'write the value
rtn = RegCloseKey(hKey) 'close the key
End If
End If
End Function
Function GetBinaryValue(Location As RegistryClassConstants, Section As String, Entry As String, Optional ByVal Default As String)
GetBinaryValue = Default
If Location Then
rtn = RegOpenKeyEx(Location, SubKey(Section), 0, KEY_READ, hKey) 'open the key
If rtn = ERROR_SUCCESS Then 'if the key could be opened
lBufferSize = 1
rtn = RegQueryValueEx(hKey, Entry, 0, REG_BINARY, 0, lBufferSize) 'get the value from the registry
sBuffer = Space(lBufferSize)
rtn = RegQueryValueEx(hKey, Entry, 0, REG_BINARY, sBuffer, lBufferSize) 'get the value from the registry
If rtn = ERROR_SUCCESS Then 'if the value could be retreived then
rtn = RegCloseKey(hKey) 'close the key
GetBinaryValue = sBuffer 'return the value to the user
End If
End If
End If
End Function
Function SetBinaryValue(Location As RegistryClassConstants, Section As String, Entry As String, Value As String)
Dim lngCounter As Long
If Location Then
rtn = RegOpenKeyEx(Location, SubKey(Section), 0, KEY_WRITE, hKey) 'open the key
If rtn = ERROR_SUCCESS Then 'if the key was open successfully then
lDataSize = Len(Value)
ReDim ByteArray(lDataSize)
For lngCounter = 1 To lDataSize
ByteArray(lngCounter) = Asc(Mid$(Value, lngCounter, 1))
Next
rtn = RegSetValueExB(hKey, Entry, 0, REG_BINARY, ByteArray(1), lDataSize) 'write the value
End If
rtn = RegCloseKey(hKey) 'close the key
End If
End Function
Public Function DeleteSetting(Location As RegistryClassConstants, Section As String, Optional ByVal Entry As String = "") As Boolean
'------------------------------------------------------------------------------------------
' Section Required. String expression containing the name of the section where the key setting
' is being deleted. If only section is provided, the specified section is deleted along
' with all related key settings.
' Key Optional. String expression containing the name of the key setting being deleted.
'------------------------------------------------------------------------------------------
Dim nRet As Long
Dim hKey As Long
If Len(Entry) Then
' Open key
nRet = RegOpenKeyEx(Location, SubKey(Section), 0&amp;, KEY_ALL_ACCESS, hKey)
If nRet = ERROR_SUCCESS Then
' Set appropriate value for default query
If Entry = "*" Then Entry = vbNullString
' Delete the requested value
nRet = RegDeleteValue(hKey, Entry)
Call RegCloseKey(hKey)
End If
Else
' Open parent key
nRet = RegOpenKeyEx(Location, SubKey(), 0&amp;, KEY_ALL_ACCESS, hKey)
If nRet = ERROR_SUCCESS Then
' Attempt to delete whole section
nRet = RegDeleteKey(hKey, Section)
Call RegCloseKey(hKey)
End If
End If
DeleteSetting = (nRet = ERROR_SUCCESS)
End Function
'******************************************************************************************
' Private Functions/Subs
'******************************************************************************************
Private Function SubKey(Optional ByVal Section As String = "") As String
'------------------------------------------------------------------------------------------
' Description: Build SubKey from known values.
' Syntax: SubKey("General") = SoftwareWoudloper DevelopmentRegistry Test ApplicationGeneral
'------------------------------------------------------------------------------------------
SubKey = "Software\" &amp; m_Company &amp; "\" &amp; m_AppName
If Len(Section) Then
SubKey = SubKey &amp; "\" &amp; Section
End If
End Function
?> |