Python调用C/C++动态链接库的方法详解


Posted in Python onJuly 22, 2014

本文以实例讲解了Python调用C/C++ DLL动态链接库的方法,具体示例如下:

示例一:

首先,在创建一个DLL工程(本例创建环境为VS 2005),头文件:

//hello.h
#ifdef EXPORT_HELLO_DLL
#define HELLO_API __declspec(dllexport)
#else
#define HELLO_API __declspec(dllimport)
#endif
extern "C"
{
 HELLO_API int IntAdd(int , int);
}

CPP文件:

//hello.cpp
#define EXPORT_HELLO_DLL
#include "hello.h"
HELLO_API int IntAdd(int a, int b)
{
 return a + b;
}

这里有两个注意点:

(1)弄清楚编译的时候函数的调用约定采用的__cdecl还是__stdcall,因为根据DLL中函数调用约定方式,Python将使用相应的函数加载DLL。

(2)如果采用C++的工程,那么导出的接口需要extern "C",这样python中才能识别导出的函数。

我的工程中采用__cdecl函数调用约定方式进行编译链接产生hello.dll,然后Python中采用ctypes库对hello.dll进行加载和函数调用:

from ctypes import *
dll = cdll.LoadLibrary('hello.dll');
ret = dll.IntAdd(2, 4);
print ret;

至此,第一个小例子已经完成了,读者可以自己动手尝试一下运行效果。

示例二:

示例一只是一个"hello world"级别的程序,实际运用中更多的需要传递数据结构、字符串等,才能满足我们的需求。那么本示例将展示,如何传递数据结构参数,以及如何通过数据结构获取返回值。

首先编写DLL工程中的头文件:

//hello.h
#ifdef EXPORT_HELLO_DLL
#define HELLO_API __declspec(dllexport)
#else
#define HELLO_API __declspec(dllimport)
#endif

#define ARRAY_NUMBER 20
#define STR_LEN 20

struct StructTest
{
 int number;
 char* pChar;
 char str[STR_LEN];
 int iArray[ARRAY_NUMBER];
};

extern "C"
{
 //HELLO_API int IntAdd(int , int);
 HELLO_API char* GetStructInfo(struct StructTest* pStruct);
}

CPP文件如下:

//hello.cpp
#include <string.h>
#define EXPORT_HELLO_DLL
#include "hello.h"

HELLO_API char* GetStructInfo(struct StructTest* pStruct)
{
 for (int i = 0; i < ARRAY_NUMBER; i++)
 pStruct->iArray[i] = i;
 pStruct->pChar = "hello python!";
 strcpy (pStruct->str, "hello world!");
 pStruct->number = 100;
 return "just OK";
}

GetStructInfo这个函数通过传递一个StructTest类型的指针,然后对对象中的属性进行赋值,最后返回"just OK".

编写Python调用代码如下,首先在Python中继承Structure构造一个和C DLL中一致的数据结构StructTest,然后设置函数GetStructInfo的参数类型和返回值类型,最后创建一个StructTest对象,并将其转化为指针作为参数,调用函数GetStrcutInfo,最后通过输出数据结构的值来检查是否调用成功

from ctypes import *
ARRAY_NUMBER = 20;
STR_LEN = 20;
#define type
INTARRAY20 = c_int * ARRAY_NUMBER;
CHARARRAY20 = c_char * STR_LEN;
#define struct
class StructTest(Structure):
  _fields_ = [
    ("number", c_int),
    ("pChar", c_char_p),
    ("str", CHARARRAY20),
    ("iArray", INTARRAY20)
        ]
#load dll and get the function object
dll = cdll.LoadLibrary('hello.dll');
GetStructInfo = dll.GetStructInfo;
#set the return type
GetStructInfo.restype = c_char_p;
#set the argtypes
GetStructInfo.argtypes = [POINTER(StructTest)];
objectStruct = StructTest();
#invoke api GetStructInfo
retStr = GetStructInfo(byref(objectStruct));
#check result
print "number: ", objectStruct.number;
print "pChar: ", objectStruct.pChar;
print "str: ", objectStruct.str;
for i,val in enumerate(objectStruct.iArray):
  print 'Array[i]: ', val;
print retStr;

总结:

