开发者

Ctypes Offset Into A Buffer

I have a string buffer: b = create_string_buffer(numb) where numb is a number of bytes.

In my wrapper I need to splice up this buffer. When calling a function that expects a 开发者_开发知识库POINTER(c_char) I can do: myfunction(self, byref(b, offset)) but in a Structure:

 class mystruct(Structure):
    _fields_ = [("buf", POINTER(c_char))]

I am unable to do this, getting an argument type exception. So my question is: how can I assign .buf to be an offset into b. Direct assignment works so .buf = b, however this is unsuitable. (Python does not hold up to well against ~32,000 such buffers being created every second, hence my desire to use a single spliced buffer.)


ctypes.cast

>>> import ctypes
>>> b = ctypes.create_string_buffer(500)
>>> b[:6] = 'foobar'
>>> ctypes.cast(ctypes.byref(b, 4), ctypes.POINTER(ctypes.c_char))
<ctypes.LP_c_char object at 0x100756e60>
>>> _.contents
c_char('a')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