-
-
[分享]获得PE资源树代码
-
2009-1-8 16:07
7326
-
当时做一个软件的副产品,贴出来,供有需要的参考(VB代码)。
' PE 文件资源部分结构
Private Type IMAGE_RESOURCE_DIRECTORY
Characteristics As Long
TimeDateStamp As Long
MajorVersion As Integer
MinorVersion As Integer
NumberOfNamedEntries As Integer
NumberOfIdEntries As Integer
End Type
Private Type IMAGE_RESOURCE_DIRECTORY_ENTRY
Name As Long
OffsetToData As Long
End Type
Private Type IMAGE_RESOURCE_DATA_ENTRY
OffsetToData As Long
Size As Long
CodePage As Long
Reserved As Long
End Type
'资源数据结构
Private Type ResourceLeafInfo
nTypeID As Long '资源类型
pStructRA As Long '此资源的 IMAGE_RESOURCE_DATA_ENTRY 结构相对地址(从资源段算起)
pDataAA As Long '此资源的绝对地址(从文件首部算起)
cbSize As Long '资源大小
End Type
'递归遍历资源树,获得叶节点相关数据
Private Sub PickLeaves(hFile As Long, _
pResOffset As Long, _
pNodeEntry As Long, _
nTypeID As Long, _
tResLeafInfo() As ResourceLeafInfo)
Dim tResDir As IMAGE_RESOURCE_DIRECTORY
Dim tResDirEntry As IMAGE_RESOURCE_DIRECTORY_ENTRY
Dim tResDataEntry As IMAGE_RESOURCE_DATA_ENTRY
Dim i As Long
Call llseek(hFile, pResOffset + pNodeEntry, FILE_BEGIN)
Call lread(hFile, tResDir, Len(tResDir))
For i = 0 To tResDir.NumberOfIdEntries + tResDir.NumberOfNamedEntries - 1
Call llseek(hFile, pResOffset + pNodeEntry + Len(tResDir) + Len(tResDirEntry) * i, FILE_BEGIN)
Call lread(hFile, tResDirEntry, Len(tResDirEntry))
If CBool(tResDirEntry.OffsetToData And &H80000000) Then '指向下一个目录节点
Call PickLeaves(hFile, pResOffset, tResDirEntry.OffsetToData And &H7FFFFFFF, nTypeID, tResLeafInfo())
Else '指向数据入口
Call llseek(hFile, pResOffset + tResDirEntry.OffsetToData, FILE_BEGIN)
Call lread(hFile, tResDataEntry, Len(tResDataEntry))
ReDim Preserve tResLeafInfo(UBound(tResLeafInfo) + 1) As ResourceLeafInfo
With tResLeafInfo(UBound(tResLeafInfo))
.nTypeID = nTypeID
.pStructRA = tResDirEntry.OffsetToData
.pDataAA = tResDataEntry.OffsetToData
.cbSize = tResDataEntry.Size
End With
End If
Next i
End Sub
阿里云助力开发者!2核2G 3M带宽不限流量!6.18限时价,开
发者可享99元/年,续费同价!