📄 leedisasmview.cpp
字号:
// LeeDisasmView.cpp : implementation of the CLeeDisasmView class
//
#include "stdafx.h"
#include "LeeDisasm.h"
#include "LeeDisasmDoc.h"
#include "LeeDisasmView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLeeDisasmView
IMPLEMENT_DYNCREATE(CLeeDisasmView, CListView)
BEGIN_MESSAGE_MAP(CLeeDisasmView, CListView)
//{{AFX_MSG_MAP(CLeeDisasmView)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLeeDisasmView construction/destruction
CLeeDisasmView::CLeeDisasmView()
{
// TODO: add construction code here
m_pData = NULL;
m_pDisasm = NULL;
}
CLeeDisasmView::~CLeeDisasmView()
{
if(m_pData != NULL)
{
free(m_pData);
m_pData = NULL;
}
if(m_pDisasm != NULL)
{
delete m_pDisasm;
m_pDisasm = NULL;
}
}
BOOL CLeeDisasmView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style |= LVS_REPORT;
return CListView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CLeeDisasmView drawing
void CLeeDisasmView::OnDraw(CDC* pDC)
{
CLeeDisasmDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
void CLeeDisasmView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
// TODO: You may populate your ListView with items by directly accessing
// its list control through a call to GetListCtrl().
CListCtrl& m_list = GetListCtrl();
m_list.InsertColumn(0, _T("Address"), LVCFMT_LEFT, 100, -1);
m_list.InsertColumn(1, _T("Binary Code"), LVCFMT_LEFT, 200, -1);
m_list.InsertColumn(2, _T("Assembly Code"), LVCFMT_LEFT, 300, -1);
m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT);
}
/////////////////////////////////////////////////////////////////////////////
// CLeeDisasmView printing
BOOL CLeeDisasmView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CLeeDisasmView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CLeeDisasmView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CLeeDisasmView diagnostics
#ifdef _DEBUG
void CLeeDisasmView::AssertValid() const
{
CListView::AssertValid();
}
void CLeeDisasmView::Dump(CDumpContext& dc) const
{
CListView::Dump(dc);
}
CLeeDisasmDoc* CLeeDisasmView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLeeDisasmDoc)));
return (CLeeDisasmDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CLeeDisasmView message handlers
void CLeeDisasmView::OnFileOpen()
{
// TODO: Add your command handler code here
char szFilter[256];
strcpy(szFilter, "All PE files(*.exe, *.dll)|*.exe;*.dll|All files(*.*)|*.*||");
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, this);
CString strFilePath = _T("");
if(dlg.DoModal() == IDOK)
{
strFilePath = dlg.GetPathName();
}
if(strFilePath.IsEmpty())
return;
CListCtrl& thelist = GetListCtrl();
thelist.DeleteAllItems();
if(m_pData != NULL)
{
free(m_pData);
m_pData = NULL;
}
FILE* fp = fopen(strFilePath.GetBuffer(0), "rb");
if(fp == NULL)
{
MessageBox("打开文件失败。", "LeeDisasm", MB_OK | MB_ICONINFORMATION);
return;
}
char szTitle[128];
sprintf(szTitle, "LeeDisasm[%s]", strFilePath.GetBuffer(0));
AfxGetMainWnd()->SetWindowText(szTitle);
IMAGE_DOS_HEADER dosh;
fread(&dosh, sizeof(dosh), 1, fp);
if(dosh.e_magic != IMAGE_DOS_SIGNATURE)
{
MessageBox("不是PE文件。", "LeeDisasm", MB_OK | MB_ICONINFORMATION);
fclose(fp);
return;
}
IMAGE_NT_HEADERS32 nth;
fseek(fp, dosh.e_lfanew, SEEK_SET);
fread(&nth, sizeof(nth), 1, fp);
if(nth.Signature != IMAGE_NT_SIGNATURE)
{
MessageBox("不是PE文件。", "LeeDisasm", MB_OK | MB_ICONINFORMATION);
fclose(fp);
return;
}
IMAGE_SECTION_HEADER sech;
int iCnt = 0;
while(iCnt < nth.FileHeader.NumberOfSections)
{
fread(&sech, sizeof(sech), 1, fp);
if(!memcmp(sech.Name, ".text", 5))
break;
iCnt ++;
}
if(iCnt >= nth.FileHeader.NumberOfSections)
{
MessageBox("没有找到.text节。", "LeeDisasm", MB_OK | MB_ICONINFORMATION);
fclose(fp);
return;
}
m_pData = (char*)malloc(sech.SizeOfRawData);
if(m_pData == NULL)
{
MessageBox("申请内存失败。", "LeeDisasm", MB_OK | MB_ICONINFORMATION);
fclose(fp);
return;
}
fseek(fp, sech.PointerToRawData, SEEK_SET);
fread(m_pData, 1, sech.SizeOfRawData, fp);
fclose(fp);
m_dwAddr = sech.VirtualAddress+nth.OptionalHeader.ImageBase;
m_dwSize = sech.SizeOfRawData;
m_pDisasm = new CDisasm(this);
m_pDisasm->DisAsmCode(m_pData, m_dwAddr, m_dwSize);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -