所以我正在尝试读取一个无符号短数组,该数组由我正在使用的 c 库中的 void 指针返回。 header中的函数定义是这样的:
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: 'NoneType' object has no attribute '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 中表示为 None。
我不知道该函数为什么会这样,但您看到的输出非常清楚地表明您的函数返回 null。
关于Python ctypes 如何从空指针返回读取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15938599/