如何使用Python中的DLL文件?

问题:如何使用Python中的DLL文件?

DLL从内部使用文件的最简单方法是什么Python

具体来说,如何在编写任何附加包装器C++代码的情况下完成此功能Python

Python与使用第三方库相比,本机功能是首选。

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.


回答 0

为了易于使用,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型的数据类型intcharshortvoid*,等等),并且可以通过数值或引用传递。尽管我的示例没有这样做,但它也可以返回特定的数据类型(HLL API通过修改引用传递的变量来返回值)。


就上面显示的特定示例而言,IBM的EHLLAPI是一个相当一致的接口。

所有呼叫传递四个空指针(EHLLAPI将通过第四个参数的指针返回代码回来,一个int是这样,而我指定int的返回类型,我可以放心地忽略它),按照IBM的文档在这里。换句话说,该函数的C变体为:

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

这使得一个简单的ctypes函数可以执行EHLLAPI库提供的任何功能,但是其他库可能需要为ctypes每个库函数设置一个单独的函数。

从的返回值WINFUNCTYPE是一个函数原型,但是您仍然必须设置更多的参数信息(类型之外)。每个元组中hllApiParams都有一个参数“方向”(1 =输入,2 =输出,依此类推),一个参数名称和一个默认值- ctypes有关详细信息,请参见doco

一旦有了原型和参数信息,就可以创建一个Python“可调用” hllApi函数来调用该函数。您只需创建(所需的变量p1通过p4在我的情况),并调用函数与他们。

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.


回答 1

该页面有一个非常简单的示例,示例从DLL文件中调用函数。

为了完整起见,在此处解释细节:

在Python中调用DLL函数非常容易。我有一个带有两个函数的自制DLL文件:addsub具有两个参数。

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

This page has a very simple example of calling functions from a DLL file.

Paraphrasing the details here for completeness:

It’s very easy to call a DLL function in Python. I have a self-made DLL file with two functions: add and sub which take two arguments.

add(a, b) returns addition of two numbers
sub(a, b) returns substraction of two numbers

The name of the DLL file will be “demo.dll”

Program:

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

Output:

Addition value:11
Substraction:9


回答 2

ctypes可用于访问dll,这是一个教程:

http://docs.python.org/library/ctypes.html#module-ctypes

ctypes can be used to access dlls, here’s a tutorial:

http://docs.python.org/library/ctypes.html#module-ctypes


回答 3

也许与Dispatch

from win32com.client import Dispatch

zk = Dispatch("zkemkeeper.ZKEM") 

其中zkemkeeper是系统上已注册的DLL文件…之后,您可以通过调用函数来访问它们:

zk.Connect_Net(IP_address, port)

Maybe with Dispatch:

from win32com.client import Dispatch

zk = Dispatch("zkemkeeper.ZKEM") 

Where zkemkeeper is a registered DLL file on the system… After that, you can access functions just by calling them:

zk.Connect_Net(IP_address, port)

回答 4

建立DLL并使用ctypes在Python下连结

我提出关于如何建立一个完全样例shared library,并用它在Python借助于ctypes。我考虑Windows并处理DLLs。需要两个步骤:

  1. 从命令行或从IDE使用Visual Studio的编译器生成DLL。
  2. 使用ctypes在Python下链接DLL。

共享库

shared library我考虑的是以下内容,包含在在testDLL.cpp文件中。唯一的功能testDLL只是接收int并打印它。

#include <stdio.h>

extern "C" {

__declspec(dllexport)

void testDLL(const int i) {
    printf("%d\n", i);
}

} // extern "C"

从命令行构建DLL

要从命令行运行一个DLLwithVisual Studio

"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\vsdevcmd"

设置包含路径,然后运行

cl.exe /D_USRDLL /D_WINDLL testDLL.cpp /MT /link /DLL /OUT:testDLL.dll

生成DLL。

DLL从IDE 构建

或者,DLL可以使用Visual Studio以下方式构建:

  1. 文件->新建->项目;
  2. 已安装->模板-> Visual C ++-> Windows-> Win32-> Win32Project;
  3. 下一个;
  4. 应用程序类型-> DLL;
  5. 附加选项->空项目(选择);
  6. 附加选项->预编译头(取消选择);
  7. 项目->属性->配置管理器->活动解决方案平台:x64;
  8. 项目->属性-> Configuration Manager->活动解决方案配置:发布。

在Python下链接DLL

在Python下,执行以下操作

import os
import sys
from ctypes import *

lib = cdll.LoadLibrary('testDLL.dll')

lib.testDLL(3)

Building a DLL and linking it under Python using ctypes

I present a fully worked example on how building a shared library and using it under Python by means of ctypes. I consider the Windows case and deal with DLLs. Two steps are needed:

  1. Build the DLL using Visual Studio’s compiler either from the command line or from the IDE;
  2. Link the DLL under Python using ctypes.

The shared library

The shared library I consider is the following and is contained in the testDLL.cpp file. The only function testDLL just receives an int and prints it.

#include <stdio.h>
​
extern "C" {
​
__declspec(dllexport)
​
void testDLL(const int i) {
    printf("%d\n", i);
}
​
} // extern "C"

Building the DLL from the command line

To build a DLL with Visual Studio from the command line run

"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\vsdevcmd"

to set the include path and then run

cl.exe /D_USRDLL /D_WINDLL testDLL.cpp /MT /link /DLL /OUT:testDLL.dll

to build the DLL.

Building the DLL from the IDE

Alternatively, the DLL can be build using Visual Studio as follows:

  1. File -> New -> Project;
  2. Installed -> Templates -> Visual C++ -> Windows -> Win32 -> Win32Project;
  3. Next;
  4. Application type -> DLL;
  5. Additional options -> Empty project (select);
  6. Additional options -> Precompiled header (unselect);
  7. Project -> Properties -> Configuration Manager -> Active solution platform: x64;
  8. Project -> Properties -> Configuration Manager -> Active solution configuration: Release.

Linking the DLL under Python

Under Python, do the following

import os
import sys
from ctypes import *

lib = cdll.LoadLibrary('testDLL.dll')

lib.testDLL(3)

回答 5

ctypes将是最容易使用的东西,但是使用不当会使Python崩溃。如果您想快速地做某事,并且小心一点,那就太好了。

我鼓励您查看Boost Python。是的,它要求您编写一些C ++代码并具有C ++编译器,但实际上您无需学习C ++即可使用它,并且可以从Microsoft获得免费的(如啤酒中的)C ++编译器

ctypes will be the easiest thing to use but (mis)using it makes Python subject to crashing. If you are trying to do something quickly, and you are careful, it’s great.

I would encourage you to check out Boost Python. Yes, it requires that you write some C++ code and have a C++ compiler, but you don’t actually need to learn C++ to use it, and you can get a free (as in beer) C++ compiler from Microsoft.


回答 6

如果DLL是COM库类型,则可以使用pythonnet。

pip install pythonnet

然后在您的python代码中,尝试以下操作

import clr
clr.AddReference('path_to_your_dll')

然后根据DLL中的类实例化一个对象,并访问其中的方法。

If the DLL is of type COM library, then you can use pythonnet.

pip install pythonnet

Then in your python code, try the following

import clr
clr.AddReference('path_to_your_dll')

then instantiate an object as per the class in the DLL, and access the methods within it.