direct.dist.pefile

from direct.dist.pefile import DataResource, IconGroupResource, PEFile, RVASize, ResourceTable, Section, VersionInfoResource, expdirtab, impdirtab

Tools for manipulating Portable Executable files.

This can be used, for example, to extract a list of dependencies from an .exe or .dll file, or to add version information and an icon resource to it.

Inheritance diagram

Inheritance diagram of direct.dist.pefile

class DataResource[source]

Bases: object

A resource entry in the resource table.

__init__(self)[source]
accelerator = 9
animated_cursor = 21
animated_icon = 22
bitmap = 2
cursor = 1
cursor_group = 12
dialog = 5
dlg_include = 17
property encoding
font = 8
font_directory = 7
get_data(self)[source]
get_text(self, errors='strict')[source]
html = 23
icon = 3
icon_group = 14
manifest = 24
menu = 4
message_table = 11
plug_play = 19
rcdata = 10
string = 6
version = 16
vxd = 20
class IconGroupResource[source]

Bases: object

class Icon(width, height, planes, bpp, size, id)

Bases: tuple

property bpp

Alias for field number 3

property height

Alias for field number 1

property id

Alias for field number 5

property planes

Alias for field number 2

property size

Alias for field number 4

property width

Alias for field number 0

__init__(self)[source]
add_icon(self, *args, **kwargs)[source]
code_page = 0
get_data(self)[source]
type = 14
unpack_from(self, data, offs=0)[source]
class PEFile[source]

Bases: object

add_icon(self, icon, ordinal=2)[source]

Adds an icon resource from the given Icon object. Requires calling add_resource_section() afterwards.

add_resource_section(self)[source]

Adds a resource section to the file containing the resources that were previously added via add_icon et al. Assumes the file does not contain a resource section yet.

add_section(self, name, flags, data)[source]

Adds a new section with the given name, flags and data. The virtual address space is automatically resized to fit the new data.

Returns the newly created Section object.

add_version_info(self, file_ver, product_ver, data, lang=1033, codepage=1200)[source]

Adds a version info resource to the file.

close(self)[source]
get_address_offset(self, addr)[source]

Turns an address into a offset relative to the file beginning.

get_address_section(self, addr)[source]

Returns the section that this virtual address belongs to.

get_export_address(self, symbol_name)[source]

Finds the virtual address for a named export symbol.

imports = ()
open(self, fn, mode='r')[source]
read(self, fp)[source]

Reads a PE file from the given file object, which must be opened in binary mode.

rename_export(self, old_name, new_name)[source]

Renames a symbol in the export table.

write_changes(self)[source]

Assuming the file was opened in read-write mode, writes back the changes made via this class to the .exe file.

class RVASize(addr, size)

Bases: tuple

property addr

Alias for field number 0

property size

Alias for field number 1

class ResourceTable(ident=())[source]

Bases: object

A table in the resource directory.

__init__(self, ident=())[source]
count_resources(self)[source]

Counts all of the resources.

get_nested_tables(self)[source]

Returns all tables in this table and subtables.

items(self)[source]
pack_header(self, data, offs)[source]
unpack_from(self, mem, addr=0, offs=0)[source]
class Section[source]

Bases: object

modified = True
read_header(self, fp)[source]
write_header(self, fp)[source]
class VersionInfoResource[source]

Bases: object

__init__(self)[source]
code_page = 0
get_data(self)[source]
type = 16
unpack_from(self, data)[source]
class expdirtab(flags, timdat, majver, minver, name, ordinal_base, nentries, nnames, entries, names, ordinals)

Bases: tuple

property entries

Alias for field number 8

property flags

Alias for field number 0

property majver

Alias for field number 2

property minver

Alias for field number 3

property name

Alias for field number 4

property names

Alias for field number 9

property nentries

Alias for field number 6

property nnames

Alias for field number 7

property ordinal_base

Alias for field number 5

property ordinals

Alias for field number 10

property timdat

Alias for field number 1

class impdirtab(lookup, timdat, forward, name, impaddr)

Bases: tuple

property forward

Alias for field number 2

property impaddr

Alias for field number 4

property lookup

Alias for field number 0

property name

Alias for field number 3

property timdat

Alias for field number 1