From 0859e900aa39219324bc3ece5ab6de31f3c765ee Mon Sep 17 00:00:00 2001 From: Guillaume Lachance Date: Mon, 13 Jan 2025 10:25:28 -0500 Subject: [PATCH] Jump to hive implementation (#85) --- RegExp/MainFrame.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++ RegExp/MainFrame.h | 4 +++ RegExp/resource.h | 5 +++- 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/RegExp/MainFrame.cpp b/RegExp/MainFrame.cpp index 3fec382..fc68e89 100644 --- a/RegExp/MainFrame.cpp +++ b/RegExp/MainFrame.cpp @@ -871,6 +871,40 @@ LRESULT CMainFrame::OnTreeContextMenu(int, LPNMHDR hdr, BOOL&) { // hMenu.InsertMenu(0, MF_BYPOSITION, ID_KEY_JUMPTOHIVEFILE, L"Jump &Hive File..."); } + + hMenu.AppendMenu(MF_SEPARATOR); + auto path = GetCurrentKeyPath(); + auto currentHive = path.Mid(0, path.Find(L"\\", 0)); + if (currentHive == L"HKEY_CURRENT_USER") { + auto destination = L"HKEY_LOCAL_MACHINE" + path.Mid(currentHive.GetLength()); + auto key = Registry::OpenKey(destination, KEY_QUERY_VALUE); + if (key) + hMenu.AppendMenu(MF_STRING, ID_GOTOHIVE_LOCALMACHINE, L"Goto HKEY_LOCAL_MACHINE"); + if (path.MakeUpper().Find(L"HKEY_CURRENT_USER\\SOFTWARE\\CLASSES") == 0) + hMenu.AppendMenu(MF_STRING, ID_GOTOHIVE_CLASSESROOT, L"Goto HKEY_CLASSES_ROOT"); + } + else if (currentHive == L"HKEY_LOCAL_MACHINE") { + auto destination = L"HKEY_CURRENT_USER" + path.Mid(currentHive.GetLength()); + auto key = Registry::OpenKey(destination, KEY_QUERY_VALUE); + if (key) + hMenu.AppendMenu(MF_STRING, ID_GOTOHIVE_CURRENTUSER, L"Goto HKEY_CURRENT_USER"); + if (path.MakeUpper().Find(L"HKEY_LOCAL_MACHINE\\SOFTWARE\\CLASSES") == 0) + hMenu.AppendMenu(MF_STRING, ID_GOTOHIVE_CLASSESROOT, L"Goto HKEY_CLASSES_ROOT"); + } + else if (currentHive == L"HKEY_CLASSES_ROOT") { + auto destination = L"HKEY_LOCAL_MACHINE\\SOFTWARE\\CLASSES" + path.Mid(currentHive.GetLength()); + auto key = Registry::OpenKey(destination, KEY_QUERY_VALUE); + if (key) + hMenu.AppendMenu(MF_STRING, ID_GOTOHIVE_LOCALMACHINE, L"Goto HKEY_LOCAL_MACHINE"); + destination = L"HKEY_CURRENT_USER\\SOFTWARE\\CLASSES" + path.Mid(currentHive.GetLength()); + key = Registry::OpenKey(destination, KEY_QUERY_VALUE); + if (key) + hMenu.AppendMenu(MF_STRING, ID_GOTOHIVE_CURRENTUSER, L"Goto HKEY_CURRENT_USER"); + } + auto lastMenuItem = hMenu.GetMenuItemID(hMenu.GetMenuItemCount()-1); + if (lastMenuItem == 0) + hMenu.DeleteMenu(hMenu.GetMenuItemCount()-1, MF_BYPOSITION); + ShowContextMenu(hMenu, 0, pt2.x, pt2.y); } return 0; @@ -2723,3 +2757,34 @@ void CMainFrame::UpdateList(bool newLocation) { m_List.RedrawItems(m_List.GetTopIndex(), m_List.GetTopIndex() + m_List.GetCountPerPage()); } +LRESULT CMainFrame::OnGotoHive(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) { + auto path = GetCurrentKeyPath(); + CString currentHive = path.Mid(0, path.Find(L"\\", 0)); + CString destinationHive = L""; + + if (wID == ID_GOTOHIVE_CURRENTUSER) { + destinationHive = L"HKEY_CURRENT_USER"; + } + else if (wID == ID_GOTOHIVE_LOCALMACHINE) { + destinationHive = L"HKEY_LOCAL_MACHINE"; + } + else { + ATLASSERT(wID == ID_GOTOHIVE_CLASSESROOT); + destinationHive = L"HKEY_CLASSES_ROOT"; + } + + if (currentHive == L"HKEY_CLASSES_ROOT" && (destinationHive == L"HKEY_CURRENT_USER" || destinationHive == L"HKEY_LOCAL_MACHINE")) { + destinationHive += L"\\SOFTWARE\\CLASSES"; + } + + if (destinationHive == L"HKEY_CLASSES_ROOT" && (currentHive == L"HKEY_CURRENT_USER" || currentHive == L"HKEY_LOCAL_MACHINE")) { + currentHive += L"\\SOFTWARE\\CLASSES"; + } + + path = destinationHive + path.Mid(currentHive.GetLength()); + if (currentHive != destinationHive) { + GotoKey(path, false); + } + + return 0; +} diff --git a/RegExp/MainFrame.h b/RegExp/MainFrame.h index efeec28..ea78fa1 100644 --- a/RegExp/MainFrame.h +++ b/RegExp/MainFrame.h @@ -164,6 +164,9 @@ class CMainFrame : COMMAND_ID_HANDLER(ID_KEY_ADDBOOKMARK, OnAddBookmark) COMMAND_ID_HANDLER(ID_DELETE_BOOKMARK, OnDeleteBookmark) COMMAND_ID_HANDLER(ID_LOCATIONS_MANAGE, OnManageLocations) + COMMAND_ID_HANDLER(ID_GOTOHIVE_CURRENTUSER, OnGotoHive) + COMMAND_ID_HANDLER(ID_GOTOHIVE_LOCALMACHINE, OnGotoHive) + COMMAND_ID_HANDLER(ID_GOTOHIVE_CLASSESROOT, OnGotoHive) MESSAGE_HANDLER(WM_SHOWWINDOW, OnShowWindow) MESSAGE_HANDLER(WM_CREATE, OnCreate) MESSAGE_HANDLER(WM_DESTROY, OnDestroy) @@ -295,6 +298,7 @@ class CMainFrame : LRESULT OnDeleteBookmark(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/); LRESULT OnManageLocations(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/); LRESULT OnViewSelectAll(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/); + LRESULT OnGotoHive(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/); void ConnectRemoteRegistry(CString hostname); void InitCommandBar(); diff --git a/RegExp/resource.h b/RegExp/resource.h index d58e44d..fefd38b 100644 --- a/RegExp/resource.h +++ b/RegExp/resource.h @@ -196,6 +196,9 @@ #define ID_KEY_ADDBOOKMARK 32882 #define ID_DELETE_BOOKMARK 32883 #define ID_VIEW_SELECTALL 32887 +#define ID_GOTOHIVE_CURRENTUSER 32888 +#define ID_GOTOHIVE_LOCALMACHINE 32889 +#define ID_GOTOHIVE_CLASSESROOT 32890 // Next default values for new objects // @@ -203,7 +206,7 @@ #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NO_MFC 1 #define _APS_NEXT_RESOURCE_VALUE 182 -#define _APS_NEXT_COMMAND_VALUE 32890 +#define _APS_NEXT_COMMAND_VALUE 32891 #define _APS_NEXT_CONTROL_VALUE 1060 #define _APS_NEXT_SYMED_VALUE 115 #endif