DLL生成lib

import sys
import os
import os.path
import glob

def genLib(dllpath, arch, outdir):
    dirname = os.path.dirname(dllpath)
    dllname = os.path.basename(dllpath).split(".")[0]
    deftemp = "%s/%s_temp.temp"%(dirname, dllname)
    defout = "%s/%s.def"%(dirname, dllname)
    if outdir == "":
        outdir = dirname
    if deftemp[0] == "/":
        deftemp = deftemp[1:]
        defout = defout[1:]
    defcmd = "dumpbin %s /exports /out:%s"%(dllpath, deftemp)
    print defcmd
    os.system(defcmd)
    f_temp = open(deftemp, "r")
    f_out = open(defout, "wb")
    f_out.write("LIBRARY %s\n"%(dllname))
    f_out.write("EXPORTS\n")
    begin = False
    for line in f_temp:
        line_s = line.strip().split()
        if len(line_s) < 1:
            continue
        if line_s[0] == "ordinal":
            begin = True
            continue
        if line_s[0] == "Summary":
            begin = False
            break
        if begin == True:
            f_out.write("\t%s\n"%(line_s[3]))
        #print "line_s:%s"%line_s[0]
    f_out.close()
    f_temp.close()
    os.remove(deftemp)
    outlib = "%s/%s.lib"%(outdir, dllname)
    if outlib[0] == "/":
        outlib = outlib[1:]
    libcmd = "lib /def:%s /MACHINE:%s /out:%s"%(defout, arch, outlib)
    print libcmd
    os.system(libcmd)
#需要先通过VS2010的快捷键切换到cmd下设置好环境变量
if __name__ == "__main__":
    wildcard = sys.argv[1]
    arch = "X86"
    if len(sys.argv) > 2:
        arch = sys.argv[2]
    outdir = ""
    if len(sys.argv) > 3:
        outdir = sys.argv[3]
    if not os.path.exists(outdir) and outdir != "":
        os.makedirs(outdir)
    names = glob.glob(wildcard)
    for dllpath in names:
        genLib(dllpath, arch, outdir)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值