Skip to content

Commit

Permalink
Process Memory dialog added: "View->Process Memory..."
Browse files Browse the repository at this point in the history
  • Loading branch information
jovibor committed May 19, 2024
1 parent 9c16c29 commit b1154c0
Show file tree
Hide file tree
Showing 15 changed files with 321 additions and 61 deletions.
15 changes: 12 additions & 3 deletions Hexer/CChildFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,24 @@ void CChildFrame::OnFrameActivate()
GetMainFrame()->OnChildFrameActivate();
}

void CChildFrame::OnFrameDisctivate()
void CChildFrame::OnFrameDisactivate()
{
for (const auto eWnd : Ut::g_arrHexDlg) { //Hide all active dialogs.
//HexCtrl dialogs that is not in panes. They must be hidden/restored during tab switching.
static constexpr HEXCTRL::EHexWnd arrHexDlg[] { HEXCTRL::EHexWnd::DLG_CODEPAGE, HEXCTRL::EHexWnd::DLG_GOTO,
HEXCTRL::EHexWnd::DLG_MODIFY, HEXCTRL::EHexWnd::DLG_SEARCH };

for (const auto eWnd : arrHexDlg) { //Hide all active HexCtrl dialogs.
if (const auto hWnd = GetHexCtrl()->GetWndHandle(eWnd, false); ::IsWindowVisible(hWnd)) {
::ShowWindow(hWnd, SW_HIDE);
m_vecWndHidden.emplace_back(hWnd);
}
}

if (const auto hWnd = GetHexerView()->GetDlgProcMemory(); ::IsWindowVisible(hWnd)) { //Hide DlgProcMemory.
::ShowWindow(hWnd, SW_HIDE);
m_vecWndHidden.emplace_back(hWnd);
}

GetMainFrame()->OnChildFrameDisactivate();
}

Expand Down Expand Up @@ -123,5 +132,5 @@ void CChildFrame::OnMDIActivate(BOOL bActivate, CWnd* pActivateWnd, CWnd* pDeact
return;
}

bActivate ? OnFrameActivate() : OnFrameDisctivate();
bActivate ? OnFrameActivate() : OnFrameDisactivate();
}
2 changes: 1 addition & 1 deletion Hexer/CChildFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CChildFrame final : public CMDIChildWndEx {
BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)override;
afx_msg void OnDestroy();
void OnFrameActivate();
void OnFrameDisctivate();
void OnFrameDisactivate();
afx_msg void OnMDIActivate(BOOL bActivate, CWnd* pActivateWnd, CWnd* pDeactivateWnd);
DECLARE_DYNCREATE(CChildFrame);
DECLARE_MESSAGE_MAP();
Expand Down
5 changes: 5 additions & 0 deletions Hexer/CHexerDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ auto CHexerDoc::GetProcID()const->DWORD
return m_stDataLoader.GetProcID();
}

auto CHexerDoc::GetVecProcMemory()const->const std::vector<MEMORY_BASIC_INFORMATION>&
{
return m_stDataLoader.GetVecProcMemory();
}

