0

I am attempting to load two C++ DLLs in a Python program using ctypes, however, one of the modules throws an error despite the other loading completely fine. Here is the code:

import ctypes

os.add_dll_directory(r"C:\Users\{user}\PycharmProjects\pythonProject")

A = ctypes.cdll.LoadLibrary("A.dll")
B = ctypes.cdll.LoadLibrary("B.dll")

As stated, I am trying to load two DLLs, let's say we have "A.dll" and "B.dll".

"B.dll" is dependent on "A.dll", so I am loading "A.dll" first. This load works, so we know the libraries are located in the correct directory. However, when attempting to load "B.dll", this error is thrown:

FileNotFoundError: Could not find module 'B.dll' (or one of its dependencies). Try using the full path with constructor syntax.

Since we know they are located in the correct directory, the issue could only be that it could not locate one of its dependencies. However, I already loaded the library that "B.dll" is dependent on, so I'm not sure what exactly could be missing.

When using this library elsewhere, such as in native C++ code, it is a requirement to also include the library's header file, for example "B.h". I'm wondering if that could be the issue here - is it necessary to somehow include the header file that is associated with "B.dll" in order to be located by ctypes?

fault
  • 29
  • 6
  • Are you sure b.dll doesn't have some other dependency that isn't being found? – SoronelHaetir Aug 22 '23 at 02:00
  • @SoronelHaetir I ran b.dll through DependencyWalker and the only dependencies listed were a.dll and two other windows libraries, which were "ws2_32.dll" and "kernel32.dll". I went ahead and included those in the folder and imported them but b.dll still fails to load. I didn't expect this to work because it hasn't been a requirement to import those when using the dll in C++ programs. So, I'm not sure what other dependency could be missing. – fault Aug 22 '23 at 20:37
  • [\[SO\]: How to create a Minimal, Reproducible Example (reprex (mcve))](https://stackoverflow.com/help/minimal-reproducible-example). The "obfuscation" part (*{user}*, *A.dll*, ...) just seems silly. – CristiFati Aug 23 '23 at 05:37
  • @CristiFati Apologies, though I'd rather not share my personal name and the library names are quite long - I thought I might make things more straightforward. – fault Aug 23 '23 at 17:54
  • What *Python* version are you using? [\[SO\]: Discover missing module using command-line ("DLL load failed" error) (@CristiFati's answer)](https://stackoverflow.com/a/74883130/4788546) (and referenced *URL*s) might help. – CristiFati Aug 23 '23 at 18:06
  • @CristiFati The environment is using Python 3.10. I downloaded [Dependencies](https://github.com/lucasg/Dependencies) from your [post](https://stackoverflow.com/a/74883130/4788546) and opened B.dll, again it's showing that the only dependencies are: 1. **A.dll** 2. **WS2_32.dll** 3. **kernel32.dll**. I can provide a screenshot if needed. – fault Aug 23 '23 at 18:52
  • Probably it would help. Make sure paths are visible. Also include the whole run output. – CristiFati Aug 23 '23 at 19:18
  • @CristiFati Hi, I was able to get it working by changing the search path directly to the location where the DLL was compiled, which contained the source code. I suppose it was searching for some ambiguous dependencies from that directory. Thanks! – fault Aug 23 '23 at 20:34

0 Answers0