0

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?

Eddy
  • 379
  • 1
  • 5
  • 16

2 Answers2

1

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 ...>
>>>
Taku
  • 31,927
  • 11
  • 74
  • 85
  • I used your Windows method but that gives the same not defined error. – Eddy May 14 '17 at 20:36
  • I did a cut and paste from your post. import ctypes from ctypes import cdll from ctypes.util import find_library theDll = find_library('DsiLibrary_dll') dsi_lib = cdll.LoadLibrary(theDll) – Eddy May 14 '17 at 21:06
  • I'm not sure what you mean by "see it online". Are you talking about the snip you gave me? Maybe this is a bug in VS 2017 Preview Python. What Python are you using? – Eddy May 14 '17 at 21:54
  • Yea, I see it online but it doesn't work on VS 2017 Preview Python. Maybe a bug. – Eddy May 14 '17 at 21:56
  • Do you name your file ctypes.py? If not, it might be possible there's something wrong with your python... – Taku May 14 '17 at 22:00
  • Yes, my file is named PythonSample.py. But it is LoadLibrary that is not definec. – Eddy May 14 '17 at 22:42
  • Here is a link to a screen shot of my cut/paste of your code: https://www.dropbox.com/s/zu9j2thccxv0zvr/SO_Sample.png?dl=0 – Eddy May 14 '17 at 23:07
  • Your code isn't the same as my code, your code is the same as the code you posted in the question, not mine. Somehow there's a miss-copy/paste – Taku May 14 '17 at 23:08
  • I just did another copy/paste and now I get "OSError: [WinError 193] %1 is not a valid Win32 application". Here is a correction: https://www.dropbox.com/s/zu9j2thccxv0zvr/SO_Sample.png?dl=0 – Eddy May 15 '17 at 01:00
  • i dont think that error is fixable with your current setup... http://stackoverflow.com/a/27910041/6622817 – Taku May 15 '17 at 01:21
  • Ahh ... so you did fix my original problem. This is simply a 32/64 bit mixup. I'll work on that ... thanks. – Eddy May 15 '17 at 12:12
0

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.

 
SEGGY
  • 1
  • 1