我正在尝试导入一个C函数,它返回缓冲区地址(unsgined char *)。 Python是否可以采用POINTER(c_ubyte)来访问dll中分配的内存 没有在Python中执行任何额外的内存复制或分配移动? 似乎没有必要通过Python重新分配这样的内存空间,因为它已经存在 由导入的* .dll。
创建以下是dll文件中的简化C代码:
static char gp_huge_buffer[1024][1024];
unsigned char *GetBufferAddress(void){
//This function will modify gp_huge_buffer data for Python
return (unsigned char *)gp_huge_buffer;
}
我的Python脚本:
import ctypes
mydll = ctypes.cdll.LoadLibrary('my dll path')
mydll.GetBufferAddress.argtypes = []
mydll.GetBufferAddress.restype = ctypes.POINTER(ctypes.c_ubyte)
# Maybe doing something as blow .... I don't know ???
p_ubyte = mydll.GetBufferAddress()
bytes_from_dll = byte_array.from_address(p_ubyte)
# No additional memory allocation by Python, just transform bytes_from_dll to data_from_dll?
data_from_dll = SomeTypeCast(bytes_from_dll)
with open('file.dat', 'r') as fd
data_from_fd = fd.Read()
if data_from_dll == data_from_fd :
print("I need to compare the two group of huge binary data")
print("Especially one is in file.dat and the other is in *.dll")