Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ctypes介绍如何从空指针返回读取数组_Python_Ctypes - Fatal编程技术网

Python ctypes介绍如何从空指针返回读取数组

Python ctypes介绍如何从空指针返回读取数组,python,ctypes,Python,Ctypes,因此,我试图读取一个无符号短数组,它由我使用的c库中的一个空指针返回。标题中的函数定义如下: void* Foo(int a, int b) 此函数最终返回指向unsigned short类型数组的指针 在python中,我一直试图使用ctypes返回数组,但没有成功。以下是我所拥有的: import ctypes import numpy as np libc = ctypes.cdll.LoadLibrary("Library.dll") Vec=np.zeros((1000,),dt

因此,我试图读取一个无符号短数组,它由我使用的c库中的一个空指针返回。标题中的函数定义如下:

void* Foo(int a, int b)
此函数最终返回指向unsigned short类型数组的指针

在python中,我一直试图使用ctypes返回数组,但没有成功。以下是我所拥有的:

import ctypes
import numpy as np

libc = ctypes.cdll.LoadLibrary("Library.dll")

Vec=np.zeros((1000,),dtype=np.uint16)

c_ushort_p = ctypes.POINTER(ctypes.c_ushort)
Vec_p=confVec.ctypes.data_as(c_ushort_p)

libc.Foo.restype = ctypes.c_void_p
Vec_p=libc.Foo(1,1)

print Vec_p
返回“无”

如果我尝试:

...
libc.Foo.restype = ctypes.c_void_p
Vec_p=libc.Foo(1,1)

print Vec_p[0]
我得到TypeError:“非类型”对象没有属性“getitem

我也试过:

...
libc.Foo.restype = ctypes.c_void_p
Vec_p=ctypes.cast(libc.Foo(1,1),c_ushort_p)

print Vec_p
返回,其中print Vec_p[0]给出“ValueError:NULL指针访问”


有人能帮我吗?

DLL返回空指针。这些在ctypes中表示为无


我不知道为什么函数会这样做,但是您看到的输出非常清楚地表明,您的函数返回null

多谢各位。我以为我用错了ctypes。事实证明,我的程序中的其他地方有一个简单的错误。第二次尝试似乎没有问题,但设置了
Foo.restype=c\u ushort\u p
,而不是强制转换。然后,不管您得到的是数组
length
,您都可以使用
np.ctypeslib.as\u数组(Foo(a,b),(length,)
来创建NumPy视图。