How to use win32api (or similar) on 64-bit components?
我将简要解释代码。我想在特定的二进制文件(exe)中添加某些.dll的内容。当我需要它时,我将从该二进制文件中删除dll。此过程将在CI / CD过程中执行得更好。
以下代码对于32位python解释器和32位二进制文??件非常适用。
问题报告:
即使使用64位解释器,我也无法对64位二进制文??件执行此过程。
使用64位解释器,我无法加载32位或64位二进制文??件。问题是是否有办法或者像win32api这样的库,例如" win64api"?
尝试的结果始终为:
1 | pywintypes.error: (193, 'LoadLibrary', '% 1 is not a valid Win32 application.') |
我的问题:
有没有可以在64位文件上执行此任务的表单/模块/库?
环境和示例二进制文件:
完美运行:
-
Python 3.7.7(标签/v3.7.7:d7c567b08f,2020年3月10日,09:44:33)[MSC
v.1900 32位(Intel)]在win32上 -
https://www.python.org/ftp/python/3.7.7/python-3.7.7.exe(或任何其他
其他32位二进制文??件)
不起作用:
-
Python 3.7.7(标签/v3.7.7:d7c567b08f,2020年3月10日,10:41:24)[MSC
v32上的1900 64位(AMD64)版本[pb> -
https://www.python.org/ftp/python/3.7.7/python-3.7.7-amd64.exe(或
任何其他64位二进制文??件)
当地规格
-
platform.platform()>>'Windows-10-10.0.18362-SP0'
-
platform.uname()>> uname_result(system ='Windows',
节点='DESKTOP-SER206K',版本= '10',版本= '10 .0.18362',
计算机='AMD64',处理器='AMD64系列21模型2步进0,
AuthenticAMD')
示例代码
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 | 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\\\ ew_dll.dll" with open(NEW_PATH_RC,"wb") as f: f.write(new_rc_content) |
64位进程根本无法加载32位DLL,因此根本无法正常工作。当然,64位Windows仍可以运行32位可执行文件,而那些可执行文件可以加载32位DLL文件。