[英]access python ctypes structure from c shared library
我写了一个python代码来将ctypes结构传递给c库函数
python代码:
from ctypes import *
class Info(Structure):
_field_ = [("input",c_int),
("out",c_int)]
info = Info()
info.input = c_int(32)
info.out = c_int(121)
lib = CDLL("./sharedLib.so").getVal
a = lib(byref(info))
C代码:
#include <stdio.h>
struct info{
int input;
int out;
};
void getVal(struct info *a){
printf("in = %i \n", a->input);
printf("out = %i \n", a->out);
}
使用命令编译它: gcc -shared -fPIC -o sharedLib.so sharedLib.c
输出:
in = 0
out = 0
我的问题是,为什么输出与我在python代码中设置的值不同。 有什么解决办法吗? 我在32位环境中
在ctypes结构定义中,您编写了_field_
而不是_fields_
。 结果,这些字段不会转换为C等效项。
尝试添加:
lib.argtypes = [POINTER(Info)]
在调用lib之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.