I will explain the code briefly. I want to add the contents of some .dlls in a specific binary (exe). When I need it, I will remove the dlls from within this binary. This process will perform better in the CI / CD processes. The following code works perfectly for a 32-bit python interpreter and a 32-bit binary.
Issue Report: I can't do this process for 64-bit binaries even with the 64-bit interpreter. With the 64-bit interpreter I cannot load 32-bit or 64-bit binaries. The question is whether there is a way or perhaps a library like win32api, like a "win64api"?
The result for the attempt is always:
pywintypes.error: (193, 'LoadLibrary', '% 1 is not a valid Win32 application.')
My question: Is there any form / module / library that can perform this task on 64-bit files?
Environment and sample binaries:
Works perfectly:
- Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 09:44:33) [MSC v.1900 32 bit (Intel)] on win32
- https://www.python.org/ftp/python/3.7.7/python-3.7.7.exe (or any other 32-bit binary)
Doesn't work:
Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit (AMD64)] on win32
https://www.python.org/ftp/python/3.7.7/python-3.7.7-amd64.exe (or any other 64-bit binary)
Local specs
platform.platform() >> 'Windows-10-10.0.18362-SP0'
platform.uname() >> uname_result(system='Windows', node='DESKTOP-SER206K', release='10', version='10.0.18362', machine='AMD64', processor='AMD64 Family 21 Model 2 Stepping 0, AuthenticAMD')
Sample code
import os
import win32api
import win32con
import base64
binary = "C:\\Users\\Guto\\Documents\\python\\python-3.7.7.exe"
assert os.path.exists(binary)
PATH_RC = "C:\\Users\\Guto\\Documents\\python\\sqlite3.dll"
assert os.path.exists(PATH_RC)
# Get a handle that can be used by the UpdateResource()
h = win32api.BeginUpdateResource(binary, 0)
rc_content = open(PATH_RC, "rb").read()
rc_content_b64 = base64.b64encode(rc_content)
win32api.UpdateResource(h, win32con.RT_STRING, "rc_content_b64", rc_content_b64)
# End the update resource of the handle.
win32api.EndUpdateResource(h, 0)
#at this point, I have a modified binary
#then I will access that information
h = win32api.LoadLibrary(binary)
r_list = win32api.EnumResourceNames(h, win32con.RT_STRING)
#Find and Load a resource component
resource = win32api.LoadResource(h, win32con.RT_STRING, "rc_content_b64")
new_rc_content = base64.b64decode(resource)
#Write the DLL again
NEW_PATH_RC = "C:\\Users\\Guto\\Documents\\python\\new_dll.dll"
with open(NEW_PATH_RC, "wb") as f:
f.write(new_rc_content)