我在Python中使用ctypes,我需要将一个指针传递给指向某个C函数的结构体的指针数组。 这是结构:
typedef struct {
float x;
float y;
float z;
float radius;
} Sphere;
我的功能具有以下原型:
void render(Sphere** spheres);
在 Python 中,我为 Sphere 结构声明了一个类,我需要将 argtypes 设置为
render
函数:
lib_render = ctypes.cdll.LoadLibrary('librender.so')
class Sphere(ctypes.Structure):
_fields_ = [('x', ctypes.c_float),
('y', ctypes.c_float),
('z', ctypes.c_float),
('radius', ctypes.c_float)]
render = lib_render.render
render.argtypes = [<cannot find out what needs to be here>]
spheres = numpy.array([Sphere(1, 2.8, 3, 0.5),
Sphere(4.2, 2, 1, 3.2)])
render(spheres)
如何正确传递该数组?
我不太使用 numpy,但以下内容无需它即可工作。 我假设如果您将指针传递给指针,则指针列表必须以空终止。
from ctypes import *
class Sphere(Structure):
_fields_ = [('x', c_float),
('y', c_float),
('z', c_float),
('radius', c_float)]
dll = CDLL('test')
dll.render.argtypes = POINTER(POINTER(Sphere)),
dll.render.restype = None
# Create a couple of objects
a = Sphere(1,2,3,4)
b = Sphere(5,6,7,8)
# build a list of pointers, null-terminated.
c = (POINTER(Sphere) * 3)(pointer(a),pointer(b),None)
dll.render(c)
测试DLL:
#include <stdio.h>
typedef struct Sphere {
float x;
float y;
float z;
float radius;
} Sphere;
__declspec(dllexport) void render(Sphere** spheres)
{
for(;*spheres;++spheres)
printf("%f %f %f %f\n",(*spheres)->x,(*spheres)->y,(*spheres)->z,(*spheres)->radius);
}
输出:
1.000000 2.000000 3.000000 4.000000
5.000000 6.000000 7.000000 8.000000
使用
numpy
,使用 void render(Sphere* spheres, size_t len)
,这有效。 也许对numpy比较熟悉的人可以评论是否可以支持Sphere**
。
from ctypes import *
import numpy as np
class Sphere(Structure):
_fields_ = [('x', c_float),
('y', c_float),
('z', c_float),
('radius', c_float)]
dll = CDLL('test')
dll.render.argtypes = POINTER(Sphere),c_size_t
dll.render.restype = None
a = Sphere(1,2,3,4)
b = Sphere(5,6,7,8)
# c = (Sphere * 2)(a,b)
# dll.render(c,len(c))
d = np.array([a,b])
dll.render(d.ctypes.data_as(POINTER(Sphere)),len(d))