Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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
通过void*传递数组,并在python ctypes中获取数组_Python_Arrays_Ctypes - Fatal编程技术网

通过void*传递数组,并在python ctypes中获取数组

通过void*传递数组,并在python ctypes中获取数组,python,arrays,ctypes,Python,Arrays,Ctypes,我正在使用ctypes包装一个C库,以使其在Python(3)中可用。我是Python和ctypes的初学者 其中一个函数将指向数组的void*作为参数,并填充它。我认为数据是整数,但文档非常糟糕,所以我无法确认。我可能想尝试其他类型,但我在这里尝试了c_int MY_API ULONG GetImage ( DWORD width, DWORD height, void * lpRawData); 我需要使用Python访问存储在这个数组中的值。 到目前为止,我已经尝试像这样配置argtyp

我正在使用ctypes包装一个C库,以使其在Python(3)中可用。我是Python和ctypes的初学者

其中一个函数将指向数组的
void*
作为参数,并填充它。我认为数据是整数,但文档非常糟糕,所以我无法确认。我可能想尝试其他类型,但我在这里尝试了
c_int

MY_API ULONG GetImage ( DWORD width, DWORD height, void * lpRawData);
我需要使用Python访问存储在这个数组中的值。 到目前为止,我已经尝试像这样配置argtypes,但是失败了

from ctypes import windll, c_int, POINTER, byref
from ctypes.wintypes import DWORD, ULONG

# Height and Width are known
width = 47
height = 45

# Loading the Library
self._lib = windll.LoadLibrary('lib/myLib.dll')

# Building the reference to the C function
self.CGetImage = self._lib.GetImage
self.CGetImage.argtypes = (DWORD, DWORD, POINTER(c_int * (width*height)) )
self.CGetImage.restype = ULONG

# Creating the array storing the data
rawData = c_int * (width * height)

self.CGetImage( DWORD(width), DWORD(height), byref(rawData) )
返回的
byref参数必须是ctypes实例,而不是“\u ctypes.PyCArrayType”

不幸的是,我是一个初学者,我在其他SO帖子的ctypes文档中发现的东西,通常都是关于
numpy
库的。通常在这些文章中,C API中定义的类型也比
void*
明确得多,并且通常作为返回值而不是参数(尽管我想它不会改变解决此问题的方式)


我如何才能从我的C库中获取这些数据,以便用Python将它们作为int或double进行操作?

我对ctypes也不太了解,但你不能使用
C\u void\p
;稍后您必须找到一种方法将其转换为适当的数据类型,但我想最初您必须使用
c\u void\u p
进行设置。顺便问一下,您是否在
中缺少argtype。argtypes
中缺少argtype。也就是说,它应该是
[DWORD,DWORD,c\u void\u p]
吗?我就是这么想的。但是我必须首先找到一种方法来定义数组的大小!我会使用
c\u void\u p
来存储句柄。我认为您只需要传入void指针。如何在Python中使用void指针取决于您。在Python中,您需要尊重宽度和高度的概念,以及在is-Python时如何访问、更改或转换void指针取决于您自己。但是,
ctypes
在与C代码交互时只需要知道它是一个空指针。C不知道空指针指向什么类型或数据,因此在与C代码交互时,您不需要向
ctypes
表达这些想法。在Python中访问或修改数据时,您只需要表达这些想法。不要在
argtypes
中过度指定指向数组子类的指针;使用通用的
int*
。错误本身是因为需要创建数组类型的实例,即
CGetImage.argtypes=(DWORD,DWORD,POINTER(c_int))
rawData=(c_int*(宽度*高度))
result=CGetImage(宽度、高度、原始数据)