Option Explicit
Private Type FILETIME
intLow As Long
intHigh As Long
End Type
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 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
Private Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const ERROR_SUCCESS = 0&
Const SYNCHRONIZE = &H100000
Const STANDARD_RIGHTS_READ = &H20000
Const KEY_QUERY_VALUE = &H1
Const KEY_ENUMERATE_SUB_KEYS = &H8
Const KEY_NOTIFY = &H10
Const KEY_READ = ((STANDARD_RIGHTS_READ Or _
KEY_QUERY_VALUE Or _
KEY_ENUMERATE_SUB_KEYS Or _
KEY_NOTIFY) And _
(Not SYNCHRONIZE))
Dim strBranch As Long
Private Sub Combo1_Click()
' Set the branch to search depending on
' what is selected in the ComboBox
Select Case Combo1.ListIndex
Case 0
strBranch = HKEY_CLASSES_ROOT
Case 1
strBranch = HKEY_CURRENT_USER
Case 2
strBranch = HKEY_LOCAL_MACHINE
Case Else
strBranch = HKEY_USERS
End Select
End Sub
Private Sub startscanregbutton_Click()
Dim i As Integer
Dim lngKeyHandle As Long
Dim lngResult As Long
Dim lngCurIdx As Long
Dim strValue As String
Dim lngValueLen As Long
Dim strClass As String
Dim lngClassLen As Long
Dim strResult As String
Dim lngTime As FILETIME
Dim strSearch As String
Dim intSearchLen As Integer
Dim blnMatch As Boolean
i = 0
' Clear the current results
listreg.Clear
' Assign the new string to search for
strSearch = Text1.Text
intSearchLen = Len(strSearch)
' Open the Root Branch to search
lngResult = RegOpenKeyEx(strBranch, _
"", _
0&, _
KEY_READ, _
lngKeyHandle)
If lngResult <> ERROR_SUCCESS Then
MsgBox "Cannot open key.", , "Search Registry Keys"
Else
' If the Root branch can be opened, disable
' the buttons and begin the search
startscanregbutton.Enabled = False
' Command2.Enabled = False
listreg.Enabled = False
Reg.MousePointer = 11
lngCurIdx = 0
Do
lngValueLen = 2000
strValue = String(lngValueLen, 0)
lngClassLen = 2000
strClass = String(lngClassLen, 0)
' Enumerate all the sub keys
lngResult = RegEnumKeyEx(lngKeyHandle, _
lngCurIdx, _
ByVal strValue, _
lngValueLen, _
0&, _
ByVal strClass, _
lngClassLen, _
lngTime)
' Increment the index of found keys
lngCurIdx = lngCurIdx + 1
If lngResult = ERROR_SUCCESS Then
' Trim the current key to its actual length
strResult = Left(strValue, lngValueLen)
' Eliminate case if the search is insensitive
blnMatch = False
strValue = strResult
If Check1.Value = 0 Then
strResult = LCase(strResult)
strSearch = LCase(strSearch)
End If
' Compare strings based upon search type
Select Case Combo2.ListIndex
Case 0
' Check if any portion of the search string is found.
If InStr(strResult, strSearch) Then blnMatch = True
Case 1
' Check if an exact match is found.
If strResult = strSearch Then blnMatch = True
Case 2
' Check if the search string matches the
' left portion of the key string.
If Left(strResult, intSearchLen) = strSearch Then blnMatch = True
Case Else
' Check if the search string matches the
' right portion of the key string.
If Right(strResult, intSearchLen) = strSearch Then blnMatch = True
End Select
' Populate the list with keys that match
' the search criteria
If blnMatch Then
i = i + 1
listreg.AddItem strValue
End If
End If
' Keep looking for more keys
Loop While lngResult = ERROR_SUCCESS
' Close the Root Branch
lngResult = RegCloseKey(lngKeyHandle)
' Enable the buttons
Reg.MousePointer = 0
listreg.Enabled = True
startscanregbutton.Enabled = True
' Command2.Enabled = True
' Display the total matches
MsgBox "Total matches:" & Str(i), , "Search Registry Keys"
End If
End Sub
Private Sub Form_Load()
SearchReglbl.Caption = "Search Mode:"
Currentreglbl.Caption = "Find What:"
Combo1.AddItem "HKEY_CLASSES_ROOT"
Combo1.AddItem "HKEY_CURRENT_USER"
Combo1.AddItem "HKEY_LOCAL_MACHINE"
Combo1.AddItem "HKEY_USERS"
Combo1.ListIndex = 0
Combo1.TabIndex = 0
Combo2.AddItem "Portion"
Combo2.AddItem "All"
Combo2.AddItem "Left"
Combo2.AddItem "Right"
Combo2.ListIndex = 0
Combo2.TabIndex = 1
Text1.Text = ""
Text1.TabIndex = 2
Check1.Caption = "Match Case"
Check1.Move 4680, 1320, 1275, 255
Check1.TabIndex = 5
listreg.TabIndex = 6
End Sub
Private Sub Image2_Click()
Unload Me
End Sub
Private Sub Image3_Click()
End Sub