1

I've written a c code and compiled it into a dll, and now I'm trying to import it to Python, then I get the error "OSError: [WinError 126] The specified module could not be found".

I have already searched for solutions: in the code I use some other DLLs:

python36.dll
ioterasdk.dll
KERNEL32.dll

so I checked that all of them are included (except for KERNEL32.dll that I don't know how to check), and I also added "python36.dll"'s location to the System Environment Variables.

The code:

from ctypes import *
mydll = cdll.LoadLibrary("D:\\full\\path\\BlueTeraPy.dll")

I get this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\admin\AppData\Local\Programs\Python\Python36-32\lib\ctypes\__init__.py", line 426, in LoadLibrary
    return self._dlltype(name)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python36-32\lib\ctypes\__init__.py", line 348, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found

I'm new to Stack Overflow and to DLLs in general, so if you need more information please tell. Thank you

Nadavz
  • 11
  • 1
  • 1
    in my experience its usually when your dll is trying to load something and fails, does your dll have dependencies? are they accessible(on your PATH)? – Nullman Apr 15 '19 at 10:11
  • I'm not sure what is the definition of "dependencies". For python36.dll: I added its path to the System Environment Variables, for ioterasdk.dll: I copied its header, dll and lib files to the same folder of BlueTeraPy.dll. – Nadavz Apr 15 '19 at 10:34
  • you can use [procmon](https://learn.microsoft.com/en-us/sysinternals/downloads/procmon) to monitor what your dll is trying to do and if it gets errors, you can also use [dependency walker](http://www.dependencywalker.com/) to analyze the dll statically and see what it will want to load. i use these two tools for this exact issue – Nullman Apr 15 '19 at 10:37
  • 1
    Your traceback says you are running 32-bit Python. Are you very sure the DLLs you are trying to load are of the correct bitness? A 32-bit process cannot load a 64-bit DLL (and vice versa), and if the bitness is mismatched you will very often get the message *the specified module could not be found* even when you can see it in the folder. – BoarGules Apr 15 '19 at 10:58
  • I did suspect that it has something to do with the architecture. My computer is 64-bit, so do you suggest me to download the 64 bit python? – Nadavz Apr 15 '19 at 11:40
  • is your dll 64bit? if yes, then you will either need to recompile it to 32bit or get 64bit python. if you arent sure and have VS installed try [this](https://stackoverflow.com/a/51278133/7540911) – Nullman Apr 15 '19 at 13:16

1 Answers1

0

You can't load the library itself, you must find the library in the dll.

import ctypes import *
from ctypes.util import *
dll = find_library("D:\\full\\path\\BlueTeraPy.dll")
lib = cdll.LoadLibrary(dll)
mazunki
  • 682
  • 5
  • 17