3 回答

TA贡献1865条经验 获得超7个赞
为了便于使用,ctypes是要走的路。
以下ctypes示例来自我编写的实际代码(在Python 2.5中)。到目前为止,这是我找到你所要求的最简单方法。
import ctypes
# Load DLL into memory.
hllDll = ctypes.WinDLL ("c:\\PComm\\ehlapi32.dll")
# Set up prototype and parameters for the desired function call.
# HLLAPI
hllApiProto = ctypes.WINFUNCTYPE (
ctypes.c_int, # Return type.
ctypes.c_void_p, # Parameters 1 ...
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_void_p) # ... thru 4.
hllApiParams = (1, "p1", 0), (1, "p2", 0), (1, "p3",0), (1, "p4",0),
# Actually map the call ("HLLAPI(...)") to a Python name.
hllApi = hllApiProto (("HLLAPI", hllDll), hllApiParams)
# This is how you can actually call the DLL function.
# Set up the variables and call the Python name with them.
p1 = ctypes.c_int (1)
p2 = ctypes.c_char_p (sessionVar)
p3 = ctypes.c_int (1)
p4 = ctypes.c_int (0)
hllApi (ctypes.byref (p1), p2, ctypes.byref (p3), ctypes.byref (p4))
的ctypes东西拥有所有的C型的数据类型(int,char,short,void*,等等),并且可以通过数值或引用传递。它也可以返回特定的数据类型,尽管我的例子没有这样做(HLL API通过修改引用传递的变量来返回值)。
就上面显示的具体示例而言,IBM的EHLLAPI是一个相当一致的接口。
所有呼叫传递四个空指针(EHLLAPI发送返回代码回通过第四个参数,一个指向int如此,而我指定int的返回类型,我可以放心地忽略它),按照IBM的文档在这里。换句话说,函数的C变体将是:
int hllApi (void *p1, void *p2, void *p3, void *p4)
这使得单个简单的ctypes函数能够执行EHLLAPI库提供的任何操作,但是其他库可能需要为ctypes每个库函数设置单独的函数。
返回值来自WINFUNCTYPE函数原型,但您仍需要设置更多参数信息(超出类型)。每个元组hllApiParams都有一个参数“direction”(1 =输入,2 =输出等),参数名称和默认值 - ctypes有关详细信息,请参阅doco
获得原型和参数信息后,您可以创建一个Python“callable” hllApi来调用该函数。您只需创建(所需的变量p1通过p4在我的情况),并调用函数与他们。

TA贡献1824条经验 获得超8个赞
这个页面有一个从DLL文件调用函数的非常简单的例子。
在此处解释详细信息以获得完整性:
在Python中调用DLL函数非常容易。我有两个功能,一个自制的DLL文件:
add
和sub
这两个参数。
add(a, b)
返回两个数字的加法sub(a, b)
返回两个数字的减法DLL文件的名称将是“demo.dll”
程序:
from ctypes import*
# give location of dll
mydll = cdll.LoadLibrary("C:\\demo.dll")
result1= mydll.add(10,1)
result2= mydll.sub(10,1)
print "Addition value:"+result1
print "Substraction:"+result2
输出:
Addition value:11
Substraction:9

TA贡献1853条经验 获得超9个赞
也许与Dispatch
:
from win32com.client import Dispatchzk = Dispatch("zkemkeeper.ZKEM")
其中zkemkeeper是系统上注册的DLL文件...之后,您可以通过调用它们来访问函数:
zk.Connect_Net(IP_address, port)
添加回答
举报