查找exe依赖dll库的Python脚本

打包exe时需要将依赖的dll库一起打包,这些dll库数量多,所以我写了Python脚本将指定exe的依赖dll库输出到指定文件夹中。使用时只需在终端中输入如下命令:

python LoadDll.py 指定exe 输出文件夹 
import sys
import os
import shutil


def load_dll() -> list:
    dll_list = list()
    with open("./dll_list.txt", 'r') as f:
        for line in f.readlines():
            if ".dll" in line:
                dll_list.append(line[4:-1])
    return dll_list


def check_bit() -> int:
    with open("./dll_list.txt", 'r') as f:
        for line in f.readlines():
            if "32 bit word machine" in line:
                return 32
            if "64 bit word machine" in line:
                return 64


def move_32_bit_dll(dll_list: list, output_path: str) -> bool:
    count = 0
    dll_path = "C:/Windows/SysWOW64/"
    opencv_path = "D:/CPPLibs/opencv/build/x86/vc16/bin/"
    qt_path = "D:/APPs/Qt/5.15.2/msvc2019/bin/"
    move_platforms = False

    for dll in dll_list:
        if "opencv_world" == dll[:13] and os.path.isfile(opencv_path + dll):
            shutil.copy(opencv_path + dll, os.path.join(output_path, dll))
            count += 1
        elif os.path.isfile(dll_path + dll):
            shutil.copy(dll_path + dll, os.path.join(output_path, dll))
            count += 1
        elif "Qt5" == dll[:3] and os.path.isfile(qt_path + dll):
            shutil.copy(qt_path + dll, os.path.join(output_path, dll))
            count += 1
            if not move_platforms: move_platforms = True
            
    shutil.copy(dll_path + "msiexec.exe", os.path.join(output_path, "msiexec.exe"))

    if move_platforms:
        platforms_path = "D:/APPs/Qt/5.15.2/msvc2019/plugins/platforms/"
        os.makedirs(os.path.join(output_path, "platforms"))
        for file in os.listdir(platforms_path):
            if ".dll" in file:
                shutil.copy(platforms_path + file, os.path.join(os.path.join(output_path, "platforms"), file))

    return count == len(dll_list)


def move_64_bit_dll(dll_list: list, output_path: str) -> bool:
    count = 0
    dll_path = "C:/Windows/System32/"
    opencv_path = "D:/CPPLibs/opencv/build/x64/vc16/bin/"
    qt_path = "D:/APPs/Qt/5.15.2/msvc2019_64/bin/"
    move_platforms = False

    for dll in dll_list:
        if "opencv_world" == dll[:13] and os.path.isfile(opencv_path + dll):
            shutil.copy(opencv_path + dll, os.path.join(output_path, dll))
            count += 1
        elif os.path.isfile(dll_path + dll):
            shutil.copy(dll_path + dll, os.path.join(output_path, dll))
            count += 1
        elif "Qt5" == dll[:3] and os.path.isfile(qt_path + dll):
            shutil.copy(qt_path + dll, os.path.join(output_path, dll))
            count += 1
            if not move_platforms: move_platforms = True

    shutil.copy(dll_path + "msiexec.exe", os.path.join(output_path, "msiexec.exe"))

    if move_platforms:
        platforms_path = "D:/APPs/Qt/5.15.2/msvc2019_64/plugins/platforms/" 
        os.makedirs(os.path.join(output_path, "platforms"))
        for file in os.listdir(platforms_path):
            if ".dll" in file:
                shutil.copy(platforms_path + file, os.path.join(os.path.join(output_path, "platforms"), file))

    return count == len(dll_list)


def log(dll_list: list, output_path: str) -> None:
    ls0 = os.listdir(output_path)
    ls1 = [dll+"\n" for dll in dll_list if dll not in ls0]
    with open(os.path.join(output_path, "load_dll_log.txt"), 'x') as f:
        f.write("以下dll未被找到:\n")
        f.writelines(ls1)


if __name__ == "__main__":
    _, input_file, output_path = sys.argv
    os.system("D:/APPs/VS/IDE/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/dumpbin /headers /dependents {} >./dll_list.txt".format(input_file))
    dll_list = load_dll()

    if check_bit() == 32:
        if not move_32_bit_dll(dll_list, output_path):
            log(dll_list, output_path)
    else:
        if not move_64_bit_dll(dll_list, output_path):
            log(dll_list, output_path)

    shutil.move("./dll_list.txt", os.path.join(output_path, "dll_list.txt"))

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值