我有下面的C代码,可以从python脚本中使用。在
不幸的是,这只是一个自动生成的巨大库的节选,我无法更改。这里我只想将结构元素打印到一个控制台上,以演示出什么地方出了问题。在
// CFunc.h
#include <stdio.h>
typedef struct
{
int npar;
struct
{
int id;
int value;
} params[10];
} Data_t;
void Cfunc( const Data_t * d);
// CFunc.c
#include "CFunc.h"
void Cfunc( const Data_t * d)
{
int inpar = 0;
int maxnpar = 0;
printf("%d:\n", d->npar);
maxnpar = d->npar;
inpar=0;
while (maxnpar > inpar)
{
printf(" %d: %08x %08x\n", inpar, d->params[inpar].id, *(int*)&d->params[inpar].value);
inpar++;
}
}
它被编译并链接到共享库:
gcc -fPIC -c CFunc.c -o CFunc.o
gcc -shared -lrt -Wl,-soname,libCFunc.so.1 -o libCFunc.so CFunc.o
因此,我使用ctypes执行了以下实现:
^{pr2}$所以我在初始化给定元组列表中的结构,在本例中只有2个,并调用C函数来查看其输出。在
paramlist = (
( 0x050000000, 0x00000000 ),
( 0x050000001, 0x447a0000 ) )
temp = Data_t(paramlist)
pyFunc(temp)
不幸的是,输出结果与预期不符:
2:
0: 00000000 79948ef0
1: 00007fe5 00000000
你觉得我错过了什么吗?在
[Python 3]: ctypes - A foreign function library for Python。在
我重组了你的代码。在
dll.h:
dll.c:
^{pr2}$代码.py:
输出:
相关问题 更多 >
编程相关推荐