1,486
社区成员
发帖
与我相关
我的任务
分享
Private Function SetValueEx(ByVal lngHwdKey As Long, ByVal strValueName _
As String, ByVal lngType As Long, ByVal varValueData As Variant) _
As Long
'Declare variables
Dim lngValue As Long
Dim strValue As String
'Determine the size and type of data to be written
Select Case lngType
'Strings
Case REG_SZ
strValue = varValueData & Chr$(0)
SetValueEx = RegSetValueExString(lngHwdKey, strValueName, _
0&, lngType, strValue, Len(strValue))
'DWORDs
Case REG_DWORD
lngValue = varValueData
SetValueEx = RegSetValueExLong(lngHwdKey, strValueName, _
0&, lngType, lngValue, 4)
End Select
End Function
Public Function QueryKeyValue(ByVal strSectionName As String, ByVal _
strKeyName As String, Optional ByVal lngRegDataType As RegDataType _
= HKEY_LOCAL_MACHINE) As Long
'Declare variables
Dim lngRC As Long
Dim lngHwd As Long
Dim varValue As Variant
'Get the value currently in the key and close it
lngRC = RegOpenKeyEx(lngRegDataType, strSectionName, 0, _
KEY_ALL_ACCESS, lngHwd)
m_strKeyHandle = lngHwd 'set key handle
lngRC = QueryValueEx(lngHwd, strKeyName, varValue)
RegCloseKey (lngHwd)
'Return with the return code
QueryKeyValue = lngRC
End Function
Private Function QueryValueEx(ByVal lngHwdKey As Long, ByVal strKeyName _
As String, ByVal varValue As Variant) As Long
'Declare variables
Dim lngDataLen As Long
Dim lngRC As Long
Dim lngType As Long
Dim lngTemp As Long
Dim strTemp As String
On Error GoTo QueryErr
'Determine the size and type of data to be read
lngRC = RegQueryValueExNULL(lngHwdKey, strKeyName, 0&, lngType, _
0&, lngDataLen)
If lngRC <> 0 Then Error 5
Select Case lngType
'Strings
Case REG_SZ:
strTemp = String(lngDataLen, 0)
lngRC = RegQueryValueExString(lngHwdKey, strKeyName, 0&, _
lngType, strTemp, lngDataLen)
If lngRC = 0 Then
m_strKeyValue = Left$(strTemp, lngDataLen - 1)
Else
m_strKeyValue = Empty
End If
'DWORDs
Case REG_DWORD:
lngRC = RegQueryValueExLong(lngHwdKey, strKeyName, 0&, _
lngType, lngTemp, lngDataLen)
If lngRC = 0 Then
m_lngKeyValue = lngTemp
Else
m_lngKeyValue = -1
End If
'All other data types
Case Else
lngRC = -1
End Select
On Error GoTo 0
QueryValueEx = lngRC
Exit Function
QueryErr:
lngRC = -1
QueryValueEx = lngRC
End Function
Public Function DeleteKey(lngRegDataType As Long, strKey As String) As Long
Dim lngValue As Long
lngValue = RegDeleteKey(lngRegDataType, strKey)
DeleteKey = lngValue
End Function
Public Function DeleteKeyValue(ByVal strSectionName As String, ByVal _
strKeyName As String, Optional ByVal lngRegDataType As RegDataType _
= HKEY_LOCAL_MACHINE) As Long
'Declare variables
Dim lngRC As Long
Dim lngHwd As Long
Dim varValue As Variant
'Get the value currently in the key and close it
lngRC = RegOpenKeyEx(lngRegDataType, strSectionName, 0, _
KEY_ALL_ACCESS, lngHwd)
lngRC = RegDeleteValue(lngHwd, strKeyName)
RegCloseKey (lngHwd)
'Return with the return code
DeleteKeyValue = lngRC
End Function
Private Sub Class_Initialize()
'MsgBox "class init"
End Sub
Private Sub Class_Terminate()
'MsgBox "class terminate"
End Sub
'Declare private variables
Private m_lngKeyValue As Long
Private m_strKeyValue As String
Private m_strKeyHandle As Long
'Declare enumerations
Public Enum RegDataType
HKEY_CLASSES_ROOT = &H80000000
HKEY_CURRENT_USER = &H80000001
HKEY_LOCAL_MACHINE = &H80000002
HKEY_USERS = &H80000003
HKEY_PERFORMANCE_DATA = &H80000004
HKEY_CURRENT_CONFIG = &H80000005
HKEY_DYN_DATA = &H80000006
End Enum
使用示例
[code=VB]
Private Sub Command1_Click()
Dim i As Long
Set cReg = New clsRegistry
i = cReg.CreateKey("Network\", "Temp", HKEY_CURRENT_USER)
If i = 0 Then
MsgBox "New key created."
Else
MsgBox "Couldn't create key."
End If
Set cReg = Nothing
End Sub
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 RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Sub Command1_Click()
openregery
End Sub
Private Sub openregery()
Dim ret As Long, hKey As Long, hKey2 As Long
ret = RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft", hKey)
If ret = 0 Then
MsgBox "HKLM\SOFTWARE\Microsoft = " & hKey
End If
ret = RegOpenKey(hKey, "Windows\CurrentVersion", hKey2)
If ret = 0 Then
MsgBox "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion = " & hKey2
End If
'Use RegCreateKey function to create subkey "HKEY_LOCAL_MACHINE\SOFTWARE\Hongqt"
ret = RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\Hongqt", hKey)
If Not ret Then
MsgBox "新建 HKEY_LOCAL_MACHINE\SOFTWARE\Hongqt SubKey 成功"
Else
MsgBox "新建子键的操作失败"
End If
RegCloseKey hKey
RegCloseKey hKey2
End Sub