简体   繁体   中英

How do I create a Python ctypes pointer to an array of pointers

I need to learn how to handle the char** in the C++ method below through Python ctypes. I was doing fine calling other methods that only need single pointers by using create_string_buffer() , but this method requires a pointer to an array of pointers.

ladybugConvertToMultipleBGRU32(
         LadybugContext          context,
        const  LadybugImage * pImage,
        unsigned char**        arpDestBuffers,
         LadybugImageInfo *   pImageInfo  )

How do I create a pointer to an array of six create_string_buffer(7963648) buffers in ctypes to pass to this C++ method for writing?

arpDestBuffers = pointer to [create_string_buffer(7963648) for i in xrange(6)]

Thank you for any help.


Both the answers given below work. I just didn't realize I had another problem in my code which prevented me from seeing the results right away. The first example is just as Luc wrote:

SixBuffers = c_char_p * 6
arpDestBuffers = SixBuffers(
            *[c_char_p(create_string_buffer(7963648).raw) for i in xrange(6)] )

The second example coming from omu_negru's answer is:

arpDestBuffers = (POINTER(c_char) * 6)()
arpDestBuffers[:] = [create_string_buffer(7963648) for i in xrange(6)]

Both are accepted by the function and overwritten. Typing print repr(arpDestBuffers[4][:10]) before and after calling the function gives:

'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
'\x15\x10\x0e\xff\x15\x10\x0e\xff\x14\x0f'

which shows that the function successfully overwrote the buffer with data.

Maybe Something like

SixBuffers = c_char_p * 6
arpDestBuffers = SixBuffers(*[c_char_p(create_string_buffer(7963648).raw) for i in xrange(6)])

Didin't try myself, so not sure that it works. Inspired by http://python.net/crew/theller/ctypes/tutorial.html#arrays

after you create all the string_buffers you need by calling create_string_buffer you can create an array for them with :

var=(POINTER(c_char)*size)()  /* pointer_to_pointer */

Then you can fill it up by indexing it with var[index] and finally just call your function using it as an argument.... Works fine for me and i just tested it on a function with the signature void test_fn(char*,char**,unsigned char); written in C

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM