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

I need to learn how to handle char** in the C ++ method below using Python types. I did great calls to other methods that only need single pointers 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 to create a pointer to an array of six create_string_buffer(7963648) buffers in ctypes to pass this C ++ method to write?

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

Thanks for any help.


Both of the answers below work. I just did not understand that I had another problem in my code that prevented me from immediately seeing the results. The first example is the same as Luke wrote:

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

Second example coming from omu_negru answer:

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

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

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

which indicates that the function has successfully overwritten the buffer with data.

+4
source share
2 answers

after creating all the var=(POINTER(c_char)size)() / pointer_to_pointer */ you need by calling create_string_buffer, you can create an array for them: var=(POINTER(c_char)size)() / pointer_to_pointer */

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

0
source

Maybe something like

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

Do not try, so not sure if it works. Inspired by http://python.net/crew/theller/ctypes/tutorial.html#arrays

+1
source

Source: https://habr.com/ru/post/1393310/


All Articles