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

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

问题描述

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

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

具体来说,如何编写任何额外的包装C++代码来将功能暴露给Python?

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

本机 Python 功能比使用第三方库更受欢迎.

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

推荐答案

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

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

以下 ctypes 示例来自我编写的实际代码(在 Python 2.5 中).到目前为止,这是我找到的最简单的方法来完成您的要求.

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

ctypes 包含所有 C 类型的数据类型(intcharshortvoid*,等等)并且可以通过值或引用传递.它还可以返回特定的数据类型,尽管我的示例没有这样做(HLL API 通过修改通过引用传递的变量来返回值).

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

就上面显示的具体示例而言,IBM 的 EHLLAPI 是一个相当一致的接口.

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

所有调用都传递四个 void 指针(EHLLAPI 通过第四个参数将返回代码发回,一个指向 int 的指针,因此,而我指定 int 作为返回类型,我可以放心地忽略它)根据 IBM 的文档 此处.换句话说,该函数的 C 变体将是:

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)

这使得单个简单的 ctypes 函数能够执行 EHLLAPI 库提供的任何操作,但其他库可能需要为每个函数设置单独的 ctypes 函数库函数.

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.

WINFUNCTYPE 的返回值是一个函数原型,但你仍然需要设置更多的参数信息(除了类型之外).hllApiParams 中的每个元组都有一个参数方向"(1 = 输入,2 = 输出等)、一个参数名称和一个默认值 - 请参阅 ctypes 文档以了解详情

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

一旦你有了原型和参数信息,你就可以创建一个 Python可调用的"hllApi,用它来调用函数.您只需创建所需的变量(在我的情况下为 p1p4)并使用它们调用函数.

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天全站免登陆