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 相关文章推荐
Windows和Linux下使用Python访问SqlServer的方法介绍
Mar 10 Python
在Python中编写数据库模块的教程
Apr 29 Python
python实现数独算法实例
Jun 09 Python
详解Python字符串对象的实现
Dec 24 Python
浅谈插入排序算法在Python程序中的实现及简单改进
May 04 Python
使用Django启动命令行及执行脚本的方法
May 29 Python
python 实现将txt文件多行合并为一行并将中间的空格去掉方法
Dec 20 Python
python实现串口自动触发工作的示例
Jul 02 Python
基于Numba提高python运行效率过程解析
Mar 02 Python
python GUI库图形界面开发之PyQt5信号与槽事件处理机制详细介绍与实例解析
Mar 08 Python
Python实现Keras搭建神经网络训练分类模型教程
Jun 12 Python
python 利用panda 实现列联表(交叉表)
Feb 06 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
提取HTML标签
2006/10/09 PHP
jq的get传参数在utf-8中乱码问题的解决php版
2008/07/23 PHP
php设计模式 Template (模板模式)
2011/06/26 PHP
php array_intersect比array_diff快(附详细的使用说明)
2011/07/03 PHP
destoon后台网站设置变成空白的解决方法
2014/06/21 PHP
php源码分析之DZX1.5随机数函数random用法
2015/06/17 PHP
学习php设计模式 php实现门面模式(Facade)
2015/12/07 PHP
实现WordPress主题侧边栏切换功能的PHP脚本详解
2015/12/14 PHP
Apache无法自动跳转却显示目录的解决方法
2020/11/30 PHP
Ubuntu VPS中wordpress网站打开时提示”建立数据库连接错误”的解决办法
2016/11/03 PHP
php+redis消息队列实现抢购功能
2018/02/08 PHP
人人网javascript面试题 可以提前实现下
2012/01/05 Javascript
JavaScript判断密码强度(自写代码)
2013/09/06 Javascript
JS实现遮罩层效果的简单实例
2013/11/12 Javascript
js捕获鼠标滚轮事件代码
2013/12/16 Javascript
JavaScript使用Max函数返回两个数字中较大数的方法
2015/04/06 Javascript
js实现类似jquery里animate动画效果的方法
2015/04/10 Javascript
基于jQuery实现的仿百度首页滑动选项卡效果代码
2015/11/16 Javascript
浅谈React + Webpack 构建打包优化
2018/01/23 Javascript
JS通过位运算实现权限加解密
2018/08/14 Javascript
Vue路由history模式解决404问题的几种方法
2018/09/29 Javascript
Koa 中的错误处理解析
2019/04/09 Javascript
vue 中 beforeRouteEnter 死循环的问题
2019/04/23 Javascript
python如何实现远程控制电脑(结合微信)
2015/12/21 Python
关于python的bottle框架跨域请求报错问题的处理方法
2017/03/19 Python
如何更优雅地写python代码
2019/07/02 Python
用Python解数独的方法示例
2019/10/24 Python
如何基于Python批量下载音乐
2019/11/11 Python
英国领先的珍珠首饰品牌:Orchira
2016/09/11 全球购物
英国假发网站:Hothair
2018/02/23 全球购物
CK澳大利亚官网:Calvin Klein澳大利亚
2020/12/12 全球购物
实习老师个人总结的自我评价
2013/09/28 职场文书
简单的项目建议书模板
2014/03/12 职场文书
党支部反对四风思想汇报
2014/10/10 职场文书
检讨书范文大全
2015/05/07 职场文书
欠条样本
2015/07/03 职场文书