博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 调用C++ DLL,传递int,char,char*,数组和多维数组
阅读量:4493 次
发布时间:2019-06-08

本文共 1477 字,大约阅读时间需要 4 分钟。

ctypes 数据类型和 C数据类型 对照表

ctypes type C type Python type
_Bool bool (1)
char 1-character string
wchar_t 1-character unicode string
char int/long
unsigned char int/long
short int/long
unsigned short int/long
int int/long
unsigned int int/long
long int/long
unsigned long int/long
__int64 or long long int/long
unsigned __int64 or unsigned long long int/long
float float
double float
long double float
char * (NUL terminated) string or None
wchar_t * (NUL terminated) unicode or None
void *

int/long or None

 

//C++文件

#include
using namespace std;//该文件名称:cpptest.cpp//终端下编译指令://g++ -o cpptest.so -shared -fPIC cpptest.cpp//-o 指定生成的文件名,-shared 指定微共享库,-fPIC 表明使用地址无关代码extern "C"{
//在extern “C”中的函数才能被外部调用 int test(int int_test,char char_test,char *test_string,int int_arr[4],char char_arr2[2][2]) { cout<<"输出参数中的int型:"; cout<
<

 

//py文件

import ctypesmylib = ctypes.cdll.LoadLibrary("cpptest.so")char_p_test = bytes("中国","utf8")#汉字需用采用utf8编码int_arr4 = ctypes.c_int*4int_arr = int_arr4()int_arr[0] = 1int_arr[1] = 3int_arr[2] = 5int_arr[3] = 9char_arr2 = ctypes.c_char*2char_arr22 = char_arr2*2char_arr22a = char_arr22()char_arr22a[0][0] = b'a'char_arr22a[0][1]=  b'b'char_arr22a[1][0] = b'c'char_arr22a[1][1] = b'd'mylib.test(9999,'a',char_p_test,int_arr,char_arr22a)

 

 

参考:

 

结构体传参

https://www.jb51.net/article/52513.htm

 

 

https://www.programcreek.com/python/example/1243/ctypes.c_char_p

转载于:https://www.cnblogs.com/code1992/p/11211974.html

你可能感兴趣的文章
Deep Learning 13_深度学习UFLDL教程:Independent Component Analysis_Exercise(斯坦福大学深度学习教程)...
查看>>
oracle 表空间/用户 增加删除
查看>>
安装PIL库时提示python未注册错误(自定义python安装路径)
查看>>
MySQL和MSSQL差异(增量)备份的原理
查看>>
帮你整理的前端笔试面试题
查看>>
一个简单易上手的webapp框架HBuilder mui
查看>>
创建对象
查看>>
《暗时间》学习总结之高效利用时间
查看>>
设计模式—策略模式
查看>>
zhx P98 a题
查看>>
POJ 3041 Asteroids (二分图最小点覆盖)
查看>>
Vue 爬坑之路(四)—— 与 Vuex 的第一次接触
查看>>
MySQL数据迁移到SQL Server
查看>>
Disconf源码分析之启动过程分析下(2)
查看>>
HTML <input> 标签
查看>>
java抽象类实现接口可以不用实现方法
查看>>
poj 2395 Out of Hay (最小生成树的最大边)
查看>>
(原)OSX 也变成svn服务器---3(欢迎大家指出错误。交流提升自己。)
查看>>
2019春第八周作业
查看>>
adt-bundle-windows-x86-20131030
查看>>