Using the pefile.py file to get the file (.exe)

I want to use python to get the version of the executable, and I know pefile.py

How to use it for this?

Notes: The executable file may be incomplete.

+4
source share
3 answers

Here is a complete example script that does what you want:

import sys def main(pename): from pefile import PE pe = PE(pename) if not 'VS_FIXEDFILEINFO' in pe.__dict__: print "ERROR: Oops, %s has no version info. Can't continue." % (pename) return if not pe.VS_FIXEDFILEINFO: print "ERROR: VS_FIXEDFILEINFO field not set for %s. Can't continue." % (pename) return verinfo = pe.VS_FIXEDFILEINFO filever = (verinfo.FileVersionMS >> 16, verinfo.FileVersionMS & 0xFFFF, verinfo.FileVersionLS >> 16, verinfo.FileVersionLS & 0xFFFF) prodver = (verinfo.ProductVersionMS >> 16, verinfo.ProductVersionMS & 0xFFFF, verinfo.ProductVersionLS >> 16, verinfo.ProductVersionLS & 0xFFFF) print "Product version: %d.%d.%d.%d" % prodver print "File version: %d.%d.%d.%d" % filever if __name__ == '__main__': if len(sys.argv) != 2: sys.stderr.write("ERROR:\n\tSyntax: verinfo <pefile>\n") sys.exit(1) sys.exit(main(sys.argv[1])) 

Matching lines:

  verinfo = pe.VS_FIXEDFILEINFO filever = (verinfo.FileVersionMS >> 16, verinfo.FileVersionMS & 0xFFFF, verinfo.FileVersionLS >> 16, verinfo.FileVersionLS & 0xFFFF) prodver = (verinfo.ProductVersionMS >> 16, verinfo.ProductVersionMS & 0xFFFF, verinfo.ProductVersionLS >> 16, verinfo.ProductVersionLS & 0xFFFF) 

all this happens only after verifying that we have something meaningful in these properties.

+2
source

The version numbers of Windows programs are stored in the resources section of the program file, and not in the PE format header. I am not familiar with pefile.py , so I do not know if it handles resource sections directly. If not, you can find the information you need in this MSDN article .

0
source

Assuming that the “version of the executable file” means a) on Windows, b) the information shown on the Properties tab, Details on the File Version section, you can get it using pywin32 with the following command:

 >>> import win32api as w >>> hex(w.GetFileVersionInfo('c:/windows/regedit.exe', '\\')['FileVersionMS']) '0x60000' >>> hex(w.GetFileVersionInfo('c:/windows/regedit.exe', '\\')['FileVersionLS']) '0x17714650' 

Please note that 0x60000 has primary / minor numbers (6.0), and 0x17714650 is the next two, which, if taken as two separate words (0x1771 and 0x4650, or 6001 and 18000 in decimal form), correspond to the values ​​on my machine, where Version regedit 6.0.6001.18000.

0
source

All Articles