I am trying to import a .dll in Python using the ctypes package.
import ctypes
example_dll = ctypes.windll.LoadLibrary(".\Example.dll")
This .dll has 2 other .dll dependencies which I have installed in the same location. The code runs fine when I use Anaconda (Python 3.7) but as soon as I run it with my Python3.10 it throws the following error:
FileNotFoundError: Could not find module: '...\Example.dll' (or one of its dependencies)> Try using the full path with constructor syntax.
I tried to use the full path, I used dependencyWalker and the dependencies should be there. It does run with Anaconda so it's unclear to me why I can't use it with my normal Python 3.10. The .dll is a 64 bit and my Python3.10 is as well.
Any ideas?
Update
The dll is 64 bit and my python.exe is 64 as well so this should be fine. I checked also all the .dll dependencies using dumpbin and they all exist (and are 64 bit).
Other things I have tried:
(1) Installed 'normal' python 3.7 since my Anaconda distribution uses python 3.7. Does not resolve the error.
(2) Used os.add_dll_directory(), tried full path for my .dll but this does not resolve the error.
(3) Used ctypes.CDLL(), but no success.
I was comparing the CDLL class in ctypes.init.py between my Anaconda distribution and my normal python and saw a small difference with regards to winmode:
Anaconda
if _os.name == "nt":
if winmode is not None:
mode = winmode
else:
import nt
mode = nt._LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
if '/' in name or '\\' in name:
self._name = nt._getfullpathname(self._name)
mode |= nt._LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
# PATH is how DLL lookup has always worked in the past
# in Python on Windows. IMHO both the above mode flags
# are not wanted and cause many serious regressions within
# the conda ecosystem on Windows. We should however
# propagate any PATH changes that have happened to Python
# library and that is not yet implemented.
LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008
mode = LOAD_WITH_ALTERED_SEARCH_PATH
Python (3.10)
if _os.name == "nt":
if winmode is not None:
mode = winmode
else:
import nt
mode = nt._LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
if '/' in name or '\\' in name:
self._name = nt._getfullpathname(self._name)
mode |= nt._LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
The last part is missing in my normal python. I tried to add it to and rerun, but then I get the following error:
OSError: [WinError 193] %1 is not a valid Win32 application
Which is related to 32 vs 64 bit. Could this be just a bug in Python? It works with Anaconda but not normal python ...
Dependencies on my Example.dll below. I have blacked out the last 2 .dlls as these are proprietary (these two are stored in the same folder as my Example.dll). When I debug my code I can see the issue is with Example.dll and not with the others.