2

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 )
Community
  • 1
  • 1
Shawn Zhang
  • 1,327
  • 1
  • 10
  • 18
  • What *Python* version are you using? What do you mean by "*to pass `0x00000002` as `256` to `winmode`*" (as it makes no sense)? Also post the stacktrace. Doesn't your last snippet work? – CristiFati Jan 12 '20 at 03:09
  • @CristiFati thanks, I am using python3.8 . It was a typo. Should be `0x00000100` ( `256` in decmial ). Just Python complain can't find `B.DLL`. – Shawn Zhang Jan 13 '20 at 16:26
  • I imagined. First of all, how do you know it's *B.dll* that's not found? Please add the stacktrace. Second, take a look at https://stackoverflow.com/questions/58631512/pywin32-and-python-3-8-0/58632354#58632354. It seems different, but I think it's the same issue (so I can mark this as a duplicate). Let me know how it works out... – CristiFati Jan 13 '20 at 16:43

1 Answers1

1

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)
araisch
  • 63
  • 5
  • Last version worked for me. As I understand 0x8 corresponds to LOAD_WITH_ALTERED_SEARCH_PATH ([doc](https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexw)) but is not available in `nt` module. – Mikolasan Jul 17 '20 at 20:39