private void FindHeader(Image_Resource_Directory resourceDir) { int entryCount = resourceDir.NumberOfIdEntries + resourceDir.NumberOfNamedEntries; for (int i = 0; i < entryCount; i++) { Image_Resource_Directory_Entry entry = resourceDir.ImageResourceDirectoryEntries[i]; if ((entry.OffsetToData & 0x80000000) != 0) { Image_Resource_Directory child = (Image_Resource_Directory)entry.ChildEntry; FindHeader(child); } else { Image_Resource_Data_Entry data = (Image_Resource_Data_Entry)entry.ChildEntry; ICON_HEADER hd = new ICON_HEADER(data.Data); ICON_DIRECTORY[] ds = new ICON_DIRECTORY[hd.Count]; for (int j = 0; j < ds.Length; j++) { ds[j] = new ICON_DIRECTORY(data.Data, 6 + j * 14); if (ds[j].IConID == this.ID) { Header = hd; Directory = ds[j]; return; } } } } }
// level为节点的深度,root深度为0;output为输出目录 public void ExportResource(string output, int level, UInt32 resType) { string currentDir = output; if (level == 0) { currentDir += "\\Resources"; Directory.CreateDirectory(currentDir); } for (int i = 0; i < ImageResourceDirectoryEntries.Length; i++) { Image_Resource_Directory_Entry current = ImageResourceDirectoryEntries[i]; if (current.NameString != null) { string myDir = currentDir + "\\" + ((IMAGE_RESOURCE_DIR_STRING_U)current.NameString).ToString(); Directory.CreateDirectory(myDir); if ((current.OffsetToData >> 31) == 1) { if (level == 0) //只有最顶层的ID才表示资源类型 { resType = 0; //0为Name型资源 } ((Image_Resource_Directory)current.ChildEntry).ExportResource(myDir, level + 1, resType); } else { ((Image_Resource_Data_Entry)current.ChildEntry).ExportResource(myDir, resType); } } else { string myDir = currentDir + "\\" + current.Name.ToString(); Directory.CreateDirectory(myDir); if (level == 0) //只有最顶层的ID才表示资源类型 { resType = current.Name; } if ((current.OffsetToData >> 31) == 1) { ((Image_Resource_Directory)current.ChildEntry).ExportResource(myDir, level + 1, resType); } else { ((Image_Resource_Data_Entry)current.ChildEntry).ExportResource(myDir, resType); } } } }
public Image_Resource_Directory(PEFileInfo fileInfo, long offset, long rootOffset) { byte[] buff = new byte[16]; fileInfo.pFileStream.Position = offset; int len = fileInfo.pFileStream.Read(buff, 0, 16); // 获取Directory的成员 Characteristics = (0xffffffff & buff[0]) + ((0xffffffff & buff[1]) << 8) + ((0xffffffff & buff[2]) << 16) + ((0xffffffff & buff[3]) << 24); TimeDateStamp = (0xffffffff & buff[4]) + ((0xffffffff & buff[5]) << 8) + ((0xffffffff & buff[6]) << 16) + ((0xffffffff & buff[7]) << 24); MajorVersion = (UInt16)(((0xffff & buff[9]) << 8) + buff[8]); MinorVersion = (UInt16)(((0xffff & buff[11]) << 8) + buff[10]); NumberOfNamedEntries = (UInt16)(((0xffff & buff[13]) << 8) + buff[12]); NumberOfIdEntries = (UInt16)(((0xffff & buff[15]) << 8) + buff[14]); // 获取后面的所有Entry //long currentCurrsor = offset + 16; int entryCount = 0; if (NumberOfNamedEntries > 0) { entryCount += NumberOfNamedEntries; } if (NumberOfIdEntries > 0) { entryCount += NumberOfIdEntries; } ImageResourceDirectoryEntries = new Image_Resource_Directory_Entry[entryCount]; if (entryCount > 0) { buff = new byte[8 * entryCount]; len = fileInfo.pFileStream.Read(buff, 0, 8 * entryCount); for (int i = 0; i < entryCount; i++) { ImageResourceDirectoryEntries[i] = new Image_Resource_Directory_Entry(buff, 8 * i, fileInfo, rootOffset); } } }