tags:

views:

29

answers:

1

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.)

+1  A: 

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')
Aaron Gallagher