Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python ctypes use winmode to load DLLs

Tags:

python

ctypes

I've installed an C++ API application which puts a couple of DLLs (A.DLL and B.DLL) in my programs folder. A.DLL has a dependency on B.DLL

I can load them successfully with ctypes.WinDLL IF from the installation folder like C:\Programs Files\XXX-API\A.DLL

while if I move the folder to another place C:\TEMP\ , the Python cytes load will complain that it can't find B.DLL.

I'm looking into the winmode, looks like it will solve the problem. The winmode seems to take an integer from parameters in MS reference .

for example :

LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR

0x00000100

To use LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR ,to pass 0x00000100 as 256 to winmode ? ctype will complain can not find B.DLL,I'm not sure what's missing ,appreciate any idea from you ,thanks !

ctypes.WinDLL(path_to_A_DLL , winmode = 256 )
like image 880
Shawn Zhang Avatar asked Feb 16 '26 17:02

Shawn Zhang


1 Answers

Alternative 1:

import nt
_func1 = ctypes.WinDLL(lib_name, winmode = nt._LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)

Alternative 2:

_func1 = ctypes.WinDLL(lib_name, winmode = 0x100)

Should both work.

But was trying os.add_dll_directory() all day long. Switched finally to

_func1 = ctypes.WinDLL(absolute_lib_path, winmode = 0x8)
like image 80
araisch Avatar answered Feb 18 '26 07:02

araisch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!