-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrename_dll.py
42 lines (32 loc) · 1.26 KB
/
rename_dll.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
Make sure you're running this script in a developer command prompt or dumpbin and lib will not be found
"""
import argparse
import subprocess
import re
from shutil import copyfile
parser = argparse.ArgumentParser(description='Renames a dll file, generates new def and lib files')
parser.add_argument('inputdll', help='input dll')
parser.add_argument('outputdll', help='output dll')
args = parser.parse_args()
# dump the dll exports using dumpbin
process = subprocess.Popen(['dumpbin', '/EXPORTS', args.inputdll], stdout=subprocess.PIPE)
out, err = process.communicate()
# get all the function definitions
lines = out.splitlines(keepends=False)
pattern = r'^\s*(\d+)\s+[A-Z0-9]+\s+[A-Z0-9]{8}\s+([^ ]+)'
library_output = 'EXPORTS \n'
for line in lines:
matches = re.search(pattern, line.decode('ascii'))
if matches is not None:
#ordinal = matches.group(1)
function_name = matches.group(2)
library_output = library_output + function_name + '\n'
# write the def file
deffile_name = args.outputdll[:-4] + '.def'
with open(deffile_name, 'w') as f:
f.write(library_output)
process = subprocess.Popen(['lib', '/MACHINE:X86', '/DEF:' + deffile_name], )
out, err = process.communicate()
# copy the dll over
copyfile(args.inputdll, args.outputdll)