我碰巧也在研究类似的问题。我的python脚本load.so库从c++.so获取图像缓冲区地址。在我得到缓冲区地址之后,我需要能够读取缓冲区中的每个字节。我用“from_address”创建了一个列表对象:imageBytes = list(c_ubyte * dataSize).from_address(pointer)
下面详细介绍了如何获取从c++到pyth的内存地址,以及如何在python端访问内存数据。在c++代码中框架提供者.cpp公司名称:
^{pr2}$
我的Python:import ctypes
import binascii
lib = ctypes.cdll.LoadLibrary('/libpath/frameprovider.so')
print vars(lib)
class dataPack(ctypes.Structure):
_fields_ = [("width",ctypes.c_int),
("height",ctypes.c_int),
("buffersize",ctypes.c_int),
("bufferAddress", ctypes.c_void_p)]
lib.getFrame_wrapper.restype = ctypes.POINTER(dataPack)
data = lib.getFrame_wrapper()
print "in python the w= ", data.contents.width, "h=",data.contents.height
print "the buffersize=",data.contents.height
imageBytes = list(
(data.contents.buffersize * ctypes.c_ubyte).
from_address(data.contents.bufferAddress))
print "the len of imageBytes are ", len(imageBytes)
print imageBytes[data.contents.buffersize -1] #print the last byte in the buffer
print "in python, the hex value of element 12 is ", hex(imageBytes[12])