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

具体来说,如何在不编写任何额外的包装器c++代码将功能公开给Python的情况下实现这一点呢?

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


当前回答

构建一个DLL并使用ctypes在Python下链接它

我给出了一个完整的示例,说明如何通过ctypes在Python下构建共享库并使用它。我考虑Windows的情况并处理dll。需要两个步骤:

使用Visual Studio的编译器从命令行或IDE构建DLL; 使用ctypes链接Python下的DLL。

共享库

我考虑的共享库如下所示,包含在testDLL.cpp文件中。唯一的函数testDLL只接收一个int并输出它。

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

从命令行构建DLL

使用Visual Studio从命令行运行构建DLL

"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。

从IDE构建DLL

或者,DLL可以使用Visual Studio构建,如下所示:

文件->新建->项目; 已安装->模板-> Visual c++ -> Windows -> Win32 -> Win32Project; 下一个; 应用程序类型-> DLL; 附加选项->空项目(选择); 附加选项->预编译头(取消选择); 项目->属性->配置管理器->主动解决方案平台:x64; 项目—>属性—>配置管理器—>主用解决方案配置:发布。

在Python下链接DLL

在Python下,执行以下操作

import os
import sys
from ctypes import *

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

lib.testDLL(3)

其他回答

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

为了完整起见,在这里解释一下细节:

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

ctypes将是最容易使用的东西,但(错误地)使用它会使Python崩溃。如果你试图快速做某事,而且你很小心,这很好。

我鼓励你去看看Boost Python。是的,它要求你写一些c++代码,并有一个c++编译器,但你实际上不需要学习c++来使用它,你可以从微软得到一个免费的(就像啤酒一样)c++编译器。

构建一个DLL并使用ctypes在Python下链接它

我给出了一个完整的示例,说明如何通过ctypes在Python下构建共享库并使用它。我考虑Windows的情况并处理dll。需要两个步骤:

使用Visual Studio的编译器从命令行或IDE构建DLL; 使用ctypes链接Python下的DLL。

共享库

我考虑的共享库如下所示,包含在testDLL.cpp文件中。唯一的函数testDLL只接收一个int并输出它。

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

从命令行构建DLL

使用Visual Studio从命令行运行构建DLL

"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。

从IDE构建DLL

或者,DLL可以使用Visual Studio构建,如下所示:

文件->新建->项目; 已安装->模板-> Visual c++ -> Windows -> Win32 -> Win32Project; 下一个; 应用程序类型-> DLL; 附加选项->空项目(选择); 附加选项->预编译头(取消选择); 项目->属性->配置管理器->主动解决方案平台:x64; 项目—>属性—>配置管理器—>主用解决方案配置:发布。

在Python下链接DLL

在Python下,执行以下操作

import os
import sys
from ctypes import *

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

lib.testDLL(3)

c类型可以用来访问dll,这里有一个教程:

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

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

pip install pythonnet

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

import clr
clr.AddReference('path_to_your_dll')

# import the namespace and class

from Namespace import Class

# create an object of the class

obj = Class()

# access functions return type using object

value = obj.Function(<arguments>)


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