-
Mar 5th, 2022, 10:23 AM
#1
Thread Starter
PowerPoster
how to read registry all valus or keys?RegEnumKeyEx,RegEnumValue
how to read all keys or valus?
Code:
' This is a Bas file for operating the registry, which contains functions that can create new key values, delete key values, and query key values. Powered by barenx
Option Explicit
Public 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 Byte, lpcbData As Long) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hkey As Long) As Long
Public Declare Function RegConnectRegistry Lib "advapi32.dll" Alias "RegConnectRegistryA" (ByVal lpMachineName As String, ByVal hkey As Long, phkResult As Long) As Long
Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long
Public Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hkey As Long, ByVal lpSubKey As String) As Long
Public Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hkey As Long, ByVal lpValueName As String) As Long
Public Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hkey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
Public 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 FILETIME) As Long
Public 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 Byte, lpcbData As Long) As Long
Public Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hkey As Long) As Long
Public Declare Function RegGetKeySecurity Lib "advapi32.dll" (ByVal hkey As Long, ByVal SecurityInformation As Long, pSecurityDescriptor As SECURITY_DESCRIPTOR, lpcbSecurityDescriptor As Long) As Long
Public Declare Function RegLoadKey Lib "advapi32.dll" Alias "RegLoadKeyA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal lpFile As String) As Long
Public Declare Function RegNotifyChangeKeyValue Lib "advapi32.dll" (ByVal hkey As Long, ByVal bWatchSubtree As Long, ByVal dwNotifyFilter As Long, ByVal hEvent As Long, ByVal fAsynchronus 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 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
Public Declare Function RegQueryInfoKey Lib "advapi32.dll" Alias "RegQueryInfoKeyA" (ByVal hkey As Long, ByVal lpClass As String, lpcbClass As Long, ByVal lpReserved As Long, lpcSubKeys As Long, lpcbMaxSubKeyLen As Long, lpcbMaxClassLen As Long, lpcValues As Long, lpcbMaxValueNameLen As Long, lpcbMaxValueLen As Long, lpcbSecurityDescriptor As Long, lpftLastWriteTime As FILETIME) As Long
Public Declare Function RegQueryValue Lib "advapi32.dll" Alias "RegQueryValueA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal lpValue As String, lpcbValue 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 RegReplaceKey Lib "advapi32.dll" Alias "RegReplaceKeyA" (ByVal hkey As Long, ByVal lpSubKey As String, ByVal lpNewFile As String, ByVal lpOldFile As String) As Long
Public Declare Function RegRestoreKey Lib "advapi32.dll" Alias "RegRestoreKeyA" (ByVal hkey As Long, ByVal lpFile As String, ByVal dwFlags As Long) As Long
Public Declare Function RegSaveKey Lib "advapi32.dll" Alias "RegSaveKeyA" (ByVal hkey As Long, ByVal lpFile As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
Public Declare Function RegSetKeySecurity Lib "advapi32.dll" (ByVal hkey As Long, ByVal SecurityInformation As Long, pSecurityDescriptor As SECURITY_DESCRIPTOR) As Long
Public 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
Public 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
Public Declare Function RegUnLoadKey Lib "advapi32.dll" Alias "RegUnLoadKeyA" (ByVal hkey As Long, ByVal lpSubKey As String) As Long
Public Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Public Type SECURITY_ATTRIBUTES
nLength As Long
bInheritHandle As Long
lpSecurityDescriptor As Long
End Type
Public Type ACL
AclRevision As Byte
AceCount As Integer
Sbz2 As Integer
AclSize As Integer
Sbz1 As Byte
End Type
Public Type SECURITY_DESCRIPTOR
Revision As Byte
Sacl As ACL
Dacl As ACL
Owner As Long
Group As Long
Control As Long
Sbz1 As Byte
End Type
Public Const REG_BINARY = 3
Public Const REG_CREATED_NEW_KEY = &H1
Public Const REG_DWORD_BIG_ENDIAN = 5
Public Const REG_DWORD_LITTLE_ENDIAN = 4
Public Const REG_EXPAND_SZ = 2
Public Const REG_FORCE_RESTORE = (&H8)
Public Const REG_FULL_RESOURCE_DESCRIPTOR = 9
Public Const REG_KEY_INSTDEV = ("Installed")
Public Const REG_LINK = 6
Public Const REG_NONE = 0
Public Const REG_MULTI_SZ = 7
Public Const REG_NOTIFY_CHANGE_ATTRIBUTES = &H2
Public Const REG_NOTIFY_CHANGE_LAST_SET = &H4
Public Const REG_NOTIFY_CHANGE_NAME = &H1
Public Const REG_NOTIFY_CHANGE_SECURITY = &H8
Public Const REG_NO_LAZY_FLUSH = &H4
Public Const REG_OPENED_EXISTING_KEY = &H2
Public Const REG_OPTION_BACKUP_RESTORE = 4
Public Const REG_OPTION_CREATE_LINK = 2
Public Const REG_OPTION_NON_VOLATILE = 0
Public Const REG_OPTION_OPEN_LINK = &H8
Public Const REG_OPTION_RESERVED = 0
Public Const REG_OPTION_VOLATILE = 1
Public Const REG_QWORD = 11
Public Const REG_QWORD_LITTLE_ENDIAN = 11
Public Const REG_REFRESH_HIVE = &H2
Public Const REG_RESOURCE_LIST = 8
Public Const REG_RESOURCE_REQUIREMENTS_LIST = 10
Public Const REG_RESTORE_LOG_KEY = "RegRestoreLogFile"
Public Const MORE_ROWS = -1
Public Const REG_ROW = MORE_ROWS
Public Const REG_SAVE_LOG_KEY = "RegSaveLogFile"
Public Const REG_SZ = 1
Public Const REG_WHOLE_HIVE_VOLATILE = &H1
Public Const REG_LEGAL_OPTION = (REG_OPTION_RESERVED Or REG_OPTION_NON_VOLATILE Or REG_OPTION_VOLATILE Or REG_OPTION_CREATE_LINK Or REG_OPTION_BACKUP_RESTORE)
Public Const REG_LEGAL_CHANGE_FILTER = (REG_NOTIFY_CHANGE_NAME Or REG_NOTIFY_CHANGE_ATTRIBUTES Or REG_NOTIFY_CHANGE_LAST_SET Or REG_NOTIFY_CHANGE_SECURITY)
Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_DYN_DATA = &H80000006
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const HKEY_USERS = &H80000003
Public Const ERROR_SUCCESS = 0&
Public Const ERROR_NONE = 0
Public Const ERROR_BADDB = 1
Public Const ERROR_BADKEY = 2
Public Const ERROR_CANTOPEN = 3
Public Const ERROR_CANTREAD = 4
Public Const ERROR_CANTWRITE = 5
Public Const ERROR_OUTOFMEMORY = 6
Public Const ERROR_INVALID_PARAMETER = 7
Public Const ERROR_ACCESS_DENIED = 8
Public Const ERROR_INVALID_PARAMETERS = 87
Public Const ERROR_NO_MORE_ITEMS = 259&
Public Const BUFFER_SIZE As Long = 255
Public Const READ_CONTROL = &H20000
Public Const SYNCHRONIZE = &H100000
Public Const STANDARD_RIGHTS_ALL = &H1F0000
Public Const STANDARD_RIGHTS_EXECUTE = (READ_CONTROL)
Public Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
Public Const KEY_CREATE_LINK = &H20
Public Const KEY_CREATE_SUB_KEY = &H4
Public Const KEY_ENUMERATE_SUB_KEYS = &H8
Public Const KEY_EVENT = &H1
Public Const KEY_LENGTH_MASK = &HFFFF0000
Public Const KEY_NOTIFY = &H10
Public Const KEY_QUERY_VALUE = &H1
Public Const KEY_SET_VALUE = &H2
Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Public Const KEY_EXECUTE = (KEY_READ)
Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
Public 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))
'*************************************************************************************
Public Function RegDelAllSubVal(ByVal hKeyR As Long, ByVal mKey As String, Optional ByVal Flush As Boolean = False) As Boolean
On Error Resume Next
Dim hkey As Long, Cnt As Long
Dim sName As String, Ret As Long: sName = Space(BUFFER_SIZE)
Dim sData(BUFFER_SIZE) As Byte, RetData As Long
Dim NameSpace() As String: ReDim NameSpace(1)
If RegOpenKeyEx(ByVal hKeyR, ByVal mKey, ByVal 0&, KEY_ALL_ACCESS, hkey) = ERROR_SUCCESS Then
Cnt = 0: Ret = BUFFER_SIZE: RetData = BUFFER_SIZE
If RegEnumValue(hkey, Cnt, sName, Ret, ByVal 0&, ByVal 0&, sData(0), RetData) <> ERROR_NO_MORE_ITEMS Then
Do
ReDim Preserve NameSpace(Cnt)
NameSpace(Cnt) = Left$(sName, Ret)
Cnt = Cnt + 1: Ret = BUFFER_SIZE: RetData = BUFFER_SIZE
Loop While RegEnumValue(hkey, Cnt, sName, Ret, ByVal 0&, ByVal 0&, sData(0), RetData) <> ERROR_NO_MORE_ITEMS
For Cnt = 0 To UBound(NameSpace)
Call RegDeleteValue(ByVal hkey, NameSpace(Cnt))
Next Cnt
End If
Cnt = 0: Ret = BUFFER_SIZE: RetData = BUFFER_SIZE
RegDelAllSubVal = (RegEnumValue(hkey, Cnt, sName, Ret, ByVal 0&, ByVal 0&, sData(0), RetData) = ERROR_NO_MORE_ITEMS)
If Flush Then RegFlushKey hkey
RegCloseKey hkey
Else
RegDelAllSubVal = False
End If
End Function
Public Function RegDelAllSubKey(ByVal hKeyRoot As Long, ByVal mKey1 As String, Optional ByVal Flush As Boolean = False) As Boolean ', mKey2 As String
On Error Resume Next
Dim hkey As Long, Ret As Long, Cnt As Long
Dim i As Long, t As FILETIME
Dim sName As String: sName = Space(BUFFER_SIZE)
Dim NameSpace() As String: ReDim NameSpace(1)
Call RegDelAllSubVal(ByVal hKeyRoot, ByVal mKey1, False)
If RegOpenKeyEx(ByVal hKeyRoot, ByVal mKey1, ByVal 0&, KEY_ALL_ACCESS, hkey) = ERROR_SUCCESS Then
RegDelAllSubKey = True: Cnt = 0: Ret = BUFFER_SIZE
If RegEnumKeyEx(hkey, Cnt, sName, Ret, ByVal 0&, vbNullString, i, t) <> ERROR_NO_MORE_ITEMS Then
Do
ReDim Preserve NameSpace(Cnt)
NameSpace(Cnt) = Left$(sName, Ret)
Cnt = Cnt + 1: Ret = BUFFER_SIZE
Loop While RegEnumKeyEx(hkey, Cnt, sName, Ret, ByVal 0&, vbNullString, i, t) <> ERROR_NO_MORE_ITEMS
For Cnt = LBound(NameSpace) To UBound(NameSpace)
Call RegDelAllSubKey(ByVal hKeyRoot, ByVal mKey1 & "" & NameSpace(Cnt))
Next Cnt
End If
RegDelAllSubKey = (RegDeleteKey(ByVal hKeyRoot, ByVal mKey1) = ERROR_SUCCESS)
If Flush Then RegFlushKey hkey
RegCloseKey hkey
Else
RegDelAllSubKey = False
End If
End Function
Public Function RegDelSingleValue(ByVal mKeyRoot As Long, ByVal sKeyName As String, ByVal sValueName As String) As Long
On Error Resume Next
Dim hkey As Long
If RegOpenKeyEx(mKeyRoot, ByVal sKeyName, ByVal 0&, KEY_ALL_ACCESS, hkey) = ERROR_SUCCESS Then
RegDelSingleValue = RegDeleteValue(hkey, ByVal sValueName)
Call RegCloseKey(hkey)
Else
RegDelSingleValue = ERROR_CANTOPEN
End If
End Function
Public Function RegSetSingleValue(ByVal mKeyRoot As Long, ByVal sKeyName As String, ByVal sValueName As String, ByVal sValueType As Long, ByVal sValue As String) As Long
On Error Resume Next
Dim hkey As Long
If RegCreateKey(mKeyRoot, sKeyName, hkey) = ERROR_SUCCESS Then
RegSetSingleValue = RegSetValueEx(hkey, ByVal sValueName, ByVal 0&, ByVal sValueType, ByVal sValue, ByVal Len(sValue))
Call RegCloseKey(hkey)
Else
RegSetSingleValue = ERROR_CANTOPEN
End If
End Function
Last edited by xiaoyao; Mar 5th, 2022 at 10:50 AM.
-
Mar 5th, 2022, 10:52 AM
#2
Thread Starter
PowerPoster
Re: how to read registry all valus or keys?RegEnumKeyEx,RegEnumValue
for read all key name
I don't know what is wrong, some KEY values are lost in reading, such as "DownloadUI"
Private Sub Command1_Click()
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer
List1.Clear
DaziEnumerateValue HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Internet Explorer"
End Sub
Code:
Public Function DaziEnumerateValue(ByVal hkey As REGRoot, _
ByVal lpsubkey As String)
Dim lngKeyHandle As Long
Dim lngResult As Long
Dim lngCurIdx As Long
Dim strValue As String
Dim lngValueLen As Long
Dim lngData As Long
Dim lngDataLen As Long
Dim strResult As String
lngResult = RegOpenKeyEx(hkey, _
lpsubkey, _
0&, _
KEY_READ, _
lngKeyHandle)
If lngResult <> ERROR_SUCCESS Then
MsgBox "Cannot open key"
Exit Function
End If
lngCurIdx = 0
Do
lngValueLen = 2000
strValue = String(lngValueLen, 0)
lngDataLen = 2000
lngResult = RegEnumValue(lngKeyHandle, _
lngCurIdx, _
ByVal strValue, _
lngValueLen, _
0&, _
REG_DWORD, _
ByVal lngData, _
lngDataLen)
lngCurIdx = lngCurIdx + 1
If lngResult = ERROR_SUCCESS Then
strResult = lngCurIdx & ": " & Left(strValue, lngValueLen)
Form1.List1.AddItem strResult
End If
Loop While lngResult = ERROR_SUCCESS
Call RegCloseKey(lngKeyHandle)
End Function
-
Mar 10th, 2022, 05:43 AM
#3
Thread Starter
PowerPoster
Re: how to read registry all valus or keys?RegEnumKeyEx,RegEnumValue
How to write a registry management tool similar to regedit.exe?
List all children, all names and values
For example, use JSON or dictionary objects to save all values, and then monitor the registry for additions and deletions? Or after installing a software to re-read all the values into a second dictionary object? And then save the newly added, modified, deleted registry information to the third dictionary object?
-
Mar 10th, 2022, 06:11 AM
#4
Re: how to read registry all valus or keys?RegEnumKeyEx,RegEnumValue
Some keys are in the 32 bit section, some other keys in the 64 bit section
See this class by dilettante for reading the registry and specifying the WOW64_64 or WOW64_32 key type:
https://www.vbforums.com/showthread....=1#post3947938
-
Mar 10th, 2022, 06:55 AM
#5
Thread Starter
PowerPoster
Re: how to read registry all valus or keys?RegEnumKeyEx,RegEnumValue
Originally Posted by Arnoutdv
This example is too simple to read only one value. What I want is to list all items, all names, all values
-
Mar 10th, 2022, 10:30 AM
#6
Re: how to read registry all valus or keys?RegEnumKeyEx,RegEnumValue
As always, we cannot really be sure what xiayao really means...
FWIW, here's a simple "deep-enumeration" of Keys (and Values), starting from a given "BaseKey":
Code:
Private Sub Form_Click()
EnumerateOn GetObject("winmgmts://./root/default:StdRegProv"), _
"HKEY_LOCAL_MACHINE", _
"SOFTWARE\Microsoft\Internet Explorer\Capabilities"
End Sub
Sub EnumerateOn(Reg As Object, sHive As String, Key As String, Optional Reader As Object)
Dim Hive, i As Long, Value, ValueKeys, ValueTypes, SubKeys, SubKey
Select Case UCase$(sHive)
Case "HKCR", "HKEY_CLASSES_ROOT": Hive = &H80000000
Case "HKCU", "HKEY_CURRENT_USER": Hive = &H80000001
Case "HKLM", "HKEY_LOCAL_MACHINE": Hive = &H80000002
Case "HKU", "HKEY_USERS": Hive = &H80000003
Case "HKPD", "HKEY_PERFORMANCE_DATA": Hive = &H80000004
Case "HKCC", "HKEY_CURRENT_CONFIG": Hive = &H80000005
Case "HKDD", "HKEY_DYN_DATA": Hive = &H80000006
End Select
If Reader Is Nothing Then Set Reader = CreateObject("WScript.Shell")
Debug.Print Key
Reg.EnumValues Hive, Key, ValueKeys, ValueTypes
If Not IsNull(ValueKeys) Then
For i = 0 To UBound(ValueKeys)
Value = Reader.RegRead(sHive & "\" & Key & "\" & ValueKeys(i))
Debug.Print " "; ValueTypes(i); " "; ValueKeys(i); "="; Value
Next
End If
Reg.EnumKey Hive, Key, SubKeys
If Not IsNull(SubKeys) Then
For Each SubKey In SubKeys
EnumerateOn Reg, sHive, Key & "\" & SubKey, Reader
Next
End If
End Sub
HTH
Olaf
-
Mar 11th, 2022, 10:05 AM
#7
Thread Starter
PowerPoster
Re: how to read registry all valus or keys?RegEnumKeyEx,RegEnumValue
HOW TO list all keys by RegEnumKey?
it's vb.net
https://blog.csdn.net/mohongmao/article/details/3431217
Code:
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA"(ByVal hKey As Integer, ByVal lpSubKey As String, ByRef phkResult As Integer) As Integer
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Integer) As Integer
Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA"(ByVal hKey As Integer, ByVal dwIndex As Integer, ByVal lpName As String, ByVal cbName As Integer) As Integer
Const HKEY_CURRENT_USER As Integer = &H80000001
Const ERROR_NO_MORE_ITEMS As Short = 259
Const ERROR_SUCCESS As Short = 0
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Dim hKey As Integer
Dim i As Integer
Dim astr As VB6.FixedLengthString = New VB6.FixedLengthString(256)
If RegOpenKey(HKEY_CURRENT_USER, "RemoteAccess/Profile", hKey) = ERROR_SUCCESS Then
While RegEnumKey(hKey, i, astr.Value, 256) = ERROR_SUCCESS
List1.Items.Add(astr.Value)
i = i + 1
End While
RegCloseKey(hKey)
End If
End Sub
-
Mar 11th, 2022, 10:29 AM
#8
Thread Starter
PowerPoster
Re: how to read registry all valus or keys?RegEnumKeyEx,RegEnumValue
get all keys it's successfull about:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer
but this have no result,why? i add some keys in AboutURLs
Arr = ListALLKey(HKEY_LOCAL_MACHINE, "Software\Microsoft\Internet Explorer\AboutURLs")
Code:
Dim Arr() As String
Arr = ListALLKey(HKEY_LOCAL_MACHINE, "Software\Microsoft\Internet Explorer")
MsgBox Join(Arr, vbCrLf)
Code:
Function ListALLKey(ByVal hKeyNuEm As REGRoot, _
ByVal lpSubKey As String) As String()
Dim DataDC As Dictionary
Set DataDC = New Dictionary
Dim strvalue As String 'Variable to hold current enumerated key
Dim lDataLen As Long 'Length of data
Dim lresult As Long 'Result of RegEnumKey
Dim lValueLen As Long
Dim lCurIdx As Long 'Current Index which gets incremented with each pass through the loop
Dim lRetVal As Long 'Result of RegOpenKeyEx
Dim hKeyResult As Long
lRetVal = RegOpenKeyEx(hKeyNuEm, lpSubKey, 0, KEY_ALL_ACCESS, hKeyResult) 'Open key with Full Access Rights
If lRetVal = ERROR_SUCCESS Then
lCurIdx = 0 'Initialise loop counter
lDataLen = 255 'data Length
lValueLen = 255
Dim ID As Long
Dim Nid As Long, S2 As String
Do
strvalue = String(lValueLen, 0) 'get current key's value
lresult = RegEnumKey(hKeyResult, lCurIdx, strvalue, lDataLen) 'Enumerate keys
If lresult = ERROR_SUCCESS Then 'if successful, add current enumerated key to the txtEnumKeys textbox
S2 = strvalue
Nid = InStr(strvalue, Chr(0))
If Nid > 0 Then S2 = Left(strvalue, Nid - 1)
DataDC.Add ID, S2
ID = ID + 1
End If
lCurIdx = lCurIdx + 1 'Increment counter for next enumeration
Loop While lresult = ERROR_SUCCESS 'continue while successful
RegCloseKey hKeyResult 'Close key
Else 'If lRetVal is unsuccessful
MsgBox "Cannot Open Key"
End If
Dim Arr() As String, I As Long
If ID > 0 Then
ReDim Arr(ID - 1)
For I = 0 To ID - 1
Arr(I) = DataDC.Items(I)
Next
Else
ReDim Arr(-1 To -1)
End If
ListALLKey = Arr
End Function
[VB6] How Do I Work With The Registry?
https://forums.codeguru.com/showthre...h-The-Registry
Last edited by xiaoyao; Mar 11th, 2022 at 10:32 AM.
-
Mar 14th, 2022, 08:06 PM
#9
Thread Starter
PowerPoster
Re: how to read registry all valus or keys?RegEnumKeyEx,RegEnumValue
This can only delete the lowest level item, how to delete all sub-items and the sub-items below
Code:
Public Function DelRegKey(ByVal KeyRoot As REGRoot, ByVal KeyName As String, ByVal SubKeyName As String) As Long
Dim lKeyId As Long
Dim lresult As Long
DelRegKey = 0 ' Assume success
'Detect the parameters set
If Len(KeyName) = 0 And Len(SubKeyName) = 0 Then
' If the key value is not set, the corresponding error code will be returned
DelRegKey = ERROR_BADKEY
Exit Function
End If
' Open the keyword and try to create it, if it already exists, return the ID value
lresult = RegCreateKey(KeyRoot, KeyName, lKeyId)
If lresult = 0 Then
'delete keyword
DelRegKey = RegDeleteKey(lKeyId, ByVal SubKeyName)
End If
End Function
-
Mar 15th, 2022, 04:04 AM
#10
Re: how to read registry all valus or keys?RegEnumKeyEx,RegEnumValue
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Mar 15th, 2022, 05:01 AM
#11
Re: how to read registry all valus or keys?RegEnumKeyEx,RegEnumValue
Originally Posted by xiaoyao
What I want is to list all items, all names, all values
I have a bas module with the function EnumRegistrykeys that maybe can do what you need.
Code:
Option Explicit
Public Enum BSRegistryTopPredefinedKeys
HKEY_CLASSES_ROOT = &H80000000
HKEY_CURRENT_USER = &H80000001
HKEY_LOCAL_MACHINE = &H80000002
HKEY_USERS = &H80000003
End Enum
Private Const ERROR_SUCCESS = 0&
'Private Const ERROR_NO_MORE_ITEMS = 259&
Private Const REG_SZ = 1
Private Const REG_DWORD As Long = 4
Private Const ERROR_NONE = 0
Private Const KEY_ALL_ACCESS = &H3F
Private Const KEY_READ = &H20019 ' ((READ_CONTROL Or KEY_QUERY_VALUE Or
' KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not
' SYNCHRONIZE))
Private Declare Function OSRegCloseKey Lib "advapi32" Alias "RegCloseKey" (ByVal hKey As Long) As Long
Private Declare Function OSRegCreateKey Lib "advapi32" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpszSubKey As String, phkResult 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, lpData As Any, ByVal cbData As Long) As Long
Private Declare Function RegQueryValueExString 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 RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Long, lpcbData As Long) As Long
Private Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Long, lpcbData As Long) As Long
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 RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
Public Sub SaveRegString(hKey As BSRegistryTopPredefinedKeys, strPath As String, strValue As String, strData As String)
'EXAMPLE:
'
'Call SaveRegString(HKEY_CURRENT_USER, "Sof
' tware\VBW\Registry", "String", text1.tex
' t)
'
Dim keyhand As Long
Dim r As Long
r = OSRegCreateKey(hKey, strPath, keyhand)
If Right$(strData, 1) <> Chr$(0) Then
strData = strData & Chr$(0)
End If
r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strData, Len(strData))
r = OSRegCloseKey(keyhand)
End Sub
Private Function QueryRegValueEx(ByVal lhKey As BSRegistryTopPredefinedKeys, ByVal szValueName As String, vValue As Variant) As Long
Dim cch As Long
Dim lrc As Long
Dim lType As Long
Dim lValue As Long
Dim sValue As String
On Error GoTo QueryValueExError
' Determine the size and type of data to be read
lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
If lrc <> ERROR_NONE Then Error 5
Select Case lType
' For strings
Case REG_SZ, 2
sValue = String$(cch, 0)
lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, sValue, cch)
If lrc = ERROR_NONE Then
vValue = Left$(sValue, cch - 1)
Else
vValue = Empty
End If
' For DWORDS
Case REG_DWORD:
lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, lValue, cch)
If lrc = ERROR_NONE Then vValue = lValue
Case Else
'all other data types not supported
lrc = -1
End Select
QueryValueExExit:
QueryRegValueEx = lrc
Exit Function
QueryValueExError:
Resume QueryValueExExit
End Function
Public Function QueryRegValue(lPredefinedKey As BSRegistryTopPredefinedKeys, sKeyName As String, sValueName As String) As Variant
' Description:
' This Function will return the data field of a value
'
' Syntax:
' Variable = QueryRegValue(Location, KeyName, ValueName)
'
' Location must equal HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_lOCAL_MACHINE
' , HKEY_USERS
'
' KeyName is the key that the value is under (example: "Software\Microsoft\Windows\CurrentVersion\Explorer")
'
' ValueName is the name of the value you want to access (example: "link")
Dim lRetVal As Long 'result of the API functions
Dim hKey As Long 'handle of opened key
Dim vValue As Variant 'setting of queried value
lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
lRetVal = QueryRegValueEx(hKey, sValueName, vValue)
'MsgBox vValue
QueryRegValue = vValue
RegCloseKey (hKey)
End Function
Public Sub DeleteRegValue(lPredefinedKey As BSRegistryTopPredefinedKeys, sKeyName As String, sValueName As String)
Dim hKey As Long, iRet As Long
'Create a new key
iRet = OSRegCreateKey(lPredefinedKey, sKeyName, hKey)
If iRet <> ERROR_SUCCESS Then Exit Sub
'Delete the key's value
iRet = RegDeleteValue(hKey, sValueName)
'close the key
RegCloseKey hKey
End Sub
Public Function EnumRegistryKeys(ByVal hKey As BSRegistryTopPredefinedKeys, ByVal KeyName As String) As Collection
Dim handle As Long
Dim length As Long
Dim index As Long
Dim subkeyName As String
' initialize the result collection
Set EnumRegistryKeys = New Collection
' Open the key, exit if not found
If Len(KeyName) Then
If RegOpenKeyEx(hKey, KeyName, 0, KEY_READ, handle) Then Exit Function
' in all case the subsequent functions use hKey
hKey = handle
End If
Do
' this is the max length for a key name
length = 260
subkeyName = Space$(length)
' get the N-th key, exit the loop if not found
If RegEnumKey(hKey, index, subkeyName, length) Then Exit Do
' add to the result collection
subkeyName = Left$(subkeyName, InStr(subkeyName, vbNullChar) - 1)
EnumRegistryKeys.Add subkeyName, subkeyName
' prepare to query for next key
index = index + 1
Loop
' Close the key, if it was actually opened
If handle Then RegCloseKey handle
End Function
(Not tested now) I should return a collection with the manes under a key, then you can get each value with the other functions.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|