莫回无
为了便于使用,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.# HLLAPIhllApiProto = 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在我的情况),并调用函数与他们。