⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 peimagefile.cpp

📁 此为本书的配套光盘.本书不但由浅入深地讲解了软件保护技术
💻 CPP
字号:
/********************************************************************

	Copyright (c) Beijing Feitian Technologies
	http://www.FTSafe.com

	File :		PEImageFile.cpp	

	Created:	2003/11/04

	Author:		yihai
	
	Purpose:	?

	Revision:	?

*********************************************************************/

// PEImageFile.cpp: implementation of the CPEImageFile class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PEViewer.h"
#include "PEImageFile.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPEImageFile::CPEImageFile()
{
	m_hFile=NULL;
	m_hFileMapping=NULL;
	m_lpFileBase=NULL;

}

CPEImageFile::~CPEImageFile()
{
	Close();
}

bool CPEImageFile::Open(LPCTSTR lpFileName,bool bWrite)
{
	Close();
	m_bWrite = bWrite;
	lstrcpy(m_szFileName,lpFileName);

	m_hFile = CreateFile(lpFileName, bWrite?GENERIC_WRITE|GENERIC_READ:GENERIC_READ, FILE_SHARE_READ, NULL,
                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
                    
    if ( m_hFile == INVALID_HANDLE_VALUE )
    {
        TRACE("Couldn't open file with CreateFile()\n");
        return false;
    }

    m_hFileMapping = CreateFileMapping(m_hFile, NULL, bWrite?PAGE_READWRITE:PAGE_READONLY, 0, 0, NULL);
    if ( m_hFileMapping == 0 )
    {
        Close();
        TRACE("Couldn't open file mapping with CreateFileMapping()\n");
        return false;
    }

    m_lpFileBase = MapViewOfFile(m_hFileMapping, bWrite?FILE_MAP_WRITE:FILE_MAP_READ, 0, 0, 0);
    if ( m_lpFileBase == 0 )
    {
        Close();
        TRACE("Couldn't map view of file with MapViewOfFile()\n");
        return false;
    }
	TRACE("Open file %s\n\n", lpFileName);
	ReadHeader();

	return true;
}

void CPEImageFile::ReadHeader()
{
	m_uImageType = TIMG_UNKNOW;    
	m_pDosHdr = (PIMAGE_DOS_HEADER)m_lpFileBase;
	m_pNtHdr = (PIMAGE_NT_HEADERS)((LPBYTE)m_lpFileBase + m_pDosHdr->e_lfanew);

    if( ( m_pDosHdr->e_magic == IMAGE_DOS_SIGNATURE ) &&
		(m_pNtHdr->Signature == IMAGE_NT_SIGNATURE) )
    {				
			m_uImageType = TIMG_PE;
			m_pImgHdr = &(m_pNtHdr->FileHeader);
			m_pFirstSection =  IMAGE_FIRST_SECTION((m_pNtHdr));			
    }
	else
	{
		TRACE("unrecognized file format\n");
		m_uImageType = TIMG_UNKNOW;
	}
}

void CPEImageFile::Close()
{
	if(m_lpFileBase)
	{
		UnmapViewOfFile(m_lpFileBase);
		m_lpFileBase = NULL;
	}
	if(m_hFileMapping)
	{
		CloseHandle(m_hFileMapping);
		m_hFileMapping = NULL;
	}
	if(m_hFile)
	{
		CloseHandle(m_hFile);
		m_hFile = NULL;
	}

	m_pDosHdr=NULL;
	m_pNtHdr=NULL;
	m_pImgHdr=NULL;
	m_lpFileBase=NULL;
	m_pImgHdr=NULL;
	m_pFirstSection=NULL;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -