📄 virtual_memdlg.cpp
字号:
// virtual_memDlg.cpp : implementation file
//
#include "stdafx.h"
#include "virtual_mem.h"
#include "virtual_memDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CVirtual_memDlg dialog
CVirtual_memDlg::CVirtual_memDlg(CWnd* pParent /*=NULL*/)
: CDialog(CVirtual_memDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CVirtual_memDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CVirtual_memDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CVirtual_memDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CVirtual_memDlg, CDialog)
//{{AFX_MSG_MAP(CVirtual_memDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_COMMIT, OnCommit)
ON_BN_CLICKED(IDC_ALLOC2, OnAlloc2)
ON_BN_CLICKED(IDC_SET_READ, OnSetRead)
ON_BN_CLICKED(IDC_SET_RW, OnSetRw)
ON_BN_CLICKED(IDC_WRITE, OnWrite)
ON_BN_CLICKED(IDC_READ, OnRead)
ON_BN_CLICKED(IDC_FREE2, OnFree2)
ON_BN_CLICKED(IDC_FREE, OnFree)
ON_BN_CLICKED(IDC_QUERY, OnQuery)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CVirtual_memDlg message handlers
BOOL CVirtual_memDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CVirtual_memDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (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 to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CVirtual_memDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
#define BASE_PTR 0X60000000
void CVirtual_memDlg::OnAlloc2()
{
if(VirtualAlloc((LPVOID)BASE_PTR,1024*128,MEM_RESERVE,PAGE_READWRITE))
{
AfxMessageBox("成功");
}
else
{
AfxMessageBox("失败");
}
}
void CVirtual_memDlg::OnCommit()
{
if(VirtualAlloc((LPVOID)BASE_PTR,1024*128,MEM_COMMIT,PAGE_READWRITE))
{
AfxMessageBox("成功");
}
else
{
AfxMessageBox("失败");
}
}
void CVirtual_memDlg::OnSetRead()
{
DWORD dwOld;
if(VirtualProtect((LPVOID)BASE_PTR,1024*128,PAGE_READONLY,&dwOld))
{
AfxMessageBox("成功");
}
else
{
AfxMessageBox("失败");
}
}
void CVirtual_memDlg::OnSetRw()
{
DWORD dwOld;
if(VirtualProtect((LPVOID)BASE_PTR,1024*128,PAGE_READWRITE,&dwOld))
{
AfxMessageBox("成功");
}
else
{
AfxMessageBox("失败");
}
}
void CVirtual_memDlg::OnWrite()
{
memset((void*)BASE_PTR,'A',128*1024);
}
void CVirtual_memDlg::OnRead()
{
char buf[1024*128];
memcpy(buf,(void*)BASE_PTR,128*1024);
}
void CVirtual_memDlg::OnFree2()
{
if(VirtualFree((LPVOID)BASE_PTR,1024*128,MEM_DECOMMIT))
{
AfxMessageBox("成功");
}
else
{
AfxMessageBox("失败");
}
}
void CVirtual_memDlg::OnFree()
{
if(VirtualFree((LPVOID)BASE_PTR,0,MEM_RELEASE))
{
AfxMessageBox("成功");
}
else
{
AfxMessageBox("失败");
}
}
void CVirtual_memDlg::OnQuery()
{
MEMORY_BASIC_INFORMATION buf;
if(VirtualQuery((LPVOID)(BASE_PTR),&buf,sizeof(buf)))
{
CString szOut;
szOut.Format("地址1 %x\n地址2 %x\n申请时的保护方式 %x\n大小 %x\n状态 %x\n提交时的保护方式 %x",
buf.BaseAddress,buf.AllocationBase,buf.AllocationProtect,buf.RegionSize,
buf.State,buf.Protect);
AfxMessageBox(szOut);
}
else
{
AfxMessageBox("失败");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -