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代理抓取并验证使用多线程实现
May 03 Python
python解析发往本机的数据包示例 (解析数据包)
Jan 16 Python
Python内建数据结构详解
Feb 03 Python
Python利用itchat对微信中好友数据实现简单分析的方法
Nov 21 Python
Python绘制3d螺旋曲线图实例代码
Dec 20 Python
在双python下设置python3为默认的方法
Oct 31 Python
Python 分发包中添加额外文件的方法
Aug 16 Python
Python递归及尾递归优化操作实例分析
Feb 01 Python
django admin后管定制-显示字段的实例
Mar 11 Python
Python实现快速大文件比较代码解析
Sep 04 Python
使用pandas实现筛选出指定列值所对应的行
Dec 13 Python
CocosCreator ScrollView优化系列之分帧加载
Apr 14 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 mssql扩展SQL查询中文字段名解决方法
2012/10/15 PHP
PHP使用缓存即时输出内容(output buffering)的方法
2015/08/03 PHP
JavaScript更改class和id的方法
2008/10/10 Javascript
JavaScript学习笔记(二) js对象
2011/10/25 Javascript
使用CSS和jQuery模拟select并附提交后取得数据的代码
2013/10/18 Javascript
JavaScript instanceof 的使用方法示例介绍
2013/10/23 Javascript
jquery如何扑捉回车键触发的事件
2014/04/24 Javascript
ExtJS4如何给同一个formpanel不同的url
2014/05/02 Javascript
一个奇葩的最短的 IE 版本判断JS脚本
2014/05/28 Javascript
JS简单操作select和dropdownlist实例
2014/11/26 Javascript
DOM基础教程之模型中的模型节点
2015/01/19 Javascript
html的DOM中document对象anchors集合用法实例
2015/01/21 Javascript
JavaScript学习小结(7)之JS RegExp
2015/11/29 Javascript
js时间戳转为日期格式的方法
2015/12/28 Javascript
各式各样的导航条效果css3结合jquery代码实现
2016/09/17 Javascript
js利用clipboardData实现截屏粘贴功能
2016/10/12 Javascript
nodejs实现发出蜂鸣声音(系统报警声)的方法
2017/01/18 NodeJs
基于JavaScript实现自定义滚动条
2017/01/25 Javascript
angular bootstrap timepicker TypeError提示怎么办
2017/06/13 Javascript
vue实现点击隐藏与显示实例分享
2019/02/13 Javascript
ES6入门教程之Array.from()方法
2019/03/23 Javascript
Python多线程爬虫实战_爬取糗事百科段子的实例
2017/12/15 Python
在Pandas中给多层索引降级的方法
2018/11/16 Python
PyQt5 对图片进行缩放的实例
2019/06/18 Python
python 实现多线程下载m3u8格式视频并使用fmmpeg合并
2019/11/15 Python
关于python中plt.hist参数的使用详解
2019/11/28 Python
python 实现目录复制的三种小结
2019/12/04 Python
15行Python代码实现免费发送手机短信推送消息功能
2020/02/27 Python
Python接口开发实现步骤详解
2020/04/26 Python
Keras 中Leaky ReLU等高级激活函数的用法
2020/07/05 Python
利用python爬取有道词典的方法
2020/12/08 Python
《要下雨了》教学反思
2014/02/17 职场文书
工程售后服务方案
2014/06/08 职场文书
感恩信:写给爸爸妈妈的一封感谢信
2019/09/12 职场文书
500字作文之关于爸爸
2019/11/14 职场文书
python中validators库的使用方法详解
2022/09/23 Python