从Python访问DLL中的数组
我正在尝试从Python访问DLL中的int数组。我遵循ctypes文档页面中的指导原则,但是我得到了空指针访问异常。我的代码是:从Python访问DLL中的数组,python,arrays,pointers,ctypes,Python,Arrays,Pointers,Ctypes,我正在尝试从Python访问DLL中的int数组。我遵循ctypes文档页面中的指导原则,但是我得到了空指针访问异常。我的代码是: if __name__ == "__main__": cur_dir = sys.path[0] os.chdir(cur_dir) api = CDLL("PCIE_API") PciAgentIndex=POINTER(c_uint32).in_dll(api, "PciAgentIndex") print(PciAgen
if __name__ == "__main__":
cur_dir = sys.path[0]
os.chdir(cur_dir)
api = CDLL("PCIE_API")
PciAgentIndex=POINTER(c_uint32).in_dll(api, "PciAgentIndex")
print(PciAgentIndex)
print(PciAgentIndex[0])
我得到:
ValueError: NULL pointer access
当我打印最后一行时
当我通过Eclipse调试器运行此代码段并检查PciAgentIndex的content属性时,我得到:
str: Traceback (most recent call last):
File "C:\Program Files\eclipse\plugins\org.python.pydev_2.7.5.2013052819\pysrc\pydevd_resolver.py", line 182, in _getPyDictionary
attr = getattr(var, n)
ValueError: NULL pointer access
我做错了什么?我在Windows上使用Python 3.3.2。要澄清指针和数组之间的区别,请阅读comp.lang.c常见问题解答,问题6.2: 您正在从DLL中的数据创建指针。显然,数据以4个空字节(32位Python)或8个空字节(64位Python)开头。改用数组:
# for a length n array
PciAgentIndex = (c_uint32 * n).in_dll(api, "PciAgentIndex")
如果需要指针,还可以强制转换函数指针:
PciAgentIndex = cast(api.PciAgentIndex, POINTER(c_uint32))
ctypes数据对象具有指向关联C数据缓冲区的指针。指针的缓冲区是4字节还是8字节,这取决于Python是32位还是64位。数组的缓冲区是元素大小乘以长度
# for a length n array
PciAgentIndex = (c_uint32 * n).in_dll(api, "PciAgentIndex")
如果需要指针,还可以强制转换函数指针:
PciAgentIndex = cast(api.PciAgentIndex, POINTER(c_uint32))
ctypes数据对象具有指向关联C数据缓冲区的指针。指针的缓冲区是4字节还是8字节,这取决于Python是32位还是64位。数组的缓冲区是元素大小乘以长度