Skip to content

Instantly share code, notes, and snippets.

@sefgit
Last active June 19, 2024 01:48
Show Gist options
  • Save sefgit/f9804cf0f8f11477dadbdfc13387367d to your computer and use it in GitHub Desktop.
Save sefgit/f9804cf0f8f11477dadbdfc13387367d to your computer and use it in GitHub Desktop.
Create import library from dll
#
# to_lib.py
# Create import library from dll
# by seffx
# Jakarta, 2024
#
# DISCLAIMER:
# Use it on your own risk, this code is provided to PUBLIC DOMAIN, "AS IS".
#
# Usage note:
# ===========
# Run this python script inside Visual Studio Developer Command Prompt (x86 or x64)
#
# https://learn.microsoft.com/en-us/visualstudio/ide/reference/command-prompt-powershell?view=vs-2022
#
import os
import sys
MACHINE='x86' # 32 bits
#MACHINE='x64' # 64 bits
if len(sys.argv) <= 1:
print("Usage:")
print(f"\t{sys.argv[0]} DLL_FILE.dll")
print()
exit()
if not os.path.isdir("defs"):
os.mkdir("defs")
for path in sys.argv[1:]:
dllname = os.path.basename(path)
name = os.path.splitext(dllname)[0]
#print(path)
print("* ", dllname)
#print(name)
os.system(f'dumpbin /NOLOGO /exports "{path}" > "defs\\{name}.exports"')
os.system(f'undname "defs\\{name}.exports" > "defs\\{name}.exports_undecorated"')
with open(f"defs\\{name}.exports","r") as f:
line = None
while True:
line = f.readline()
if not line:
break
line = line.strip()
if line.startswith("ordinal"):
line = f.readline()
break
if not line:
break
with open(f"defs\\{name}.def","w") as o:
o.write(f"LIBRARY {name}\n")
o.write("EXPORTS\n")
while True:
line = f.readline()
line = line.strip()
if line == "":
break
v = line.split()
o.write(f" {v[3]} @{v[0]}\n")
os.system(f'lib /NOLOGO /def:"defs\\{name}.def" /out:"defs\\{name}.lib" /machine:{MACHINE}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment