注册表(Registry)函数系列之一

Option Explicit
‘注册表操作
Public 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
Public Declare Function RegOpenKey Lib “advapi32.dll” Alias “RegOpenKeyA” (ByVal hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegQueryValueEx Lib “advapi32.dll” Alias “RegQueryValueExA” (ByVal hkey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Public Declare Function RegSetValueEx Lib “advapi32.dll” Alias “RegSetValueExA” (ByVal hkey&, ByVal lpValueName$, ByVal Reserved&, ByVal dwType&, ByVal lpData$, ByVal cbData&) As Long
Private Declare Function RegSetValue Lib “advapi32.dll” Alias “RegSetValueA” (ByVal hkey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
Private Declare Function RegDeleteValue Lib “advapi32.dll” Alias “RegDeleteValueA” (ByVal hkey As Long, ByVal lpValueName As String) As Long
Public Declare Function RegOpenKeyEx Lib “advapi32” Alias “RegOpenKeyExA” (ByVal hkey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
Public Declare Function RegCreateKeyEx Lib “advapi32.dll” Alias “RegCreateKeyExA” (ByVal hkey&, ByVal lpSubKey$, ByVal Reserved&, ByVal lpClass$, ByVal dwOptions&, ByVal samDesired&, ByVal SecAtts&, phkResult&, lpdwDisp&) As Long
Private Declare Function RegDeleteKey Lib “advapi32.dll” Alias “RegDeleteKeyA” (ByVal hkey As Long, ByVal lpSubKey As String) As Long
Public Declare Function SHDeleteKey Lib “shlwapi.dll” Alias “SHDeleteKeyA” (ByVal hkey As Long, ByVal pszSubKey As String) As Long

Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_DYN_DATA = &H80000006
Public Const KEY_ALL_ACCESS = (&H1F0000 Or &H1 Or &H2 Or &H4 Or &H8 Or &H10 Or &H20) And (Not &H100000)
Public Const REG_NONE = (0)
Public Const REG_SZ = (1) ‘REG_SZ ——键值存储为字符串值
Public Const REG_EXPAND_SZ = (2)
Public Const REG_BINARY = (3) ‘REG_BINARY ——键值存储为二进制值
Public Const REG_DWORD = (4) ‘REG_DWORD ——键值存储为DWORD值(双字)
Public Const REG_DWORD_BIG_ENDIAN = (5)
Public Const REG_LINK = (6)
Public Const REG_MULTI_SZ = (7)
Public Const REG_RESOURCE_LIST = (8)
Public Const REG_FULL_RESOURCE_DESCRIPTOR = (9)
Public Const REG_RESOURCE_REQUIREMENTS_LIST = (10)

Function RegQueryStringValue(ByVal hkey As Long, ByVal sValueName As String) As String
On Error Resume Next
Dim lResult As Long, lValueType As Long, sBuf As String, lDataBufSize As Long
‘retrieve nformation about the key
lResult = RegQueryValueEx(hkey, sValueName, 0, lValueType, ByVal 0, lDataBufSize)
If lResult = 0 Then
If lValueType = REG_SZ Then
‘Create a buffer
sBuf = String(lDataBufSize, Chr$(0))
‘retrieve the key’s content
lResult = RegQueryValueEx(hkey, sValueName, 0, 0, ByVal sBuf, lDataBufSize)
If lResult = 0 Then
‘Remove the unnecessary chr$(0)’s
‘RegQueryStringValue = Left$(sBuf, InStr(1, sBuf, Chr$(0)) – 1)
‘Avoid making mistakes when chr$(0) is zero
RegQueryStringValue = GetAPIString(sBuf)
End If
ElseIf lValueType = REG_BINARY Then
Dim sData As Integer
‘retrieve the key’s value
lResult = RegQueryValueEx(hkey, sValueName, 0, 0, sData, lDataBufSize)
If lResult = 0 Then
RegQueryStringValue = sData
End If
End If
End If
On Error GoTo 0
End Function

Function GetString(ByVal hkey As Long, sPath As String, ByVal sValue As String, ByVal sApp As Variant, ByVal sIniFile As Variant) ‘从注册表读取数据, 如果失败则从初始化文件读取
On Error Resume Next
Dim lResult1, lResult2
‘Open the key
lResult1 = RegOpenKey(hkey, sPath, lRet)
‘Get the key’s content
lResult2 = RegQueryStringValue(lRet, sValue)
GetString = lResult2
‘Close the key
RegCloseKey lRet
On Error GoTo 0
‘OS is diversity, determine if registry success
If IsMissing(sApp) Then Exit Function
If IsMissing(sIniFile) Then Exit Function
If lResult1 = 0 And Len(lResult2) > 0 And Not ChineseExist(lResult2) Then Exit Function ‘含汉字字符串时不一定对
On Error Resume Next
‘Read ini file, when operating Registry is fail.
GetString = ReadIniFile(sApp, sValue, sIniFile)
On Error GoTo 0
End Function

Sub SaveString(ByVal hkey As Long, ByVal sPath As String, ByVal sValue As String, ByVal sData As String, ByVal sApp As Variant, ByVal sIniFile As Variant) ‘向注册表写入数据, 如果失败则向初始化文件写入
On Error Resume Next
Dim lResult1, lResult2
‘Create a new key
lResult1 = RegCreateKey(hkey, sPath, lRet)
‘Save a string to the key
lResult2 = RegSetValueEx(lRet, sValue, 0, REG_SZ, ByVal sData, Len(sData))
‘close the key
RegCloseKey lRet
On Error GoTo 0
‘Debug.Print lResult1
‘Debug.Print lResult2
‘OS is diversity, determine if registry success
If IsMissing(sApp) Then Exit Function
If IsMissing(sIniFile) Then Exit Function
If lResult1 = 0 And lResult2 = 0 And Not ChineseExist(sData) Then Exit Sub ‘含汉字字符串时结果有问题
On Error Resume Next
‘Write ini file, when operating Registry is fail.
WriteIniFile sApp, sValue, sData, sIniFile
WritePrivateProfileString sApp, sValue, sData, sIniFile
On Error GoTo 0
End Sub

Sub DelSetting(ByVal hkey As Long, ByVal sPath As String, ByVal sValue As String, ByVal sApp As Variant, ByVal sIniFile As Variant) ‘删除注册表数据, 如果失败则删除初始化文件数据
On Error Resume Next
‘Debug.Print hKey
‘Debug.Print sPath
‘Debug.Print sValue
Dim lResult1, lResult2
‘Create a new key
lResult1 = RegCreateKey(hkey, sPath, lRet)
‘Delete the key’s value
lResult2 = RegDeleteValue(lRet, sValue)
‘close the key
RegCloseKey lRet
On Error GoTo 0
‘Debug.Print lResult1
‘Debug.Print lResult2
‘OS is diversity, determine if registry success
If IsMissing(sApp) Then Exit Function
If IsMissing(sIniFile) Then Exit Function
If lResult1 = 0 And hkey = HKEY_LOCAL_MACHINE Then Exit Sub ‘lResult2 = 2 or 5 含汉字字符串时须两处删除
On Error Resume Next
‘Write ini file, when operating Registry is fail.
WriteIniFile sApp, sValue, vbNullString, sIniFile
WritePrivateProfileString sApp, sValue, vbNullString, sIniFile
On Error GoTo 0
End Sub

Public Sub DelKey(hkey As Long, sKey As String)
‘Delete the key
lRet = RegDeleteKey(hkey, sKey)
End Sub

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注