2017-05-14 84 views
0

我想從Python加載.dll。我在Visual Studio 2017 Preview中使用Python 3.0.17114.1。我收到一個錯誤,說「NameError:名稱LoadLibrary未定義」。Python 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很新,所以我可能犯了一些愚蠢的錯誤。有人可以提出建議嗎?

回答

1

您可以訪問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 

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 ...> 
>>> 
+0

我用了你的Windows方法,但給出了同樣的沒有定義的錯誤。 – Eddy

+0

對我來說沒有錯誤,你做了什麼? – abccd

+0

我從你的帖子中做了剪貼。從ctypes的進口ctypes的 cdll 導入從ctypes.util進口find_library theDll = find_library( 'DsiLibrary_dll') dsi_lib = cdll.LoadLibrary(theDll) – Eddy