python调用C++,传递结构体与结构体指针,以及嵌套结构体
2018-03-21 17:04
1036 查看
#include<iostream>using namespace std;//该文件名称:cpptest.cpp//终端下编译指令://g++ -o cpptest.so -shared -fPIC cpptest.cppstruct sub_struct{ char* test_char; int test_int;};struct struct_def { char* stru_string; int stru_int; char stru_arr_num[4]; sub_struct son_struct;};extern "C"{//在extern “C”中的函数才能被外部调用 int test(struct_def struct_mystruct,struct_def* struct_test_p) { //输出结构体指针的数据 cout<<"输出结构体中的char*字符:"; cout << struct_mystruct.stru_string << endl; cout<<"输出结构体中的int型:"; cout << struct_mystruct.stru_int <<endl; cout <<"输出结构体中的字符数组:"; for(int x = 0;x< 4;x++){cout << struct_mystruct.stru_arr_num[x]<<" ";} cout<< endl; cout<<"输出子结构体中的char*型:"; cout << struct_mystruct.son_struct.test_char<<endl; cout<<"输出子结构体中的int型:"; cout<<struct_mystruct.son_struct.test_int<<endl; //输出结构体指针的数据 cout<<endl; cout<<endl; cout<<"输出结构体指针中的char*字符:"; cout << struct_test_p->stru_string << endl; cout<<"输出结构体指针中的int型:"; cout << struct_test_p->stru_int <<endl; cout <<"输出结构体指针中的字符数组:"; for(int x = 0;x< 4;x++){cout << struct_test_p->stru_arr_num[x]<<" ";} cout<< endl; cout<<"输出子结构体指针中的字符串:"; cout<<struct_test_p->son_struct.test_char; cout << endl; cout<<"输出子结构体指针中的int型:"; cout<<struct_test_p->son_struct.test_int<<endl; }}##python 文件##文件名 pytest.pyimport ctypesmylib = ctypes.cdll.LoadLibrary("cpptest.so")class sub_struct(ctypes.Structure): _fields_ = [ ("test_char_p",ctypes.c_char_p), ("test_int",ctypes.c_int) ]class struct_def(ctypes.Structure): _fields_ = [ ("stru_string",ctypes.c_char_p), ("stru_int", ctypes.c_int), ("stru_arr_num", ctypes.c_char*4), ("son_struct", sub_struct) ]struct_mystruct = struct_def()struct_mystruct.stru_string = b"string in the struct"struct_mystruct.stru_int = 99struct_mystruct.stru_arr_num = b"ABCD"struct_mystruct.son_struct.test_char_p =b"sub struct of the string"struct_mystruct.son_struct.test_int = 10mylib.test(struct_mystruct,ctypes.byref(struct_mystruct))
相关文章推荐
- 使用python向C语言的链接库传递数组、结构体、指针类型的数据
- 结构体参数的传递以及如何修改指针参数的值
- 结构体指针和结构体对象的创建、置空、赋值以及与string的转换
- 结构体参数的传递以及如何修改指针参数的值
- 总结:结构体嵌套调用内层结构体数据的问题
- 有关python调用dll的问题,指针传递的问题
- 第22节 C语言结构体之结构体嵌套、结构体指针与结构体数组的代码实现
- 当用 Python ctypes 模块调用动态链接库,如何传入一个结构体指针
- 初级菜鸟 学习学习递归调用以及结构体中包含指向自己的指针用法
- Vb.net调用C写的Dll含结构体指针参数的方法
- python调用C++,C++回调Python,并传递参数
- C语言函数调用时参数压栈的顺序以及函数指针的作用
- c++调用python以及使用python将gbk/utf-8转punycode
- Python引用传递、赋值规则以及深拷贝和浅拷贝
- Python调用HTTP接口并传递cookie
- C#调用C++编写的DLL函数, 以及各种类型的参数传递 不指定
- 值传递-引用传递-指针传递-函数调用时指针参数的传递 指针和引用的区别
- 用ladon框架封装Python为Webservice接口以及调用接口的方法
- 一个基于python写的ms sql和postgresql互相转化表结构以及操作的用例
- Sphinx 全文检索引擎的搭建以及python调用例子