Python 调用VC++的动态链接库(DLL)

yipeiwu_com6年前Python基础
1. 首先VC++的DLL的导出函数定义成标准C的导出函数:
复制代码 代码如下:

#ifdef LRDLLTEST_EXPORTS
#define LRDLLTEST_API __declspec(dllexport)
#else
#define LRDLLTEST_API __declspec(dllimport)
#endif

extern "C" LRDLLTEST_API int Sum(int a , int b);
extern "C" LRDLLTEST_API void GetString(char* pChar);

//a + b
LRDLLTEST_API int Sum(int a , int b)
{
return a + b;
}

//Get a string
LRDLLTEST_API void GetString(char* pChar)
{
strcpy(pChar, "Hello DLL");
}


2. Python中调用如下:
复制代码 代码如下:

from ctypes import *

fileName="LRDllTest.dll"
func=cdll.LoadLibrary(fileName)
str = create_string_buffer(20)
n = func.Sum(2, 3)
func.GetString(str)

print n
print str.raw

关于C语言中的一些参数类型详见:http://www.python.org/doc/2.5/lib/node454.html

3. 输出结果:
5
Hello DLL

相关文章

NumPy排序的实现

numpy.sort()函数 该函数提供了多种排序功能,支持归并排序,堆排序,快速排序等多种排序算法 使用numpy.sort()方法的格式为: numpy.sort(a,axis,k...

python 中的列表解析和生成表达式

列表解析 在需要改变列表而不是需要新建某列表时,可以使用列表解析。列表解析表达式为: [expr for iter_var in iterable] [expr for iter_var...

python+unittest+requests实现接口自动化的方法

python+unittest+requests实现接口自动化的方法

前言: Requests简介 Requests 是使用Apache2 Licensed 许可证的 HTTP 库。用 Python 编写,真正的为人类着想。 Python 标准库中的 ur...

浅谈python3.6的tkinter运行问题

python3.6在运行tkinter时要选择 run as Python unit-test, 否则报错 ModuleNotFoundError: No module named 't...

python对绑定事件的鼠标、按键的判断实例

当多个事件绑定了同一个命令,那么在命令内部根据不同的事件进行处理的时候,怎么确定哪个事件发生了呢,用下面的来检测,经过测试处理tab键和alt键不能识别,其他单个都能被识别。 还有个事件...