Determine if the executable (or library) is 32 or 64-bit (on Windows)

I am trying to find out if a given executable file (or library) has been compiled for 32-bit or 64-bit from Python. I am running 64-bit versions of Vista and would like to determine if any application is compiled in the directory for 32-bit or 64-bit.

Is there an easy way to do this using only the standard Python libraries (currently 2.5.4 is used)?

+7
python windows 64bit dll executable
source share
4 answers

Windows API for this GetBinaryType . You can call this from Python using pywin32 :

 import win32file type=GetBinaryType("myfile.exe") if type==win32file.SCS_32BIT_BINARY: print "32 bit" # And so on 

If you want to do this without pywin32, you will need to read the PE header yourself. Here's an example in C #, and here's a quick port for Python:

 import struct IMAGE_FILE_MACHINE_I386=332 IMAGE_FILE_MACHINE_IA64=512 IMAGE_FILE_MACHINE_AMD64=34404 f=open("c:\windows\explorer.exe", "rb") s=f.read(2) if s!="MZ": print "Not an EXE file" else: f.seek(60) s=f.read(4) header_offset=struct.unpack("<L", s)[0] f.seek(header_offset+4) s=f.read(2) machine=struct.unpack("<H", s)[0] if machine==IMAGE_FILE_MACHINE_I386: print "IA-32 (32-bit x86)" elif machine==IMAGE_FILE_MACHINE_IA64: print "IA-64 (Itanium)" elif machine==IMAGE_FILE_MACHINE_AMD64: print "AMD64 (64-bit x86)" else: print "Unknown architecture" f.close() 
+19
source share

If you are using Python 2.5 or later on Windows, you can also use the Windows API without pywin32 using ctypes.

 from ctypes import windll, POINTER from ctypes.wintypes import LPWSTR, DWORD, BOOL SCS_32BIT_BINARY = 0 # A 32-bit Windows-based application SCS_64BIT_BINARY = 6 # A 64-bit Windows-based application SCS_DOS_BINARY = 1 # An MS-DOS-based application SCS_OS216_BINARY = 5 # A 16-bit OS/2-based application SCS_PIF_BINARY = 3 # A PIF file that executes an MS-DOS-based application SCS_POSIX_BINARY = 4 # A POSIX-based application SCS_WOW_BINARY = 2 # A 16-bit Windows-based application _GetBinaryType = windll.kernel32.GetBinaryTypeW _GetBinaryType.argtypes = (LPWSTR, POINTER(DWORD)) _GetBinaryType.restype = BOOL def GetBinaryType(filepath): res = DWORD() handle_nonzero_success(_GetBinaryType(filepath, res)) return res 

Then use GetBinaryType in the same way as with win32file.GetBinaryType.

Please note: you will need to implement handle_nonzero_success, which basically throws an exception if the return value is 0.

+4
source share

After completing this setup, I was able to successfully use Martin B's answer in Python 3.5:

 s=f.read(2).decode(encoding="utf-8", errors="strict") 

Initially, it worked perfectly with my program in Python 2.7, but after making other necessary changes, I found that I was getting b'MZ ', and the decryption seems to be fixed.

0
source share
  1. Using 32-bit Python 3.7 on 64-bit Win 7, the first code snippet in the top answer does not work for me. Failure because GetBinaryType is an unknown character. win32file.GetBinaryType is the use of win32file.GetBinaryType .
  2. Also, running it on a pyd file does not work, even if it is renamed to dll:

    import joked

    import win32file from pathlib

    myDir = Path ("C: \ Users \ rdboylan \ AppData \ Roaming \ Python \ Python37 \ site-packages \ pythonwin") for fn in ("Pythonwin.exe", "win32ui.pyd"): print (fn, end = ":") myf = myDir / fn if myf.suffix == ".pyd": mytemp = myf.with_suffix (". dll"), if mytemp.exists (): raising "Cannot create temporary dll, because { } ". format (mytemp) shutil.copyfile (myf, mytemp) type = win32file.GetBinaryType (str (mytemp)) mytemp.unlink () else: type = win32file.GetBinaryType (str (myf)), if type == win32file .SCS_32BIT_ print ("32 bit") else: print ("Something else") # And so on

    [I do not know why this will not format properly. Sorry.]

Results in

 Pythonwin.exe: 32 bit win32ui.pyd: Traceback (most recent call last): File "C:/Users/rdboylan/Documents/Wk devel/bitness.py", line 14, in <module> type = win32file.GetBinaryType(str(mytemp)) pywintypes.error: (193, 'GetBinaryType', '%1 is not a valid Win32 application.') 
0
source share

All Articles