PELib.cpp
资源名称:PEPack.rar [点击查看]
上传用户:sdguojin
上传日期:2022-07-31
资源大小:1726k
文件大小:2k
源码类别:
系统编程
开发平台:
Visual C++
- #include "PELib.h"
- namespace PELib
- {
- CPELib::CPELib()
- {}
- CPELib::~CPELib()
- {
- if( m_pSection )
- {
- delete []m_pSection;
- m_pSection = NULL;
- }
- }
- BOOL CPELib::Init(wstring str_FileName)
- {
- BOOL bRetCode = FALSE;
- DWORD dwTempNum = 0;
- HANDLE hNewFile = ::CreateFile(
- str_FileName.c_str(),
- GENERIC_READ,
- FILE_SHARE_READ,
- NULL,
- OPEN_EXISTING,
- NULL, NULL);
- if( hNewFile == INVALID_HANDLE_VALUE )
- goto Exit0;
- ReadFile(hNewFile, &m_DosHeader, sizeof(IMAGE_DOS_HEADER), &dwTempNum, NULL);
- if( sizeof(IMAGE_DOS_HEADER) != dwTempNum )
- goto Exit0;
- if( 0x5A4D != m_DosHeader.e_magic )
- goto Exit0; //验证PE第一个成员是否等于MZ
- SetFilePointer(hNewFile, m_DosHeader.e_lfanew, NULL, FILE_BEGIN);
- ReadFile(hNewFile, &m_NtHeader, sizeof(IMAGE_NT_HEADERS32), &dwTempNum, NULL);
- if ( sizeof(IMAGE_NT_HEADERS32) != dwTempNum )
- goto Exit0;
- if( 0x4550 != m_NtHeader.Signature)
- goto Exit0;
- DWORD dwSectionNum = m_NtHeader.FileHeader.NumberOfSections;
- m_pSection = new IMAGE_SECTION_HEADER[dwSectionNum];
- if( !m_pSection )
- goto Exit0;
- memset(m_pSection, 0, dwSectionNum * sizeof(IMAGE_SECTION_HEADER));
- ReadFile(hNewFile, m_pSection, sizeof(IMAGE_SECTION_HEADER) * dwSectionNum, &dwTempNum, NULL);
- bRetCode = TRUE;
- Exit0:
- if ( hNewFile )
- {
- CloseHandle(hNewFile);
- hNewFile = NULL;
- }
- return bRetCode;
- }
- }