如何使用Python中的DLL文件? [英] How can I use a DLL file from Python?

查看:219
本文介绍了如何使用Python中的DLL文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中使用DLL文件最简单的方法是什么?



具体来说,如果没有使用包装C ++代码将功能公开给Python?



本地Python功能比使用第三方库更为重要。

解决方案

为便于使用, ctypes 是要走的路。



ctypes的以下示例来自我写的实际代码(在Python 2.5中)。这是迄今为止我找到的最简单的方法。

  import ctypes 

#将DLL加载到内存中。

hllDll = ctypes.WinDLL(c:\\PComm\\ehlapi32.dll)

#设置所需函数调用的原型和参数。
#HLLAPI

hllApiProto = ctypes.WINFUNCTYPE(
ctypes.c_int,#返回类型。
ctypes.c_void_p,#参数1 ...
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_void_p)#...通过4.
hllApiParams =(1,p1,0),(1,p2 0),(1,p3,0),(1,p4,0),

#实际将调用(HLLAPI(...)映射到Python名称。

hllApi = hllApiProto((HLLAPI,hllDll),hllApiParams)

#这是如何实际调用DLL函数的。
#设置变量并调用Python名称。

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是一个相当一致的界面。



所有调用都传递四个void指针(EHLLAPI发送返回码通过第四个参数,指向 int 的指针,而我指定 int 作为返回类型,我可以安全地忽略它),根据IBM的文档这里。换句话说,函数的C变量是:

  int hllApi(void * p1,void * p2,void * p3,void * p4)

这使得一个简单的 ctypes 函数能够做任何EHLLAPI库提供的功能,但很可能其他库将需要一个单独的 ctypes 函数设置每个库函数。 >

来自 WINFUNCTYPE 的返回值是一个函数原型,但您仍然必须设置更多的参数信息(超过类型)。 hllApiParams 中的每个元组都有参数direction(1 = input,2 = output等),参数名称和默认值 - 请参阅 ctypes doco详细信息



一旦你有原型和参数信息,你可以创建一个Python可调用 hllApi 用于调用该函数。您只需在我的情况下,通过 p4 创建所需的变量( p1 ),然后调用该函数。


What is the easiest way to use a DLL file from within Python?

Specifically, how can this be done without writing any additional wrapper C++ code to expose the functionality to Python?

Native Python functionality is strongly preferred over using a third-party library.

解决方案

For ease of use, ctypes is the way to go.

The following example of ctypes is from actual code I've written (in Python 2.5). This has been, by far, the easiest way I've found for doing what you ask.

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))

The ctypes stuff has all the C-type data types (int, char, short, void*, and so on) and can pass by value or reference. It can also return specific data types although my example doesn't do that (the HLL API returns values by modifying a variable passed by reference).


In terms of the specific example shown above, IBM's EHLLAPI is a fairly consistent interface.

All calls pass four void pointers (EHLLAPI sends the return code back through the fourth parameter, a pointer to an int so, while I specify int as the return type, I can safely ignore it) as per IBM's documentation here. In other words, the C variant of the function would be:

int hllApi (void *p1, void *p2, void *p3, void *p4)

This makes for a single, simple ctypes function able to do anything the EHLLAPI library provides, but it's likely that other libraries will need a separate ctypes function set up per library function.

The return value from WINFUNCTYPE is a function prototype but you still have to set up more parameter information (over and above the types). Each tuple in hllApiParams has a parameter "direction" (1 = input, 2 = output and so on), a parameter name and a default value - see the ctypes doco for details

Once you have the prototype and parameter information, you can create a Python "callable" hllApi with which to call the function. You simply create the needed variable (p1 through p4 in my case) and call the function with them.

这篇关于如何使用Python中的DLL文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