Python 判斷目前的 DLL or EXE 是32bit 還是 64bit

第一種為使用 win32file 裡面的 method,GetBinaryType 這個 method 在ms 的官方文件也有提到

第二種方法為 直接讀取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"



ConstantValueMeaning
SCS_32BIT_BINARY0A 32-bit Windows-based application
SCS_64BIT_BINARY6A 64-bit Windows-based application.
SCS_DOS_BINARY1An MS-DOS – based application
SCS_OS216_BINARY5A 16-bit OS/2-based application
SCS_PIF_BINARY3A PIF file that executes an MS-DOS – based application
SCS_POSIX_BINARY4A POSIX – based application
SCS_WOW_BINARY2A 16-bit Windows-based application
ConstantValueMeaning
SCS_32BIT_BINARY0A 32-bit Windows-based application
SCS_64BIT_BINARY6A 64-bit Windows-based application.
SCS_DOS_BINARY1An MS-DOS – based application
SCS_OS216_BINARY5A 16-bit OS/2-based application
SCS_PIF_BINARY3A PIF file that executes an MS-DOS – based application
SCS_POSIX_BINARY4A POSIX – based application
SCS_WOW_BINARY2A 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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值