Python Ctypes结构体指针处理(函数参数,函数返回)
来源:互联网 发布:匈牙利算法步骤 编辑:程序博客网 时间:2024/06/06 05:18
参考网址: http://www.2cto.com/kf/201109/106444.html
本文演示了在python中调用C语言生成的动态库,返回结构体指针,并进行输出!
test.c(动态库源代码)
// 编译生成动态库: gcc -g -fPIC -shared -o libtest.so test.c#include <stdio.h>#include <string.h>#include <stdlib.h>typedef struct StructPointerTest{char name[20];int age;}StructPointerTest, *StructPointer;StructPointer test()// 返回结构体指针{ StructPointer p = (StructPointer)malloc(sizeof(StructPointerTest)); strcpy(p->name, "Joe");p->age = 20;return p; }
编译:gcc -g -fPIC -shared -o libtest.so test.c
call.py(python调用C语言生成的动态库):
#!/bin/env python# coding=UTF-8from ctypes import *#python中结构体定义class StructPointer(Structure):_fields_ = [("name", c_char * 20), ("age", c_int)]if __name__ == "__main__":lib = cdll.LoadLibrary("./libtest.so")lib.test.restype = POINTER(StructPointer)p = lib.test()print "%s: %d" %(p.contents.name, p.contents.age)
最后运行结果:
[zcm@c_py #112]$make cleanrm -f *.o libtest.so[zcm@c_py #113]$makegcc -g -fPIC -shared -o libtest.so test.c[zcm@c_py #114]$./call.py Joe: 20[zcm@c_py #115]$
- Python Ctypes结构体指针处理(函数参数,函数返回)
- Python Ctypes 多级指针 传参数 和返回值
- 结构体、结构指针作为函数参数
- Python使用ctypes调用C函数传参处理
- 结构体变量指针作为函数参数
- 用结构体指针作为函数参数
- 用结构体指针作为函数参数
- 函数参数调用指针、结构体
- 结构体指针作函数的参数
- 结构体、结构体体指针作为函数返回值
- 使用ctypes调用C共享库中函数返回值为链表式结构时的方法
- 结构体指针与结构体名做函数参数对比
- Python 使用ctypes调用 C 函数
- Python调用ctypes使用C函数printf
- Python 使用ctypes调用 C 函数
- Python 外部函数调用库ctypes简介
- 返回函数的指针, 参数为函数指针。。。。。
- 【C语言提高37】结构体元素做函数参数与结构指针做函数参数
- c#实现震动窗体
- 各种光照算法
- javascript对象小探之二——length
- 成功源于细节-河边的苹果
- 窗口大小改变事件handler
- Python Ctypes结构体指针处理(函数参数,函数返回)
- 哈佛经济学家关于工作效率的意外发现
- 在cocos2dx中製作人物文本框
- JavaScript基础
- Windows下配置sftp,并使用C#上传下载
- makefile
- [iOS] identifierForVendor和advertisingIdentifier为全0或者每次重启应用会变的bug
- 迅雷,快车,旋风下载地址加密和解密分析
- gcc: error trying to exec 'cc1': execvp: 没有那个文件或目录