小编Alv*_*vin的帖子

从c共享库访问python ctypes结构

我编写了一个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))
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

使用命令编译它: gcc -shared -fPIC -o sharedLib.so sharedLib.c

输出:

in = 0 
out = 0 
Run Code Online (Sandbox Code Playgroud)

我的问题是,为什么输出与我在python代码中设置的值不同.有什么解决方案吗?我在32位环境中

c python ctypes

1
推荐指数
1
解决办法
1406
查看次数

标签 统计

c ×1

ctypes ×1

python ×1