Perfect the Registry Jumper

  • Thread starter Thread starter gaxjyxq
  • Start date Start date
G

gaxjyxq

Guest
The code below is a Registry Jumper, sometimes it locates not right. I feel the it need to be perfected for the "Open Path" function to open TreeView, it located the path by each char of key path, can the TreeView be located by each key name? RegEdit UI adds a text box on the top of window, what's the class name? the TreeView is "SysTreeView32", the ListView is "SysListView32", is the textbox "SysTextBox32"?

Public Class RegistryJump

'Window API
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Integer) As Integer

Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
Private Const SW_HIDE = 0
Private Const SW_SHOWNORMAL = 1
Private Const SW_SHOWMINIMIZED = 2
Private Const SW_SHOWMAXIMIZED = 3
Private Const SW_MAXIMIZE = 3
Private Const SW_SHOWNOACTIVATE = 4
Private Const SW_SHOW = 5
Private Const SW_MINIMIZE = 6
Private Const SW_SHOWMINNOACTIVE = 7
Private Const SW_SHOWNA = 8
Private Const SW_RESTORE = 9

Private Declare Function SetFocus Lib "user32.dll" (ByVal hwnd As Integer) As Integer

'Message API
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByRef lParam As Integer) As Integer
Private Const WM_KEYDOWN As Integer = &H100S
Private Const WM_CHAR As Integer = &H102S
Private Const VK_LEFT As Integer = &H25S
Private Const VK_RIGHT As Integer = &H27S
Private Const VK_HOME As Integer = &H24S

'Process API
Private Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Integer, ByRef lpdwProcessId As Integer) As Integer
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As Integer
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Integer) As Integer
Private Declare Function WaitForInputIdle Lib "user32.dll" (ByVal hProcess As Integer, ByVal dwMilliseconds As Integer) As Integer
Private Const SYNCHRONIZE As Integer = &H100000
Private Const INFINITE As Integer = &HFFFFFFFF
Private Const STANDARD_RIGHTS_REQUIRED As Integer = &HF0000
Private Const PROCESS_ALL_ACCESS As Integer = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFFS)

'==========================================================
Public Shared Sub RegEditJump(ByVal RegKey As String, Optional ByVal strValueName As String = "")
Dim dic As Dictionary(Of RegistryKey, String) = HKEYStringToMainKeyAndSubKey(RegKey)
Dim mainKey As RegistryKey = dic.Keys(0)
Dim subKey As String = dic.Values(0)

Try
If mainKey.OpenSubKey(subKey) Is Nothing Then
Exit Sub
End If
Catch ex As Exception
Exit Sub
End Try

Dim hndRegEdit As Integer
Dim hndTreeView As Integer
Dim hndListView As Integer
Dim hndProcess As Integer
Dim j As Int32
Dim strTmp As String
Dim lngVK As Integer

hndRegEdit = FindWindow("RegEdit_RegEdit" & vbNullChar, vbNullString)

If hndRegEdit = 0 Then
Dim p As New Process
p.StartInfo.FileName = Directory.GetParent(Environment.SystemDirectory).ToString & "\regedit.exe"
p.Start()
p.WaitForInputIdle(5000)
End If


hndRegEdit = FindWindow("RegEdit_RegEdit" & vbNullChar, vbNullString)
If hndRegEdit = 0 Then
Exit Sub
End If

ShowWindow(hndRegEdit, SW_SHOWMAXIMIZED)
SetForegroundWindow(hndRegEdit)

hndTreeView = FindWindowEx(hndRegEdit, 0, "SysTreeView32" & vbNullChar, vbNullString) '获取TreeView句柄

SetForegroundWindow(hndTreeView)
SetFocus(hndTreeView)


GetWindowThreadProcessId(hndTreeView, hndProcess)
OpenProcess(PROCESS_ALL_ACCESS, 0, hndProcess)


'------------Open Path---------------------------------
For j = 1 To Len(RegKey)
strTmp = Mid(RegKey, j, 1) 'Get a char
'If '\' then send 'Right'
If strTmp = "\" Then
SendMessage(hndTreeView, WM_KEYDOWN, VK_RIGHT, 0)
WaitForInputIdle(hndProcess, INFINITE)
Else 'Send VirtualKey Code
lngVK = Asc(UCase(strTmp))
SendMessage(hndTreeView, WM_CHAR, lngVK, 0)
End If
Threading.Thread.Sleep(10)
Next
CloseHandle(hndProcess)
'---------------------------------------------------------

If strValueName <> "" Then
Try

Dim rk As RegistryKey = mainKey.OpenSubKey(subKey)
If rk IsNot Nothing Then
If rk.GetValue(strValueName, Nothing) IsNot Nothing Then

hndListView = FindWindowEx(hndRegEdit, 0, "SysListView32" & vbNullChar, vbNullString)


SetForegroundWindow(hndListView)
SetFocus(hndListView)


Threading.Thread.Sleep(1500)


SendMessage(hndListView, WM_KEYDOWN, VK_HOME, 0)


For j = 1 To strValueName.Length
strTmp = Mid(strValueName, j, 1)
lngVK = Asc(UCase(strTmp))
SendMessage(hndListView, WM_CHAR, lngVK, 0)
Next
End If
End If
Catch ex As Exception
End Try
End If

SetForegroundWindow(hndRegEdit)
SetFocus(hndRegEdit)
End Sub


Private Shared Function HKEYStringToMainKeyAndSubKey(RegKey As String) As Dictionary(Of RegistryKey, String)
Dim mainKey As RegistryKey = Nothing
Dim subKey As String = ""
RegKey = RegKey.ToUpper

Dim dicValue As New Dictionary(Of RegistryKey, String)
Try
If RegKey.StartsWith(Registry.LocalMachine.Name) Then
mainKey = Registry.LocalMachine
subKey = RegKey.Replace(Registry.LocalMachine.Name & "\", "")
ElseIf RegKey.StartsWith("HKLM") Then
mainKey = Registry.LocalMachine
subKey = RegKey.Replace("HKLM\", "")
ElseIf RegKey.StartsWith(Registry.CurrentUser.Name) Then
mainKey = Registry.CurrentUser
subKey = RegKey.Replace(Registry.CurrentUser.Name & "\", "")
ElseIf RegKey.StartsWith("HKCU") Then
mainKey = Registry.CurrentUser
subKey = RegKey.Replace("HKCU\", "")
ElseIf RegKey.StartsWith(Registry.ClassesRoot.Name) Then
mainKey = Registry.ClassesRoot
subKey = RegKey.Replace(Registry.ClassesRoot.Name & "\", "")
ElseIf RegKey.StartsWith("HKCR") Then
mainKey = Registry.ClassesRoot
subKey = RegKey.Replace("HKCR\", "")
ElseIf RegKey.StartsWith(Registry.Users.Name) Then
mainKey = Registry.Users
subKey = RegKey.Replace(Registry.Users.Name & "\", "")
ElseIf RegKey.StartsWith("HKU") Then
mainKey = Registry.Users
subKey = RegKey.Replace("HKU\", "")
ElseIf RegKey.StartsWith(Registry.CurrentConfig.Name) Then
mainKey = Registry.CurrentConfig
subKey = RegKey.Replace(Registry.CurrentConfig.Name & "\", "")
End If
dicValue.Add(mainKey, subKey)
Catch ex As Exception
End Try
Return dicValue
End Function
End Class

Continue reading...
 
Back
Top