Function SetRegistryValue(ByVal hKey As RootKeyEnum, ByVal KeyName As String, _
ByVal ValueName As String, ByVal Value As Variant, valueType As RegValueTypes, _
Optional Flag As RegFlags = 0) As Boolean
Dim handle As Long
Dim lngValue As Long
Dim strValue As String
Dim binValue() As Byte
Dim length As Long
Dim retVal As Long
Dim SecAttr As SECURITY_ATTRIBUTES '//键的安全设置
'//设置新键值的名称和默认安全设置
SecAttr.nLength = Len(SecAttr) '//结构大小
SecAttr.lpSecurityDescriptor = 0 '//默认安全权限
SecAttr.bInheritHandle = True '//设置的默认值
'// 打开或创建键
'If RegOpenKeyEx(hKey, KeyName, 0, KEY_ALL_ACCESS, handle) Then Exit Function
retVal = RegCreateKeyEx(hKey, KeyName, 0, vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, SecAttr, handle, retVal)
If retVal Then Exit Function
'//3种数据类型
Select Case VarType(Value)
Case vbByte, vbInteger, vbLong '// 若是字节, Integer值或Long值...
lngValue = Value
retVal = RegSetValueExLong(handle, ValueName, 0, REG_DWORD, lngValue, Len(lngValue))
Case vbString '// 字符串, 扩展环境字符串或多段字符串...
strValue = Value
Select Case Flag
Case IsExpandableString
retVal = RegSetValueEx(handle, ValueName, 0, REG_EXPAND_SZ, ByVal strValue, 255)
Case IsMultiString
retVal = RegSetValueEx(handle, ValueName, 0, REG_MULTI_SZ, ByVal strValue, 255)
Case Else '// 正常 REG_SZ 字符串
retVal = RegSetValueEx(handle, ValueName, 0, REG_SZ, ByVal strValue, 255)
End Select
Case vbArray + vbByte '// 如果是字节数组...
binValue = Value
length = UBound(binValue) - LBound(binValue) + 1
retVal = RegSetValueExByte(handle, ValueName, 0, REG_BINARY, binValue(0), length)
Case Else '// 如果其它类型
RegCloseKey handle
'Err.Raise 1001, , "不支持的值类型"
End Select
'// 返回关闭结果
RegCloseKey handle
'// 返回写入成功结果
SetRegistryValue = (retVal = 0)
End Function
Function GetRegistryValue(ByVal hKey As RootKeyEnum, ByVal KeyName As String, _
ByVal ValueName As String, Optional DefaultValue As Variant) As Variant
Dim handle As Long
Dim resLong As Long
Dim resString As String
Dim resBinary() As Byte
Dim length As Long
Dim retVal As Long
Dim valueType As Long