Eddy
Eddy

Reputation: 379

Python LoadLibrary not found

I'm trying to load a .dll from Python. I am using Python 3.0.17114.1 with Visual Studio 2017 Preview. I get an error saying "NameError: name LoadLibrary is not defined".

Here is a code snip (note that theDll comes out perfect):

import ctypes
from ctypes.util import find_library
from ctypes import LibraryLoader
from ctypes.util import find_library    
theDll = find_library('DsiLibrary_dll')
dsi_lib  = LoadLibrary(theDll)

So I read up on LoadLibrary and there are a few different ways to do it. I tried all I could find:

cdll.LoadLibrary(theDll)
CDLL.LoadLibrary(theDll)
ctypes.CDLL.LoadLibrary(theDll)
ctypes.LoadLibrary(theDll)

I'm very new at Python so I may have made some silly mistake. Can someone please make a suggestion?

Upvotes: 0

Views: 4896

Answers (2)

SEGGY
SEGGY

Reputation: 11

Spent 2 days with the same problem on Win10 PC Using Python 3.10.2 x64.
Simple python code:

import os import sys import ctypes

aqui = os.getcwd() print(aqui) os.add_dll_directory(aqui)

if os.path.exists('LibFT260.dll'): print('Found LibFT260.dll.') else: print('Failed to find LibFT260.dll.') quit()

ft = ctypes.windll.LoadLibrary('LibFT260.dll')

print(ft.FT260_GetChipVersion)


Error line:

> ft = ctypes.windll.LoadLibrary('LibFT260.dll') with or w/o '.dll' suffix.
        
I put `LibFT260.dll` in the working directory (where I call the Python script) and the code adds that path to the os.add_dll_directory.  These were the 2
major fixes mentioned on the internet; neither worked.

[FYI:  The only ctypes library load statement that would work was 
ft = ctypes.WinDLL('LibFT260.dll', winmode = 1)
Microsoft LoadLibraryEx docs say not to use winmode = 1, it is only for backwards compatibility, so I changed to winmode = 48 which was suggested as one alternative.
(I never tried None, but 0 always failed, all other non-zero worked.)]

I ran the exact same program above on a 2nd PC and the LoadLibrary() function worked!

Details and the solution:

Machine 2 LoadLibrary() works
Lenovo P53s x64
Win10 21H2 build 19044.1526
Python 3.9.0 (x64)

Machine 1 LoadLibrary() doesn't work
Lenovo W530 x64
Win10 20H2 build 19042.1586
Python 3.10.2 (x64)
tried Python 3.9.0 (x64) still didn't work.

Then I noticed many Visual C++ differences between the machines.  
The P53s had a more Visual C++ versions installed.
When I matched those versions on the W530 the code above worked on Machine 1.

I don't know which versions I had and which I added new, but this list worked:
(I have both the x64 & the x86 versions of all listed below on Machine 1 now.)   

Microsoft Visual Studio C++ Version 2005 Redistributable (MFC Security Update)
Version 2008 (Ver 9.0.30729.6161)
Version 2010 (Ver 10.0.40219)
Version 2013 (Ver 12.0.40664)
Version 2015-2022 (Ver 14.31.31103)

All of these version were still available from Microsoft on March 16, 2022.

 

Upvotes: 1

Taku
Taku

Reputation: 33724

You can access LoadLibrary like this:

import ctypes
from ctypes import cdll 
from ctypes.util import find_library    
theDll = find_library('DsiLibrary_dll')
lib = cdll.LoadLibrary(theDll)
# do stuff with lib

Ctypes documentation:

On Linux, it is required to specify the filename including the extension to load a library, so attribute access can not be used to load libraries. Either the LoadLibrary() method of the dll loaders should be used, or you should load the library by creating an instance of CDLL by calling the constructor:

>>> from ctypes import *
>>> cdll.LoadLibrary("libc.so.6") 
<CDLL 'libc.so.6', handle ... at ...>
>>> libc = CDLL("libc.so.6")       
>>> libc
<CDLL 'libc.so.6', handle ... at ...>
>>>

Upvotes: 1

Related Questions