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 ...
随机推荐
- 阿里云服务器重启出现An error occurred 如何处理
最近网站重启阿里云服务后,出现 An error occurred, An error occurred. Sorry, the page you are looking for is current ...
- angularJS(二):作用域$scope、控制器、过滤器
app.controller创建控制器 一.作用域 Scope(作用域) 是应用在 HTML (视图) 和 JavaScript (控制器)之间的纽带. Scope 是一个对象,有可用的方法和属性. ...
- 如何让css隐藏滚动条 兼容谷歌、火狐、IE等各个浏览器
项目中,页面效果需要展示一个页面的移动端效果,使用的是一个苹果手机样式背景图,咋也没用过苹果,咋也不敢形容. 如下图所示: 在谷歌浏览器如图一滚动条顺利隐藏,但是火狐就如图二了,有了滚动条丑的一批. ...
- Spring基础04——ApplicationContext
1.ApplicationContext简述 ApplicationContext代表IOC容器,在SpringIOC容器中读取Bean配置创建Bean实例之前,必须对它进行实例化,只有在容器实例化后 ...
- ln -在文件之间建立连接
总览 ln [options] source [dest] ln [options] source...directory POSIX 选项: [-f] GNU 选项(缩写): [-bdfinsvF] ...
- 牛客练习赛14 D比较月亮大小 (实现)
链接:https://ac.nowcoder.com/acm/contest/82/D来源:牛客网 题目描述 点点是一名出色的狼人.众所周知,狼人只有在满月之夜才会变成狼. 同时,月亮的大小随着时间变 ...
- First-hitting-time model
见wiki: https://en.wikipedia.org/wiki/First-hitting-time_model
- C#基础知识之dnSpy反编译
dnSpy工具可以在网上自行下载 软件界面如下: 现在进入话题,首先编写一个Hello World的控制台运行程序,如下图所示: 代码如下: using System; using System.Co ...
- JavaWeb中的文件上传和下载功能的实现
导入相关支持jar包:commons-fileupload.jar,commons-io.jar 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上 ...
- 【LuoguP4770】[NOI2018] 你的名字
题目链接 题意简述 给定一个串 \(S\) 多组询问 , 每次给定一个串 \(T\) 和一个 区间 \([l,r]\) 求串\(T\) 有多少个本质不同的子串 满足不是 \(S[l...r]\) 的子 ...