python和C/C++混合编程之使用ctypes调用 C/C++的dll


Posted in Python onApril 29, 2022

python和C/C++混合编程,推荐使用python的内置模块ctypes,从名字上可以看出是c,可见对C++的支持并不太好。

一般的步骤:

  • 1、导入ctypes模块,加载C/C++ dll到python进程空间
  • 2、python类型转换为ctypes类型
  • 3、ctypes类型转换为C/C++类型

ctypes文档

VS2017 + Python3.8(IDE:py Charm)

python和C/C++混合编程之使用ctypes调用 C/C++的dll

基本数据类型以及结构体类型都可以正常通信。
DLL:

extern "C"{

struct MyStruct{
int num_int;
long num_long;
float num_float;
double num_double;
char* num_str;
};

int __declspec(dllexport) print(MyStruct my)
printf("%d\n", my.num_int);
printf("%d\n", my.num_long);
printf("%f\n", my.num_float);
printf("%f\n", my.num_double);
printf("%s\n", my.num_str);
}

PYTHON:

import ctypes

class MyStruct(Structure):
_fields_ = [
("num_int", c_int),
("num_long", c_long),
("num_float", c_float),
("num_double", c_double),
("num_str", c_char_p)
]

# dll全路径,依赖完整
dll = ctypes.WinDLL("C:\\work\\mytest.dll")

#调用
my = MyStruct();
my.num_int = 23
my.num_long = 1024
my.num_float = 3.14
my.num_double = 3.141592653
my.num_str = b"hello world"
dll.print(my)

如果结构体嵌套,也是可以成功传输的,但是在项目很大时可能会遇到大结构体通信数据错误,如char*传到C/C++端为无效的字符。
建议,将结构体按照先简单和复杂的顺序排列成员。
参考官方文档为python和C/C++中的结构体定义字节对齐。

如:

<strong>#pragma pack(4)</strong>
struct MyStruct{
int num_int;
long num_long;
float num_float;
double num_double;
char* num_str;
};
class MyStruct(Structure):
<strong>_pack_ </strong><strong>= 4</strong>
_fields_ = [
("num_int", c_int),
("num_long", c_long),
("num_float", c_float),
("num_double", c_double),
("num_str", c_char_p)
]

到此这篇关于python 使用ctypes调用C/C++ dll详情的文章就介绍到这了!


Tags in this post...

Python 相关文章推荐
python3图片转换二进制存入mysql
Dec 06 Python
Python的消息队列包SnakeMQ使用初探
Jun 29 Python
Python如何判断数独是否合法
Sep 08 Python
详解用python实现简单的遗传算法
Jan 02 Python
详解tensorflow实现迁移学习实例
Feb 10 Python
python3爬取数据至mysql的方法
Jun 26 Python
python 求一个列表中所有元素的乘积实例
Jun 11 Python
wxPython:python首选的GUI库实例分享
Oct 05 Python
Django框架创建项目的方法入门教程
Nov 04 Python
django框架forms组件用法实例详解
Dec 10 Python
python实现学生成绩测评系统
Jun 22 Python
pycharm 实现调试窗口恢复
Feb 05 Python
python数据处理之Pandas类型转换
Python实现将多张图片合成MP4视频并加入背景音乐
Python FuzzyWuzzy实现模糊匹配
Apr 28 #Python
PYTHON基于Pyecharts绘制常见的直角坐标系图表
PYTHON 使用 Pandas 删除某列指定值所在的行
Apr 28 #Python
详解PyTorch模型保存与加载
Apr 28 #Python
Python 图片添加美颜效果
You might like
ajax在joomla中的原生态应用代码
2012/07/19 PHP
php的POSIX 函数以及进程测试的深入分析
2013/06/03 PHP
php短址转换实现方法
2015/02/25 PHP
Linux+Nginx+MySQL下配置论坛程序Discuz的基本教程
2015/12/23 PHP
Extjs Ajax 乱码问题解决方案
2009/04/15 Javascript
fancybox1.3.1 基于Jquery的插件在IE中图片显示问题
2010/10/01 Javascript
js实现文字垂直滚动和鼠标悬停效果
2015/12/31 Javascript
js中遍历Map对象的简单实例
2016/08/08 Javascript
JavaScript 判断对象中是否有某属性的常用方法
2018/06/14 Javascript
JavaScript设计模式之代理模式实例分析
2019/01/16 Javascript
[11:01]2014DOTA2西雅图邀请赛 冷冷带你探秘威斯汀
2014/07/08 DOTA
在Python中使用poplib模块收取邮件的教程
2015/04/29 Python
使用Python编写一个最基础的代码解释器的要点解析
2016/07/12 Python
python 读文件,然后转化为矩阵的实例
2018/04/23 Python
Python实现对文件进行单词划分并去重排序操作示例
2018/07/10 Python
Python3实现定时任务的四种方式
2019/06/03 Python
python词云库wordcloud的使用方法与实例详解
2020/02/17 Python
Python3如何使用tabulate打印数据
2020/09/25 Python
详解Python遍历列表时删除元素的正确做法
2021/01/07 Python
澳大利亚旅游网站:Lastminute
2017/08/07 全球购物
企业面试题试卷附带答案
2015/12/20 面试题
学校司机岗位职责
2013/11/14 职场文书
教师研修随笔感言
2014/01/23 职场文书
家长对孩子的评语
2014/04/18 职场文书
仓库规划计划书
2014/04/28 职场文书
2014年党员个人工作总结
2014/12/02 职场文书
经典导游欢迎词
2015/01/26 职场文书
谢师宴邀请函
2015/02/02 职场文书
2015新学期家长寄语
2015/02/26 职场文书
计算机专业自荐信
2015/03/05 职场文书
总经理助理岗位职责范本
2015/03/31 职场文书
党支部创先争优公开承诺书
2015/04/30 职场文书
离婚起诉书范本
2015/05/18 职场文书
2019最新版试用期劳动合同模板!
2019/07/04 职场文书
Nginx服务器添加Systemd自定义服务过程解析
2021/03/31 Servers
MyBatis XPathParser解析器使用范例详解
2022/07/15 Java/Android