auto CHexerDoc::GetVirtualInterface()->HEXCTRL::IHexVirtData*
{
return m_stDataLoader.GetVirtualInterface();
Expand Down
1 change: 1 addition & 0 deletions Hexer/CHexerDoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class CHexerDoc final : public CDocument {
[[nodiscard]] auto GetMemPageSize()const->DWORD;
[[nodiscard]] auto GetOpenMode()const->Ut::EOpenMode;
[[nodiscard]] auto GetProcID()const->DWORD;
[[nodiscard]] auto GetVecProcMemory()const->const std::vector<MEMORY_BASIC_INFORMATION>&;
[[nodiscard]] auto GetVirtualInterface() -> HEXCTRL::IHexVirtData*;
[[nodiscard]] bool IsFileMutable()const;
[[nodiscard]] bool IsProcess()const;
Expand Down
34 changes: 29 additions & 5 deletions Hexer/CHexerView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ BEGIN_MESSAGE_MAP(CHexerView, CView)
ON_COMMAND(IDM_EDIT_REDO, &CHexerView::OnEditRedo)
ON_COMMAND(IDM_EDIT_COPYHEX, &CHexerView::OnEditCopyHex)
ON_COMMAND(IDM_EDIT_PASTEHEX, &CHexerView::OnEditPasteHex)
ON_COMMAND(IDM_VIEW_PROCMEMORY, &CHexerView::OnViewProcMemory)
ON_NOTIFY(HEXCTRL::HEXCTRL_MSG_DLGBKMMGR, IDC_HEXCTRL_MAIN, &CHexerView::OnHexCtrlDLG)
ON_NOTIFY(HEXCTRL::HEXCTRL_MSG_DLGDATAINTERP, IDC_HEXCTRL_MAIN, &CHexerView::OnHexCtrlDLG)
ON_NOTIFY(HEXCTRL::HEXCTRL_MSG_DLGTEMPLMGR, IDC_HEXCTRL_MAIN, &CHexerView::OnHexCtrlDLG)
ON_NOTIFY(HEXCTRL::HEXCTRL_MSG_SETFONT, IDC_HEXCTRL_MAIN, &CHexerView::OnHexCtrlSetFont)
ON_UPDATE_COMMAND_UI(IDM_EDIT_EDITMODE, &CHexerView::OnUpdateEditEditMode)
ON_UPDATE_COMMAND_UI(IDM_VIEW_PROCMEMORY, &CHexerView::OnUpdateProcMemory)
ON_WM_SIZE()
END_MESSAGE_MAP()

Expand All @@ -42,6 +44,11 @@ auto CHexerView::GetDataInfo()const->Ut::DATAINFO
.dwPageSize = GetHexCtrl()->GetPageSize(), .eMode { pDoc->GetOpenMode() }, .fMutable = GetHexCtrl()->IsMutable() };
}

auto CHexerView::GetDlgProcMemory()const->HWND
{
return m_dlgProcMem;
}

auto CHexerView::GetHexCtrl()const->HEXCTRL::IHexCtrl*
{
return &*m_pHexCtrl;
Expand Down Expand Up @@ -142,22 +149,22 @@ void CHexerView::OnFilePrint()

void CHexerView::OnHexCtrlDLG(NMHDR* pNMHDR, LRESULT* /*pResult*/)
{
UINT uPaineID;
UINT uPaneID;
switch (pNMHDR->code) {
case HEXCTRL::HEXCTRL_MSG_DLGBKMMGR:
uPaineID = IDC_PANE_BKMMGR;
uPaneID = IDC_PANE_BKMMGR;
break;
case HEXCTRL::HEXCTRL_MSG_DLGDATAINTERP:
uPaineID = IDC_PANE_DATAINTERP;
uPaneID = IDC_PANE_DATAINTERP;
break;
case HEXCTRL::HEXCTRL_MSG_DLGTEMPLMGR:
uPaineID = IDC_PANE_TEMPLMGR;
uPaneID = IDC_PANE_TEMPLMGR;
break;
default:
return;
}

GetMainFrame()->ShowPane(uPaineID, true, true);
GetMainFrame()->ShowPane(uPaneID, true, true);
}

void CHexerView::OnHexCtrlSetFont(NMHDR* /*pNMHDR*/, LRESULT* /*pResult*/)
Expand Down Expand Up @@ -235,6 +242,23 @@ void CHexerView::OnUpdateEditEditMode(CCmdUI* pCmdUI)
}
}

void CHexerView::OnUpdateProcMemory(CCmdUI* pCmdUI)
{
pCmdUI->Enable(GetDocument()->IsProcess());
if (::IsWindow(m_dlgProcMem)) {
pCmdUI->SetCheck(m_dlgProcMem.IsWindowVisible());
}
}

void CHexerView::OnViewProcMemory()
{
if (!::IsWindow(m_dlgProcMem)) {
m_dlgProcMem.Init(GetHexCtrl(), GetDocument()->GetVecProcMemory());
m_dlgProcMem.Create(IDD_PROCMEMORY, AfxGetMainWnd());
}
m_dlgProcMem.ShowWindow(m_dlgProcMem.IsWindowVisible() ? SW_HIDE : SW_SHOW);
}

