python 调用C++ DLL,传递int,char,char*,数组和多维数组
ctypes 数据类型和 C数据类型 对照表
| ctypes type | C type | Python type |
|---|---|---|
| c_bool | _Bool | bool (1) |
| c_char | char | 1-character string |
| c_wchar | wchar_t | 1-character unicode string |
| c_byte | char | int/long |
| c_ubyte | unsigned char | int/long |
| c_short | short | int/long |
| c_ushort | unsigned short | int/long |
| c_int | int | int/long |
| c_uint | unsigned int | int/long |
| c_long | long | int/long |
| c_ulong | unsigned long | int/long |
| c_longlong | __int64 or long long | int/long |
| c_ulonglong | unsigned __int64 or unsigned long long | int/long |
| c_float | float | float |
| c_double | double | float |
| c_longdouble | long double | float |
| c_char_p | char * (NUL terminated) | string or None |
| c_wchar_p | wchar_t * (NUL terminated) | unicode or None |
| c_void_p | void * |
int/long or None |
//C++文件
#include<iostream>
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[],char char_arr2[][]) {
cout<<"输出参数中的int型:";
cout<<int_test<<endl;
cout<<"输出参数中的char型:";
cout<<char_test<<endl;
cout << "输出参数中的字char*字符:";
cout<<test_string<<endl;
cout << "输出参数中的int数组";
for(int x = ;x< ;x++){cout << int_arr[x]<<" ";}
cout << endl;
cout <<"输出参数中的二维数组:";
for(int x = ;x<;x++){
for(int y = ;y<;y++){
cout <<char_arr2[x][y] << " ";
}
}
cout << endl;
return ;
}
}
//py文件
import ctypes
mylib = ctypes.cdll.LoadLibrary("cpptest.so")
char_p_test = bytes("中国","utf8")#汉字需用采用utf8编码
int_arr4 = ctypes.c_int*4
int_arr = int_arr4()
int_arr[0] = 1
int_arr[1] = 3
int_arr[2] = 5
int_arr[3] = 9
char_arr2 = ctypes.c_char*2
char_arr22 = char_arr2*2
char_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)
参考:python 调用C++,传递int,char,char*,数组和多维数组
结构体传参
https://www.jb51.net/article/52513.htm
『Python CoolBook』使用ctypes访问C代码_下_demo进阶
https://www.programcreek.com/python/example/1243/ctypes.c_char_p
python 调用C++ DLL,传递int,char,char*,数组和多维数组的更多相关文章
- 绝对好文C#调用C++DLL传递结构体数组的终极解决方案
C#调用C++DLL传递结构体数组的终极解决方案 时间 2013-09-17 18:40:56 CSDN博客相似文章 (0) 原文 http://blog.csdn.net/xxdddail/art ...
- 简单实现python调用c#dll动态链接库
在python调用c#dll库时要先安装库clr,即安装pythonnet,参考文章:https://www.cnblogs.com/kevin-Y/p/10235125.html(为在python中 ...
- [源码]Python调用C# DLL例子(Python与.Net交互)
K8Cscan C# DLL例子代码 namespace CscanDLL { public class scan { public static string run(string ip) { if ...
- C#调用C++DLL传递结构体数组的终极解决方案
在项目开发时,要调用C++封装的DLL,普通的类型C#上一般都对应,只要用DllImport传入从DLL中引入函数就可以了.但是当传递的是结构体.结构体数组或者结构体指针的时候,就会发现C#上没有类型 ...
- python中数组与多维数组用法介绍
增加时a.append( 'a ')就可以了.只要按顺序加,就没有问题 . 使用时,完全可以使用下标: 代码如下 复制代码 a[0] a[1] 但出果引用不存在的下标,则会引发异常.这时,你需要先添加 ...
- Python输入数组(一维数组、二维数组)
一维数组: arr = input("") //输入一个一维数组,每个数之间使空格隔开 num = [int(n) for n in arr.split()] //将输入每个数以空 ...
- Python数组操作将一维数组变成二维数组
一.问题 我们在进行数组操作的时候会遇到将一个低维的数组变成一个高维的素数组 二.解决 第一种方法基本思路就是将低维数组进行等长的循环,在第一次为零的情况下,需要添加一个[]数组,原因是将它的基本框架 ...
- Python代码阅读(第12篇):初始化二维数组
Python 代码阅读合集介绍:为什么不推荐Python初学者直接看项目源码 本篇阅读的代码实现了二维数组的初始化功能,根据给定的宽高初始化二维数组. 本篇阅读的代码片段来自于30-seconds-o ...
- Python调用C++DLL函数出错String类型问题
调用c++ 函数原型如下,一直失败,请个日志断点发现 参数未能正确解析. int EXPORT init_ner(string cfg_path); typedef int (*Proc_init_n ...
随机推荐
- 高级BASH
Bash介绍与入门 1,简介 Bash(GNU Bourne-Again Shell)是一个为GNU计划编写的Unix shell,它是许多Linux平台默认使用的shell shell是一个命令解释 ...
- 剑指offer-栈的压入、弹出序列-栈和队列-python
题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压 ...
- install stackless python on ubuntu
前言 我准备用stackless模拟游戏玩家登陆/注册等行为,测试游戏服务器的性能. 但是在安装stackless的过程中遇到了很多问题,特此记录下来,也分享给需要的朋友. 关于stackless S ...
- 【React 8/100】 React路由
React路由 React路由介绍 现代的前端应用大多数是SPA(单页应用程序),也就是只有一个HTML页面的应用程序.因为它的用户体验更好.对服务器压力更小,所以更受欢迎.为了有效的使用单个页面来管 ...
- axios 文件流下载
this.axios .post(this.baseUrl+"/exportUser", { admin: "",keys: "",keyw ...
- STM32 常用词汇释义
1.AF——Alternate function 复用功能: 2.NVIC——Nested Vectored Interrupt Controller 内嵌向量中断控制器 3.ISER[8]— ...
- vue 之 双向绑定原理
一.实现双向绑定 详细版: 前端MVVM实现双向数据绑定的做法大致有如下三种: 1.发布者-订阅者模式(backbone.js) 思路:使用自定义的data属性在HTML代码中指明绑定.所有绑定起来的 ...
- mac+react-native环境搭建
主要参考 https://reactnative.cn/docs/getting-started.html react-native中文网 IOS版 1.Node v10以上.Watchman 和 R ...
- mysql 数据库url
jdbc:mysql://localhost:3306/database?useUnicode=true&useJDBCCompliantTimezoneShift=true&useL ...
- 【03】Python 文件读写 JSON
1 打开文件 文件操作步骤: 1.打开文件获取文件的句柄,句柄就理解为这个文件 2.通过文件句柄操作文件 3.关闭文件. 1.1 打开方法 f = open('xxx.txt') #需f.close( ...