我正在尝试从Python加载一个.dll。我在VisualStudio2017预览中使用Python3.0.17114.1。我收到一个错误,说"NameError: name LoadLibrary是未定义的“。
下面是一个代码片段(请注意,theDll是完美的):
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,有几种不同的方法。我尽我所能:
cdll.LoadLibrary(theDll)
CDLL.LoadLibrary(theDll)
ctypes.CDLL.LoadLibrary(theDll)
ctypes.LoadLibrary(theDll)我是Python的新手,所以我可能犯了一些愚蠢的错误。谁能提个建议吗?
发布于 2017-05-14 20:06:42
您可以这样访问LoadLibrary:
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在Linux上,需要指定文件名(包括加载库的扩展名),因此不能使用属性访问来加载库。应该使用dll加载器的LoadLibrary()方法,或者通过调用构造函数:来创建CDLL实例来加载库。从ctype导入* >>> cdll.LoadLibrary("libc.so.6") >>> libc = CDLL("libc.so.6") >>> libc >>>
发布于 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)
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)仍然无法工作。
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)
All of these version were still available from Microsoft on March 16, 2022.https://stackoverflow.com/questions/43968210
复制相似问题