第一種為使用 win32file 裡面的 method,GetBinaryType 這個 method 在ms 的官方文件也有提到
http://msdn.microsoft.com/en-us/library/aa364819(VS.85).aspx
第二種方法為 直接讀取binary判斷byte 裡面的資料....
s
# -*- coding: utf-8 -*-
#source page:
#http://stackoverflow.com/questions/1345632/determine-if-an-executable-or-library-is-32-or-64-bits-on-windows
import win32file
if __name__ == "__main__":
spath = r"C:\Temp\somewher\Your_File64_or_32.exe"
SCS_64BIT_BINARY = 6#在這裡特別定義這個constant 原因就是因為 win32file 裡面沒有定義這個(我是使用 python2.5.2.2 32bit)
type = win32file.GetBinaryType(spath)
print "type", type
if type == win32file.SCS_32BIT_BINARY:
print "A 32-bit Windows-based application"
elif type == SCS_64BIT_BINARY:
print "A 64-bit Windows-based application."
elif type == win32file.SCS_DOS_BINARY:
print "An MS-DOS – based application"
elif type == win32file.SCS_OS216_BINARY:
print "A 16-bit OS/2-based application"
elif type == win32file.SCS_PIF_BINARY:
print "A PIF file that executes an MS-DOS – based application"
elif type == win32file.SCS_POSIX_BINARY:
print "A POSIX – based application"
elif type == win32file.SCS_WOW_BINARY:
print "A 16-bit Windows-based application"
Constant | Value | Meaning |
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 |
Constant | Value | Meaning |
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 |
http://msdn.microsoft.com/en-us/library/aa364819(VS.85).aspx
s
s
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()
s
s
s
s