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 相关文章推荐
非递归的输出1-N的全排列实例(推荐)
Apr 11 Python
pandas多级分组实现排序的方法
Apr 20 Python
pycharm打开命令行或Terminal的方法
Jan 16 Python
详解Python循环作用域与闭包
Mar 21 Python
详解python中的index函数用法
Aug 06 Python
numpy.random.shuffle打乱顺序函数的实现
Sep 10 Python
python计算二维矩形IOU实例
Jan 18 Python
新建文件时Pycharm中自动设置头部模板信息的方法
Apr 17 Python
Python pandas 列转行操作详解(类似hive中explode方法)
May 18 Python
Python爬虫获取页面所有URL链接过程详解
Jun 04 Python
python和opencv构建运动检测器的实现
Mar 03 Python
Python turtle实现贪吃蛇游戏
Jun 18 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 curl 登录163邮箱并抓取邮箱好友列表的代码(经测试)
2011/04/07 PHP
php跨服务器访问方法小结
2015/05/12 PHP
PHP实现二叉树的深度优先与广度优先遍历方法
2015/09/28 PHP
详解php几行代码实现CSV格式文件输出
2017/07/01 PHP
Laravel事件监听器用法实例分析
2019/03/12 PHP
javascript:void(0)的真正含义实例分析
2008/08/20 Javascript
cookie丢失问题(认证失效) Authentication (用户验证信息)也会丢失
2009/06/04 Javascript
uploadify 3.0 详细使用说明
2012/06/18 Javascript
Jquery的hide及toggle方法让超链接慢慢消失
2013/09/06 Javascript
jquery对元素拖动排序示例
2014/01/16 Javascript
禁用Tab键JS代码兼容Firefox和IE
2014/04/18 Javascript
IE浏览器IFrame对象内存不释放问题解决方法
2014/08/22 Javascript
Vue.js实现一个SPA登录页面的过程【推荐】
2017/04/29 Javascript
详解webpack解惑:require的五种用法
2017/06/09 Javascript
jquery实现用户登陆界面(示例讲解)
2017/09/06 jQuery
JavaScript怎样在删除前添加确认弹出框?
2019/05/27 Javascript
Layui多选只有最后一个值的解决方法
2019/09/02 Javascript
vue+element获取el-table某行的下标,根据下标操作数组对象方式
2020/08/07 Javascript
python异步任务队列示例
2014/04/01 Python
Python处理JSON数据并生成条形图
2016/08/05 Python
Python实现判断并移除列表指定位置元素的方法
2018/04/13 Python
python+pyqt5实现KFC点餐收银系统
2019/01/24 Python
python 实现兔子生兔子示例
2019/11/21 Python
Python urlopen()和urlretrieve()用法解析
2020/01/07 Python
opencv python在视屏上截图功能的实现
2020/03/05 Python
详解Html5页面实现下载文件(apk、txt等)的三种方式
2018/10/22 HTML / CSS
马耳他航空公司官方网站:Air Malta
2019/05/15 全球购物
服装店营销方案
2014/03/10 职场文书
小学学校评估方案
2014/06/08 职场文书
预备党员考察意见范文
2015/06/01 职场文书
2015双创工作总结
2015/07/24 职场文书
健康教育主题班会
2015/08/14 职场文书
python迷宫问题深度优先遍历实例
2021/06/20 Python
Java日常练习题,每天进步一点点(38)
2021/07/26 Java/Android
PostgreSQL基于pgrouting的路径规划处理方法
2022/04/18 PostgreSQL
python实现双链表
2022/05/25 Python