我有以下代码:
库文件
struct Node {
int index;
char name[10];
};
void getAllNodes(Node array[]);
libtest.py
from ctypes import *
class Node(Structure):
_fields_ = [("index", c_uint), ("name", c_char*10)]
lib = CDLL("/path/to/lib.so")
nodeCount = 5
NodeArray = Node * nodeCount
getAllNodes = lib.getAllNodes
pointers = NodeArray()
getAllNodes(pointers)
casted = cast(pointers, POINTER(NodeArray))
for x in range(0, nodeCount):
node = cast(casted[x], POINTER(Node))
print "Node idx: %s name: %s" % (node.contents.index, node.contents.name)
我必须接近,因为索引 0 处的 Node 结构具有正确的值,但其余部分是乱码。我在错误地构建什么给我带来了问题?