python中的记录指针_如何从Python中的指针地址读取?

我的python脚本加载了so库,以便从c+.so获得图像缓冲区地址。获得缓冲区地址后,需要能够读取缓冲区中的每个字节。我用“从_“地址”以创建列表对象:imageBytes = list(c_ubyte * dataSize).from_address(pointer)

下面将详细说明如何获取从c+传递给Pyth的内存地址,以及如何访问python端的内存数据。在c+代码framework Provider.cpp中:dataPackPtr = new DataPack();

DataPack * getFrame(){

uint32_t width = 1920;

uint32_t height = 1208;

const size_t buffersize = width * height * 4;//rgba, each color is one byte

unsigned char* rgbaImage = (unsigned char * )malloc(buffersize);

memset(rgbaImage, 0, buffersize); // set all the buffer data to 0.

dataPackPtr->width = width;

dataPackPtr->height = height;

dataPackPtr->buffersize = buffersize;

dataPackPtr->bufferPtr = rgbaImage;

return dataPackPtr;

}

extern "C" {

DataPack* getFrame_wrapper(){

return getFrame();

}

}

My 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])

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值