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*,数组和多维数组的更多相关文章

  1. 绝对好文C#调用C++DLL传递结构体数组的终极解决方案

    C#调用C++DLL传递结构体数组的终极解决方案 时间 2013-09-17 18:40:56 CSDN博客相似文章 (0) 原文  http://blog.csdn.net/xxdddail/art ...

  2. 简单实现python调用c#dll动态链接库

    在python调用c#dll库时要先安装库clr,即安装pythonnet,参考文章:https://www.cnblogs.com/kevin-Y/p/10235125.html(为在python中 ...

  3. [源码]Python调用C# DLL例子(Python与.Net交互)

    K8Cscan C# DLL例子代码 namespace CscanDLL { public class scan { public static string run(string ip) { if ...

  4. C#调用C++DLL传递结构体数组的终极解决方案

    在项目开发时,要调用C++封装的DLL,普通的类型C#上一般都对应,只要用DllImport传入从DLL中引入函数就可以了.但是当传递的是结构体.结构体数组或者结构体指针的时候,就会发现C#上没有类型 ...

  5. python中数组与多维数组用法介绍

    增加时a.append( 'a ')就可以了.只要按顺序加,就没有问题 . 使用时,完全可以使用下标: 代码如下 复制代码 a[0] a[1] 但出果引用不存在的下标,则会引发异常.这时,你需要先添加 ...

  6. Python输入数组(一维数组、二维数组)

    一维数组: arr = input("") //输入一个一维数组,每个数之间使空格隔开 num = [int(n) for n in arr.split()] //将输入每个数以空 ...

  7. Python数组操作将一维数组变成二维数组

    一.问题 我们在进行数组操作的时候会遇到将一个低维的数组变成一个高维的素数组 二.解决 第一种方法基本思路就是将低维数组进行等长的循环,在第一次为零的情况下,需要添加一个[]数组,原因是将它的基本框架 ...

  8. Python代码阅读(第12篇):初始化二维数组

    Python 代码阅读合集介绍:为什么不推荐Python初学者直接看项目源码 本篇阅读的代码实现了二维数组的初始化功能,根据给定的宽高初始化二维数组. 本篇阅读的代码片段来自于30-seconds-o ...

  9. Python调用C++DLL函数出错String类型问题

    调用c++ 函数原型如下,一直失败,请个日志断点发现 参数未能正确解析. int EXPORT init_ner(string cfg_path); typedef int (*Proc_init_n ...

随机推荐

  1. Diango路由映射FBV和CBV

    django中请求处理方式有2种:FBV(function base views) 和 CBV(class base views),换言之就是一种用函数处理请求,一种用类处理请求. FBV # url ...

  2. [LeetCode] 130. 被围绕的区域

    题目链接 : https://leetcode-cn.com/problems/surrounded-regions/ 题目描述: 给定一个二维的矩阵,包含 'X' 和 'O'(字母 O). 找到所有 ...

  3. Python 多列数据存储

    zip()函数 zip函数可以把多个列表相加成一个tuple(元组) a = [1,2,3,4] b = [11,22,33,44] c = [111,222,333,444] A = list(zi ...

  4. NoSQL特点

  5. 攻防世界--CGfsb238

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/5982010c172744c8a1c93c24b5200b21 1.格式化字符串漏洞 ...

  6. class path resource [applicationContext.xml] cannot be opened because it does not exis

    使用maven创建web工程,将spring配置文件applicationContext.xml放在src/resource下,用eclipse编译时提示class path resource [ap ...

  7. Mongo--04 Mongo分片集群

    目录 一.分片的概念 二. 分片工作原理 三.IP端口目录规划 1.IP端口规划 2.目录规划 四.分片集群搭建副本集步骤 1.安装软件 2.创建目录 3.创建配置文件 4.优化警告 5.启动服务 6 ...

  8. 通过virsh console进入虚拟机

    1.virsh启动一个虚拟机.执行脚本test_qga.sh 2.virsh vncdisplay <vm_ID> 3.vnc登录到vm里面,执行#systemctl start seri ...

  9. rsync服务实践

    RSYNC数据备份 RSYNC=Remote Sync 远程同步   高效,一定要结合shell 官方网站:https://rsync.samba.org/ Author:     Andrew Tr ...

  10. LOJ 6192 城市网络(树上倍增)

    LOJ #6192. 「美团 CodeM 复赛」城市网络(链接) 一棵以 $ 1 $ 号节点为根的树,每个点有一个权值,有 $ q $ 个询问,每次从 $ x $ 点开始往某个祖先 $ y $ 走,初 ...