Click here to Skip to main content
15,896,063 members
Articles / Desktop Programming / MFC

MemSpyy

Rate me:
Please Sign up or sign in to vote.
4.95/5 (9 votes)
29 Oct 2007CPOL2 min read 34.7K   793   33  
Using OpenGL to map the virtual memory address space.
// MemSpyyDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MemSpyy.h"
#include "MemSpyyDlg.h"
#include ".\MemSpyydlg.h"
#include "psapi.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CMemSpyyDlg dialog



CMemSpyyDlg::CMemSpyyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMemSpyyDlg::IDD, pParent)
	, m_PID(0)
	, m_hLibModule(0)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMemSpyyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LARGEST, m_LargestCtl);
	DDX_Text(pDX, IDC_PID, m_PID);
	DDX_Control(pDX, IDC_TOTALFREE, m_TotalFreeCtl);
	DDX_Control(pDX, IDC_OGLCUSTOM, m_OpenGLControl);
	DDX_Control(pDX, IDC_TOTALDLL, m_TotalDllCtl);
	DDX_Control(pDX, IDC_PID, m_PIDCtl);
	DDX_Control(pDX, IDC_PIDNAME, m_PIDNameCtl);
	DDX_Control(pDX, IDC_STATUS, m_StatusCtl);
}

BEGIN_MESSAGE_MAP(CMemSpyyDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_TIMER()
	ON_EN_UPDATE(IDC_PID, OnEnUpdatePid)
END_MESSAGE_MAP()


// CMemSpyyDlg message handlers

BOOL CMemSpyyDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	m_nTimer = SetTimer(1, 1000, 0);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMemSpyyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMemSpyyDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMemSpyyDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CMemSpyyDlg::SetLargestFree( unsigned long inLargestFree )
{
	char msg[255];
	sprintf( msg, "%4.2fMB", (double) ( (double) inLargestFree / 1024.0 / 1024.0) );
	m_LargestCtl.SetWindowText( msg );
}

void CMemSpyyDlg::SetTotalFree( unsigned long inTotalFree )
{
	char msg[255];
	sprintf( msg, "%4.2fMB", (double) ((double)inTotalFree / 1024.0 / 1024.0 ) );
	m_TotalFreeCtl.SetWindowText( msg );
}

void CMemSpyyDlg::SetTotalDLL( unsigned long inTotalDLL )
{
	char msg[255];
	sprintf( msg, "%4.2fMB", (double) ( (double)inTotalDLL / 1024.0 / 1024.0 ) );
	m_TotalDllCtl.SetWindowText( msg );
}

void CMemSpyyDlg::OnTimer(UINT nIDEvent)
{
	m_OpenGLControl.ForceDraw();

	CDialog::OnTimer(nIDEvent);
}

void CMemSpyyDlg::OnEnUpdatePid()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_UPDATE flag ORed into the lParam mask.

	UpdateData();

	m_OpenGLControl.SetPID( m_PID );
	m_PIDNameCtl.SetWindowText( GetEXENameByPID( m_PID ) );
}

CString CMemSpyyDlg::GetEXENameByPID( DWORD inPID )
{
	char pathName[255];

	HANDLE process = OpenProcess( PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,false, inPID );
	GetProcessImageFileName( process, pathName, 255);
	CString str = CString(pathName);

	CString strExeName = str.Right( str.GetLength() - str.ReverseFind( '\\') - 1 );

	return strExeName;
}

void CMemSpyyDlg::UpdateStatus( PVOID inBaseAddress )
{
	// Get a handle to the process.
	HANDLE process = OpenProcess( PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,false, m_PID );

	SIZE_T retVal = 0;
	MEMORY_BASIC_INFORMATION memBlock;
	retVal = VirtualQueryEx(process, inBaseAddress,&memBlock,sizeof(memBlock));
	if (retVal)
	{
		if (memBlock.State & MEM_FREE)
		{
			char tmpBuffer[255];
			sprintf( tmpBuffer, "Base: 0x%p Size: %4.2fMB   Description: Free", inBaseAddress, (double)memBlock.RegionSize/1024/1024);
			m_StatusCtl.SetWindowText( tmpBuffer );
		}
		else
		{
			char mappedFile[255];
			char tmpBuffer[255];
			if ( GetMappedFileName( process, inBaseAddress, mappedFile, 255) )
			{
				sprintf( tmpBuffer, "Base: 0x%p Size: %4.2fMB   Description: %s", inBaseAddress, (double) memBlock.RegionSize/1024/1024, strrchr(mappedFile, '\\') + 1 );
				m_StatusCtl.SetWindowText( tmpBuffer );
			}
			else
			{
				sprintf( tmpBuffer, "Base: 0x%p Size: %4.2fMB   Description: committed", inBaseAddress, (double) memBlock.RegionSize/1024/1024 );
				m_StatusCtl.SetWindowText( tmpBuffer );
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
PJ currently works for Avid Technology, and lives in Cambridge, MA. When he isn't coding, you can find him collecting old dusty funk & soul records.

Comments and Discussions