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 ...
随机推荐
- django商城项目之用sentry管理日志
之前写商城项目的时候,采用的日志处理方式为在终端输出或者写入文件,这样的话,项目部署上线之后,若服务器出现错误,需要到服务器查看相关的错误日志,很不方便.后期在学习别人开源项目的时候,学习到一个开源的 ...
- ExtensionlessUrlHandler-Integrated-4.0
window 10 锁定webconfig解决方案 解决办法: 出现这个错误是因为 IIS 7 采用了更安全的 web.config 管理机制,默认情况下会锁住配置项不允许更改.要取消锁定可以以管理 ...
- jQuery中$()可以有两个参数
jQuery(expression, [context]) 返回值:jQuery 概述 这个函数接收一个包含 CSS 选择器的字符串,然后用这个字符串去匹配一组元素. jQuery 的核心功能都是通过 ...
- NSPredicate的使用,超级强大
NSPredicate *ca = [NSPredicate predicateWithFormat:(NSString *), ...]; Format: (1)比较运算符>,<,==, ...
- tortoise svn回滚常用命令
一.revert to this version 和 revert changes from this version的区别 假设SVN已有版本4814-4854:1.基于4837版本执行[rever ...
- 05java基础
1.BigInteger和BigDecimal类 package cn.jxufe.java.chapter5.demo01; import java.math.BigInteger; public ...
- Tensorflow揭秘
https://www.bilibili.com/video/av64970827/?p=7 tf2.0主要使用tf.keras api来构建模型,主要包括如下几个部分 一.Layers 如下是一些特 ...
- 如何将上个SQL的结果作为参数传递给下个SQL
如何将上个SQL的结果作为参数传递给下个SQL: ##source认证 kinit认证 source /home/omm/ficlient/bigdata_env kinit -k -t /ETL/c ...
- MySQL---时区问题
1.控制台输入select now(); 发现与北京时间相差8小时: 2.解决: 2.1.进入MySQL客户端 mysql -uroot -p 2.2.全局,当前会话时区设置set GLOBAL ...
- Python---面向对象编程---自定义列表和集合操作类
一.定义一个列表的操作类Listinfo 包括的方法 1.列表元素添加:add_key() 添加的必须是数字或者是字符串 2.列表元素取值:get_key() 3.列表合并:update_list( ...