如何从python传递指向结构指针数组的指针
我在Python中使用ctypes,需要将指向指向结构的指针数组的指针传递给某个C函数。 这是结构:如何从python传递指向结构指针数组的指针,python,c,pointers,ctypes,Python,C,Pointers,Ctypes,我在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.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)
lib_render=ctypes.cdll.LoadLibrary('librender.so')
类球体(ctypes.Structure):
_字段\=[('x',ctypes.c_float),
('y',ctypes.c_float),
('z',ctypes.c_float),
('radius',ctypes.c_float)]
render=lib_render.render
render.argtypes=[]
球体=numpy.数组([球体(1,2.8,3,0.5),
球体(4.2,2,1,3.2)])
渲染(球体)
如何正确地传递该数组?我不太使用numpy,但是没有它,下面的代码也可以使用。我假设,如果要将指针传递给指针,指针列表必须以null结尾
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);
}
使用numpy
,使用void渲染(球体*球体,大小透镜)
,这项功能可以正常工作。如果可以支持Sphere**
,也许更熟悉numpy的人可以发表评论
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))
我不怎么使用numpy,但是没有它,follow也可以工作。我假设,如果要将指针传递给指针,指针列表必须以null结尾
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);
}
使用numpy
,使用void渲染(球体*球体,大小透镜)
,这项功能可以正常工作。如果可以支持Sphere**
,也许更熟悉numpy的人可以发表评论
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))
我还没有测试过这个,但是我假设它是
ctypes.POINTER(ctypes.POINTER(Sphere))
我试过了,但没有成功。它无法将numpy数组自动转换为LP\u LP\u Sphere
,如果我将spheres.ctypes.data\u作为(指针(指针(球体))
传递给函数,函数会接收未知垃圾,每次都不同。您是否查看了numpy.ctypeslib.ndpointer
,有一次,我用它将指针传递给一个整数数组或其他一些C类型:np.ctypeslib.ndpointer(dtype=np.int32,ndim=1,flags='C_continuous')
但我不知道如何将它应用于指向结构的指针数组。我还没有测试过它,但我假设它是ctypes.pointer(ctypes.pointer(Sphere))
我试过了,但没用。它无法将numpy数组自动转换为LP\u LP\u Sphere
,如果我将spheres.ctypes.data\u作为(指针(指针(球体))
传递给函数,函数会接收未知垃圾,每次都不同。您是否查看了numpy.ctypeslib.ndpointer
,有一次,我用它将指针传递给一个整数数组或其他一些C类型:np.ctypeslib.ndpointer(dtype=np.int32,ndim=1,flags='C_continuous')
,但我不知道如何将它应用于指向结构的指针数组。这个方法不错。我不知道你们可以把指针乘以一个数字,得到指针数组。我想我甚至不需要numpy阵列。很好的方法。我不知道你们可以把指针乘以一个数字,得到指针数组。我想我甚至不需要numpy阵列。