PE文件的图标资源是储存在资源表中的,其中图标ID为3 图标组ID为14 只要遍历图标组得到图标头数据和对应图标ID 再根据图标ID遍历资源表得到图标数据 然后合成完整图标数据就可以了,以下为具体实现代码…
#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>
#include <iostream>
using namespace std;
//根据内存偏移地址RVA得到文件偏移地址
DWORD RvaToOffset(PIMAGE_NT_HEADERS pNtHeader, DWORD rva)
{
//PE节
IMAGE_SECTION_HEADER * p_Section_Header;
//获得Pe节数目
DWORD sectionSum = pNtHeader->FileHeader.NumberOfSections;
//第一个节表项
p_Section_Header = (IMAGE_SECTION_HEADER *)((DWORD)pNtHeader + (DWORD)sizeof(IMAGE_NT_HEADERS));
for (int i = 0; i < sectionSum; i++)
{
//printf_s("%s\n", p_Section_Header->Name);
//virtualAddress节区的RVA地址
//sizeofrawdata节区对齐后的尺寸
//PointerToRawData节区起始数据在文件中的偏移
if (p_Section_Header->VirtualAddress <= rva && rva < p_Section_Header->VirtualAddress + p_Section_Header->SizeOfRawData)
{
return rva - p_Section_Header->VirtualAddress + p_Section_Header->PointerToRawData;
}
p_Section_Header++;
}
return 0x00000;
}
//char*转wchar_t*
wchar_t * AnsiToUnicode(const char* szStr)
{
int nLen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0);
if (nLen == 0)
{
return NULL;
}
wchar_t* pResult = new wchar_t[nLen];
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szStr, -1, pResult, nLen);
return pResult;
}
//根据ID得到图标资源
PIMAGE_RESOURCE_DATA_ENTRY ExtractIcoByID(PIMAGE_RESOURCE_DIRECTORY pImageResourceDirectoryRoot, PIMAGE_DOS_HEADER pImageDosHeader, PIMAGE_NT_HEADERS pImageNtHeader, int ID)
{
//遍历资源表根目录
for (int i = 0; i < pImageResourceDirectoryRoot->NumberOfIdEntries + pImageResourceDirectoryRoot-&