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读取网页内容的方法
Jul 30 Python
Python实现字典的key和values的交换
Aug 04 Python
深入解答关于Python的11道基本面试题
Apr 01 Python
Python程序员面试题 你必须提前准备!(答案及解析)
Jan 23 Python
Python实现JSON反序列化类对象的示例
Jan 31 Python
python获取当前目录路径和上级路径的实例
Apr 26 Python
win10下tensorflow和matplotlib安装教程
Sep 19 Python
Python实现计算字符串中出现次数最多的字符示例
Jan 21 Python
Python3中_(下划线)和__(双下划线)的用途和区别
Apr 26 Python
Python3操作Excel文件(读写)的简单实例
Sep 02 Python
Python中*args和**kwargs的区别详解
Sep 17 Python
Python基于xlrd模块处理合并单元格
Jul 28 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
Linux下ZendOptimizer的安装与配置方法
2007/04/12 PHP
PHP容易忘记的知识点分享
2013/04/30 PHP
PHP 循环删除无限分类子节点的实现代码
2013/06/21 PHP
Linux系统下PHP-FPM的安装和配置教程
2015/08/17 PHP
PHP实现递归无限级分类
2015/10/22 PHP
Yii快速入门经典教程
2015/12/28 PHP
php gd等比例缩放压缩图片函数
2016/06/12 PHP
PHP实现向关联数组指定的Key之前插入元素的方法
2017/06/06 PHP
ThinkPHP 3.2.3实现页面静态化功能的方法详解
2017/08/03 PHP
Javascript学习笔记二 之 变量
2010/12/15 Javascript
js正文内容高亮效果的实现方法
2013/06/30 Javascript
Backbone.js的Hello World程序实例
2015/06/19 Javascript
Jquery简单分页实现方法
2015/07/24 Javascript
文件上传,iframe跨域数据提交的实现
2016/11/18 Javascript
vuejs中监听窗口关闭和窗口刷新事件的方法
2018/09/21 Javascript
ES6的Fetch异步请求的实现方法
2018/12/07 Javascript
angularjs请求数据的方法示例
2019/08/06 Javascript
React学习之JSX与react事件实例分析
2020/01/06 Javascript
python实现比较两段文本不同之处的方法
2015/05/30 Python
python实现傅里叶级数展开的实现
2018/07/21 Python
pandas对dataFrame中某一个列的数据进行处理的方法
2019/07/08 Python
Python用字典构建多级菜单功能
2019/07/11 Python
Django model update的多种用法介绍
2020/03/28 Python
在Tensorflow中查看权重的实现
2020/01/24 Python
详解pyqt5的UI中嵌入matplotlib图形并实时刷新(挖坑和填坑)
2020/08/07 Python
python如何导出微信公众号文章方法详解
2020/08/31 Python
俄罗斯外国汽车和国产汽车配件网上商店:Движком
2020/04/19 全球购物
初三政治教学反思
2014/01/30 职场文书
保安队长职务说明书
2014/02/23 职场文书
保证书格式
2015/01/16 职场文书
工作会议通知
2015/04/15 职场文书
2016毕业实习单位评语大全
2015/12/01 职场文书
廉洁自律承诺书2016
2016/03/25 职场文书
浅谈Redis主从复制以及主从复制原理
2021/05/29 Redis
Jpa Specification如何实现and和or同时使用查询
2021/11/23 Java/Android
java版 简单三子棋游戏
2022/05/04 Java/Android