1. 用64位的Python去加载32位的DLL会出错
2. 以上只是些测试程序,在编写Python过程中尽可能的使用"try Except"来处理异常
3. 注意在Python与C DLL交互的时候字节对齐问题
4. ctypes库的功能还有待继续探索

Python 相关文章推荐
Python 网页解析HTMLParse的实例详解
Aug 10 Python
Jupyter安装nbextensions,启动提示没有nbextensions库
Apr 23 Python
详解Django之auth模块(用户认证)
Apr 17 Python
Django实战之用户认证(初始配置)
Jul 16 Python
pandas 条件搜索返回列表的方法
Oct 30 Python
对Python random模块打乱数组顺序的实例讲解
Nov 08 Python
python 函数中的内置函数及用法详解
Jul 02 Python
基于python的itchat库实现微信聊天机器人(推荐)
Oct 29 Python
Python3 A*寻路算法实现方式
Dec 24 Python
爬虫代理的cookie如何生成运行
Sep 22 Python
Python爬虫爬取全球疫情数据并存储到mysql数据库的步骤
Mar 29 Python
Python编解码问题及文本文件处理方法详解
Jun 20 Python
使用python编写批量卸载手机中安装的android应用脚本
Jul 21 #Python
使用python编写脚本获取手机当前应用apk的信息
Jul 21 #Python
使用python编写android截屏脚本双击运行即可
Jul 21 #Python
python 示例分享---逻辑推理编程解决八皇后
Jul 20 #Python
python中from module import * 的一个坑
Jul 20 #Python
用python代码做configure文件
Jul 20 #Python
python中的内置函数getattr()介绍及示例
Jul 20 #Python
You might like
浅谈PHP中Stream(流)
2015/06/08 PHP
php获取远程文件的内容和大小
2015/11/03 PHP
[原创]CI(CodeIgniter)简单统计访问人数实现方法
2016/01/19 PHP
php查询及多条件查询
2017/02/26 PHP
PHP利用二叉堆实现TopK-算法的方法详解
2017/04/24 PHP
解析javascript 实用函数的使用详解
2013/05/10 Javascript
使用jquery.upload.js实现异步上传示例代码
2014/07/29 Javascript
javascript中利用柯里化函数实现bind方法【推荐】
2016/04/29 Javascript
jQuery代码实现表格中点击相应行变色功能
2016/05/09 Javascript
JS DOMReady事件的六种实现方法总结
2016/11/23 Javascript
bootstrap精简教程_动力节点Java学院整理
2017/07/14 Javascript
laydate日历控件使用方法详解
2017/11/20 Javascript
vue将毫秒数转化为正常日期格式的实例
2018/09/16 Javascript
在js代码拼接dom对象到页面上的模板总结
2018/10/21 Javascript
深入剖析JavaScript instanceof 运算符
2019/06/14 Javascript
layui下拉列表select实现可输入查找的方法
2019/09/28 Javascript
js get和post请求实现代码解析
2020/02/06 Javascript
JavaScript实现打砖块游戏
2020/02/25 Javascript
uniapp微信小程序实现一个页面多个倒计时
2020/11/01 Javascript
使用Python编写一个最基础的代码解释器的要点解析
2016/07/12 Python
基于python3 类的属性、方法、封装、继承实例讲解
2017/09/19 Python
对python list 遍历删除的正确方法详解
2018/06/29 Python
将python2.7添加进64位系统的注册表方式
2019/11/20 Python
基于Python检测动态物体颜色过程解析
2019/12/04 Python
Python使用gluon/mxnet模块实现的mnist手写数字识别功能完整示例
2019/12/18 Python
python3.6.5基于kerberos认证的hive和hdfs连接调用方式
2020/06/06 Python
Python图像识别+KNN求解数独的实现
2020/11/13 Python
使用python对excel表格处理的一些小功能
2021/01/25 Python
CSS3的常见transformation图形变化用法小结
2016/05/13 HTML / CSS
如何在Canvas上的图形/图像绑定事件监听的实现
2020/09/16 HTML / CSS
什么是网络协议
2016/04/07 面试题
霸王洗发水广告词
2014/03/14 职场文书
政府采购方案
2014/06/12 职场文书
销售活动策划方案
2014/08/26 职场文书
Python数据分析之绘图和可视化详解
2021/06/02 Python
python中urllib包的网络请求教程
2022/04/19 Python