在Python中加载一个DLL

时间:2016-01-01 21:34:53

标签: python dll visual-studio-2015

我正在使用Windows 10和Visual Studio 2015与Python 3.4.4(来自Python软件基金会)

我想在我编写的DLL中调用一些函数。我已尝试在此论坛上找到的几种方法,但会出现一种错误或另一种错误。我从例子开始工作,但可能误解了一些事情。我已设置我的路径以包含包含DLL的文件夹。有谁知道出了什么问题?

以下是最近一次尝试的输出(我的DLL位于路径中的最后一个文件夹中):

Python 3.4 interactive window [PTVS 2.2.31124.00-14.0]
Type $help for a list of commands.
>>> from ctypes import *
>>> from builtins import print
>>> import os                       # needed for os.system()
>>> import ctypes
>>> from ctypes.util import find_library
>>> os.system("path")
0PATH=C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\NativeBinaries\x86;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;w:\eddy2;C:\Program Files (x86)\Common Files\Seagate\SnapAPI\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\SlikSvn\bin;C:\Program Files (x86)\Common Files\Acronis\SnapAPI\;C:\Program Files (x86)\Skype\Phone\;C:\Program Files\Microsoft\Web Platform Installer\;C:\Program Files\Git\cmd;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Program Files (x86)\Windows Live\Shared;C:\Users\eddyq\.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;W:\Dropbox\DSI (His)\Windows Apps\Debug

>>> os.system("where DsiLibrary_dll.dll")
W:\Dropbox\DSI (His)\Windows Apps\Debug\DsiLibrary_dll.dll
0
>>> print (find_library('DsiLibrary_dll'))          #this does print the location of the DLL
W:\Dropbox\DSI (His)\Windows Apps\Debug\DsiLibrary_dll.dll
>>> nidaq_cdecl   = ctypes.CDLL('DsiLibrary_dll')   #this gives an error "%1 is not a valid Win32 application"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python34\lib\ctypes\__init__.py", line 351, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application
>>> nidaq  = ctypes.cdll.LoadLibrary(find_library('DsiLibrary_dll'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python34\lib\ctypes\__init__.py", line 429, in LoadLibrary
    return self._dlltype(name)
  File "C:\Python34\lib\ctypes\__init__.py", line 351, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application
>>>

1 个答案:

答案 0 :(得分:1)

现在可以使用,我显然是将32位库与64位Python混合使用。

nidaq = ctypes.cdll.LoadLibrary(find_library('DsiLibrary_dll'))

感谢大家的帮助。