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 ERROR_SUCCESS = 0&
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
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
Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Private Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As Any) As Long
Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Public Const REG_SZ = 1 ' Unicode nul terminated string
Public Const REG_DWORD = 4 ' 32-bit number
'读写注册表
'读写注册表
Public Function GetString(hKey As Long, strPath As String, strValue As String)
Dim keyhand As Long
Dim datatype As Long
Dim lResult As Long
Dim strBuf As String
Dim lDataBufSize As Long
Dim intZeroPos As Integer
r = RegOpenKey(hKey, strPath, keyhand)
lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
If lValueType = REG_SZ Then
strBuf = String(lDataBufSize, " ")
lResult = RegQueryValueEx(keyhand, strValue, 0&, 0&, ByVal strBuf, lDataBufSize)
If lResult = ERROR_SUCCESS Then
intZeroPos = InStr(strBuf, Chr$(0))
If intZeroPos > 0 Then
GetString = Left$(strBuf, intZeroPos - 1)
Else
GetString = strBuf
End If
End If
End If
End Function
Public Sub SaveString(hKey As Long, strPath As String, strValue As String, strdata As String)
Dim keyhand As Long
Dim r As Long
r = RegCreateKey(hKey, strPath, keyhand)
r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strdata, Len(strdata))
r = RegCloseKey(keyhand)
End Sub
Public Function DelString(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String)
Dim keyhand As Long
Dim r As Long
r = RegOpenKey(hKey, strPath, keyhand)
r = RegDeleteValue(keyhand, strValue)
r = RegCloseKey(keyhand)
End Function
Public Function DelKeys(ByVal hKey As Long, ByVal strPath As String) ', ByVal strKeys As String)
Dim r As Long
r = RegDeleteKey(hKey, strPath)
End Function
Public Function CreateKeys(ByVal hKey As Long, ByVal strPath As String)
Dim keyhand As Long
Dim r As Long
r = RegCreateKey(hKey, strPath, keyhand)
r = RegCloseKey(keyhand)
End Function
Public Function ListSubkey(ByVal hKey As Long, ByVal strPath As String) As String
Dim keyhand As Long
Dim r As Long
Dim strlistsubkey As String
Dim sName As String, sData As String, Ret As Long, RetData As Long
Dim indexs As Long
Const ERROR_NO_MORE_ITEMS = 259&
Const BUFFER_SIZE As Long = 255
Ret = BUFFER_SIZE
sName = Space(BUFFER_SIZE)
r = RegOpenKey(hKey, strPath, keyhand)
While RegEnumKeyEx(keyhand, indexs, sName, Ret, ByVal 0&, vbNullString, ByVal 0&, ByVal 0&) <> ERROR_NO_MORE_ITEMS
If strlistsubkey = "" Then
strlistsubkey = Left$(sName, Ret)
Else
strlistsubkey = strlistsubkey + "_" + Left$(sName, Ret)
End If
indexs = indexs + 1
Wend
ListSubkey = strlistsubkey
End Function
'======API声明======
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String, phkResult 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 RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
ByVal dwType As Long, lpData As Any, ByVal cbData As Long) 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, lpData As Any, lpcbData As Long) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" _
(ByVal hKey As Long, ByVal lpSubKey As String) As Long
'======方法定义======
'创建一个项
Public Function CreateRegKey(ByVal vSubKey As String) As Boolean
On Error GoTo ErrH
Dim nResult As Long
'检查传入的值是否合法
If Len(vSubKey) = 0 Then GoTo ErrH
'在注册表中创建一个项
nResult = RegCreateKey(HKEY_CURRENT_USER, vSubKey, nResult)
If nResult <> 0 Then GoTo ErrH
CreateRegKey = True
Exit Function
ErrH:
CreateRegKey = False
End Function
'删除一个最底层项
Public Function DeleteRegKey(ByVal vSubKey As String) As Boolean
On Error GoTo ErrH
Dim nResult As Long
'检查传入的值是否合法
If Len(vSubKey) = 0 Then GoTo ErrH
'在注册表中删除一个项
nResult = RegDeleteKey(HKEY_CURRENT_USER, vSubKey)
If nResult <> 0 Then GoTo ErrH
DeleteRegKey = True
Exit Function
ErrH:
DeleteRegKey = False
End Function
'在指定的项中写入一个值
Public Function SetRegValue(ByVal vSubKey As String, ByVal vKeyName As String, ByVal vKeyValue As String) As Boolean
On Error GoTo ErrH
Dim nResult As Long
Dim nKey As Long
'检查传入的值是否合法
If Len(vSubKey) = 0 Then GoTo ErrH
If Len(vKeyName) = 0 Then GoTo ErrH
If Len(vKeyValue) = 0 Then GoTo ErrH
'检查是否存在vSubKey项,成功会返回一个nKey值
nResult = RegOpenKey(HKEY_CURRENT_USER, vSubKey, nKey)
If nResult <> 0 Then GoTo ErrH
'根据nKey键值,写入键名及键值
SetRegValue = RegSetValueEx(nKey, vKeyName, 0, REG_SZ, ByVal vKeyValue, LenB(StrConv(vKeyValue, vbFromUnicode)) + 1)
If SetRegValue <> 0 Then GoTo ErrH
SetRegValue = True
Exit Function
ErrH:
SetRegValue = False
End Function
'查找指定项中指定键值名称的键值
'如果注册表中没有指定的项值vSubKey,lpcbData参数返回0
'如果有项值但没有键名vKeyName,lpcbData参数返回2
'如果vKeyValue为一个字符,lpcbData参数返回2
'如果vKeyValue大于一个字符,lpcbData参数返回>2
Public Function GetRegValue(ByVal vSubKey As String, ByVal vKeyName As String) As String
On Error GoTo ErrH
Dim nResult As Long, nKey As Long, nKeyValue As String
Dim nBufferSize As Long
'检查传入的参数是否合法
If Len(vSubKey) = 0 Then GoTo ErrH
If Len(vKeyName) = 0 Then GoTo ErrH
'打开指定的项,成功会返回一个nKey值
nResult = RegOpenKey(HKEY_CURRENT_USER, vSubKey, nKey)
If nResult <> 0 Then GoTo ErrH
'删除指定项中的一个键
Public Function DeleteRegValue(ByVal vSubKey As String, ByVal vKeyName As String) As Boolean
On Error GoTo ErrH
Dim nResult As Long
Dim nKey As Long
'检查传入的参数是否合法
If Len(vSubKey) = 0 Then GoTo ErrH
If Len(vKeyName) = 0 Then GoTo ErrH
'打开指定的项,成功会返回一个nKey值
nResult = RegOpenKey(HKEY_CURRENT_USER, vSubKey, nKey)
If nResult <> 0 Then GoTo ErrH
'根据nKey删除指定的键
nResult = RegDeleteValue(nKey, vKeyName)
If nResult <> 0 Then GoTo ErrH
DeleteRegValue = True
Exit Function