首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找不到Python LoadLibrary

找不到Python LoadLibrary
EN

Stack Overflow用户
提问于 2017-05-14 19:52:14
回答 2查看 3.4K关注 0票数 0

我正在尝试从Python加载一个.dll。我在VisualStudio2017预览中使用Python3.0.17114.1。我收到一个错误,说"NameError: name LoadLibrary是未定义的“。

下面是一个代码片段(请注意,theDll是完美的):

代码语言:javascript
复制
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)

所以我读到了LoadLibrary,有几种不同的方法。我尽我所能:

代码语言:javascript
复制
cdll.LoadLibrary(theDll)
CDLL.LoadLibrary(theDll)
ctypes.CDLL.LoadLibrary(theDll)
ctypes.LoadLibrary(theDll)

我是Python的新手,所以我可能犯了一些愚蠢的错误。谁能提个建议吗?

EN

回答 2

Stack Overflow用户

发布于 2017-05-14 20:06:42

您可以这样访问LoadLibrary

代码语言:javascript
复制
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

C类型文档

在Linux上,需要指定文件名(包括加载库的扩展名),因此不能使用属性访问来加载库。应该使用dll加载器的LoadLibrary()方法,或者通过调用构造函数:来创建CDLL实例来加载库。从ctype导入* >>> cdll.LoadLibrary("libc.so.6") >>> libc = CDLL("libc.so.6") >>> libc >>>

票数 1
EN

Stack Overflow用户

发布于 2022-03-16 16:41:53

在Python3.10.2 Win10 PC上使用Python3.10.2 x64处理同样的问题,花了两天时间。

简单的python代码:

导入os导入sys导入ctype

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

if os.path.exists(“LibFT260.dll”):打印(“找到LibFT260.dll.”)其他:打印(“未能找到LibFT260.dll.”)退出()

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

打印(ft.FT260_GetChipVersion)

代码语言:javascript
复制
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:

机器2 LoadLibrary()工作

联想P53s x64

Win10 21H2 build 19044.1526

Python3.9.0 (x64)

机器1 LoadLibrary()不工作

联想W530 x64

Win10 20H2 build 19042.1586

Python3.10.2 (x64)

已经尝试过的Python3.9.0 (x64)仍然无法工作。

代码语言:javascript
复制
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 C++ 2005可再发行版(MFC安全更新)

2008版(Ver 9.0.30729.6161)

2010年版(Ver 10.0.40219)

2013年版(Ver 12.0.40664)

版2015-2022 (Ver 14.31.31103)

代码语言:javascript
复制
All of these version were still available from Microsoft on March 16, 2022.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43968210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档