void CHexerView::SetPaneAlreadyLaunch(UINT uPaneID)
{
switch (uPaneID) {
Expand Down
13 changes: 12 additions & 1 deletion Hexer/CHexerView.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
/*******************************************************************************
* Copyright © 2023-2024 Jovibor https://github.com/jovibor/ *
* Hexer is a Hexadecimal Editor for Windows platform. *
* Official git repository: https://github.com/jovibor/Hexer/ *
* This software is available under "The Hexer License", see the LICENSE file. *
*******************************************************************************/
#pragma once
#include "HexCtrl.h"

import Utility;
import DlgProcMemory;

class CMainFrame;
class CChildFrame;
class CHexerDoc;
class CHexerView final : public CView {
public:
[[nodiscard]] auto GetDataInfo()const->Ut::DATAINFO;
[[nodiscard]] auto GetDlgProcMemory()const->HWND;
[[nodiscard]] auto GetHexCtrl()const->HEXCTRL::IHexCtrl*;
[[nodiscard]] auto GetHWNDForPane(UINT uPaneID) -> HWND;
private:
[[nodiscard]] auto GetMainFrame()const->CMainFrame*;
[[nodiscard]] auto GetChildFrame()const->CChildFrame*;
[[nodiscard]] auto GetDocument()const->CHexerDoc*;
[[nodiscard]] auto GetMainFrame()const->CMainFrame*;
[[nodiscard]] bool IsPaneAlreadyLaunch(UINT uPaneID)const;
void OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)override;
void OnDraw(CDC* pDC)override;
Expand All @@ -30,6 +38,8 @@ class CHexerView final : public CView {
afx_msg void OnSize(UINT nType, int cx, int cy);
void OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)override;
afx_msg void OnUpdateEditEditMode(CCmdUI* pCmdUI);
afx_msg void OnUpdateProcMemory(CCmdUI* pCmdUI);
afx_msg void OnViewProcMemory();
void SetPaneAlreadyLaunch(UINT uPaneID);
void UpdateDlgBkmMgr()const;
void UpdateDlgDataInterp()const;
Expand All @@ -41,6 +51,7 @@ class CHexerView final : public CView {
DECLARE_MESSAGE_MAP();
private:
HEXCTRL::IHexCtrlPtr m_pHexCtrl { HEXCTRL::CreateHexCtrl() };
CDlgProcMemory m_dlgProcMem;
bool m_fIsAlreadyLaunchDlgBkmMgr { false };
bool m_fIsAlreadyLaunchDlgTemplMgr { false };
bool m_fIsAlreadyLaunchDlgDataInterp { false };
Expand Down
28 changes: 17 additions & 11 deletions Hexer/DataLoader.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public:
[[nodiscard]] auto GetMaxVirtOffset()const->std::uint64_t;
[[nodiscard]] auto GetMemPageSize()const->DWORD;
[[nodiscard]] auto GetProcID()const->DWORD;
[[nodiscard]] auto GetVecProcMemory()const->const std::vector<MEMORY_BASIC_INFORMATION>&;
[[nodiscard]] auto GetVirtualInterface() -> HEXCTRL::IHexVirtData*;
[[nodiscard]] bool IsMutable()const;
[[nodiscard]] bool IsProcess()const;
Expand All @@ -53,7 +54,7 @@ private:
std::unique_ptr < std::byte[], decltype([](auto p) { _aligned_free(p); }) > m_pCache { };
std::wstring m_wstrDataPath; //Data path to open.
std::wstring m_wstrFileName; //File name without path, or Process name.
std::vector<MEMORY_BASIC_INFORMATION> m_vecMemRegInfo; //Process memory regions info.
std::vector<MEMORY_BASIC_INFORMATION> m_vecProcMemory; //Process memory regions info.
HANDLE m_hHandle { }; //Handle of a file or process.
HANDLE m_hMapObject { }; //Returned by CreateFileMappingW.
LPVOID m_lpMapBase { }; //Returned by MapViewOfFile.
Expand Down Expand Up @@ -98,7 +99,7 @@ void CDataLoader::Close()

m_pCache.reset();
m_wstrDataPath.clear();
m_vecMemRegInfo.clear();
m_vecProcMemory.clear();
m_hHandle = nullptr;
m_hMapObject = nullptr;
m_lpMapBase = nullptr;
Expand Down Expand Up @@ -148,6 +149,11 @@ auto CDataLoader::GetProcID()const->DWORD
return m_dwProcID;
}

auto CDataLoader::GetVecProcMemory()const->const std::vector<MEMORY_BASIC_INFORMATION>&
{
return m_vecProcMemory;
}

auto CDataLoader::GetVirtualInterface()->HEXCTRL::IHexVirtData*
{
return IsVirtual() ? this : nullptr;
Expand Down Expand Up @@ -233,18 +239,18 @@ void CDataLoader::OnHexGetOffset(HEXCTRL::HEXDATAINFO& hdi, bool fGetVirt)
const auto ullOffset = hdi.stHexSpan.ullOffset;
std::uint64_t u64RegsTotalSize { }; //Regions total size.
if (fGetVirt) {
const auto it = std::find_if(m_vecMemRegInfo.begin(), m_vecMemRegInfo.end(),
const auto it = std::find_if(m_vecProcMemory.begin(), m_vecProcMemory.end(),
[ullOffset, &u64RegsTotalSize](const MEMORY_BASIC_INFORMATION& ref) mutable {
u64RegsTotalSize += ref.RegionSize;
return ullOffset < u64RegsTotalSize; });

if (it != m_vecMemRegInfo.end()) {
if (it != m_vecProcMemory.end()) {
const auto ullOffsetFromRegionBegin = ullOffset - (u64RegsTotalSize - it->RegionSize);
hdi.stHexSpan.ullOffset = reinterpret_cast<ULONGLONG>(it->BaseAddress) + ullOffsetFromRegionBegin;
}
}
else {
const auto it = std::find_if(m_vecMemRegInfo.begin(), m_vecMemRegInfo.end(),
const auto it = std::find_if(m_vecProcMemory.begin(), m_vecProcMemory.end(),
[ullOffset, &u64RegsTotalSize](const MEMORY_BASIC_INFORMATION& ref) mutable {
if (ullOffset >= reinterpret_cast<ULONGLONG>(ref.BaseAddress)
&& ullOffset < (reinterpret_cast<ULONGLONG>(ref.BaseAddress) + ref.RegionSize)) {
Expand All @@ -254,7 +260,7 @@ void CDataLoader::OnHexGetOffset(HEXCTRL::HEXDATAINFO& hdi, bool fGetVirt)
u64RegsTotalSize += ref.RegionSize;
return false; });

if (it != m_vecMemRegInfo.end()) {
if (it != m_vecProcMemory.end()) {
hdi.stHexSpan.ullOffset = u64RegsTotalSize + (ullOffset - reinterpret_cast<ULONGLONG>(it->BaseAddress));
}
}
Expand Down Expand Up @@ -382,8 +388,8 @@ bool CDataLoader::OpenProcess(const Ut::DATAOPEN& dos)
return false;
}

m_vecMemRegInfo.clear();
m_vecMemRegInfo.reserve(16);
m_vecProcMemory.clear();
m_vecProcMemory.reserve(16);
const std::byte* pMemCurr { nullptr };
while (true) {
MEMORY_BASIC_INFORMATION mbi;
Expand All @@ -394,11 +400,11 @@ bool CDataLoader::OpenProcess(const Ut::DATAOPEN& dos)

if (mbi.State == MEM_COMMIT && !(mbi.Protect & (PAGE_NOACCESS | PAGE_GUARD))) {
m_ullMaxVirtOffset = reinterpret_cast<std::uint64_t>(pMemCurr) - 1;
m_vecMemRegInfo.emplace_back(mbi);
m_vecProcMemory.emplace_back(mbi);
}
}

m_stDataSize.QuadPart = std::reduce(m_vecMemRegInfo.begin(), m_vecMemRegInfo.end(), 0ULL,
m_stDataSize.QuadPart = std::reduce(m_vecProcMemory.begin(), m_vecProcMemory.end(), 0ULL,
[](ULONGLONG ullSumm, const MEMORY_BASIC_INFORMATION& ref) { return ullSumm + ref.RegionSize; });
m_dwAlignment = 64;
m_pCache.reset(static_cast<std::byte*>(_aligned_malloc(GetInternalCacheSize(), m_dwAlignment)));
Expand Down Expand Up @@ -477,7 +483,7 @@ auto CDataLoader::ReadProcData(std::uint64_t ullOffset, std::uint64_t ullSize)->
std::uint64_t u64RegsTotalSize { 0ULL }; //Total size of the commited regions of pages.
bool fVestige { false }; //Is there any remainder memory to acquire?

for (const auto& mbi : m_vecMemRegInfo) {
for (const auto& mbi : m_vecProcMemory) {
u64RegsTotalSize += mbi.RegionSize;
std::byte* pAddrToRead { };
std::uint64_t u64SizeToRead { };
Expand Down
9 changes: 3 additions & 6 deletions Hexer/Dialogs/DlgLogger.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,11 @@ void CDlgLogger::OnListGetDispInfo(NMHDR* pNMHDR, LRESULT* /*pResult*/)
if ((pItem->mask & LVIF_TEXT) == 0)
return;

const auto iItemID = pItem->iItem;
if (iItemID < 0 || pItem->iSubItem < 0)
return;

const auto& refData = m_vecData[iItemID];
const auto iItem = pItem->iItem;
const auto& refData = m_vecData[iItem];
switch (pItem->iSubItem) {
case 0: //№.
*std::format_to(pItem->pszText, L"{}", iItemID + 1) = L'\0';
*std::format_to(pItem->pszText, L"{}", iItem + 1) = L'\0';
break;
case 1: //Time.
*std::format_to(pItem->pszText, L"{:%H:%M:%OS}", refData.tmloc) = L'\0';
Expand Down
20 changes: 8 additions & 12 deletions Hexer/Dialogs/DlgOpenProcess.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,14 @@ void CDlgOpenProcess::OnListModulesGetDispInfo(NMHDR* pNMHDR, LRESULT* /*pResult
if ((pItem->mask & LVIF_TEXT) == 0)
return;

const auto iItemID = pItem->iItem;
if (iItemID < 0 || pItem->iSubItem < 0)
return;
const auto iItem = pItem->iItem;

switch (pItem->iSubItem) {
case 0: //Module Name.
pItem->pszText = m_vecModules[iItemID].wstrModName.data();
pItem->pszText = m_vecModules[iItem].wstrModName.data();
break;
case 1: //Working Set.
*std::format_to(pItem->pszText, m_locale, L"{:L}KB", m_vecModules[iItemID].dwWorkingSet / 1024) = L'\0';
*std::format_to(pItem->pszText, m_locale, L"{:L}KB", m_vecModules[iItem].dwWorkingSet / 1024) = L'\0';
break;
default:
break;
Expand Down Expand Up @@ -306,22 +304,20 @@ void CDlgOpenProcess::OnListProcsGetDispInfo(NMHDR* pNMHDR, LRESULT* /*pResult*/
if ((pItem->mask & LVIF_TEXT) == 0)
return;

const auto iItemID = pItem->iItem;
if (iItemID < 0 || pItem->iSubItem < 0)
return;
const auto iItem = pItem->iItem;

switch (pItem->iSubItem) {
case 0: //№.
*std::format_to(pItem->pszText, L"{}", iItemID) = L'\0';
*std::format_to(pItem->pszText, L"{}", iItem) = L'\0';
break;
case 1: //Process Name.
pItem->pszText = m_vecProcs[iItemID].wstrProcName.data();
pItem->pszText = m_vecProcs[iItem].wstrProcName.data();
break;
case 2: //Process ID.
*std::format_to(pItem->pszText, L"{}", m_vecProcs[iItemID].dwProcID) = L'\0';
*std::format_to(pItem->pszText, L"{}", m_vecProcs[iItem].dwProcID) = L'\0';
break;
case 3: //Working Set.
*std::format_to(pItem->pszText, m_locale, L"{:L}KB", m_vecProcs[iItemID].dwWorkingSet / 1024) = L'\0';
*std::format_to(pItem->pszText, m_locale, L"{:L}KB", m_vecProcs[iItem].dwWorkingSet / 1024) = L'\0';
break;
default:
break;
Expand Down
Loading

0 comments on commit b1154c0

Please sign in to comment.