简体   繁体   中英

Blob object for python (ctypes), C++

Hallo!

I want a blob object which I can pass around in python and from time to time give it to a C++ function to write to.

ctypes seems the way to go but I have problem with the python standard functions.

For example:

>>> import ctypes
>>> T=ctypes.c_byte * 1000
>>> blob = T()
>>> ctypes.pointer(blob)
<__main__.LP_c_byte_Array_1000 object at 0x7f6558795200>

# Do stuff with blob data through the pointer in C++

>>> f = open('test.bin', 'wb')
>>> f.write(blob)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument 1 must be string or buffer, not _ctypes.ArrayType

I would really like to avoid copying the data if not necessary.

Thanks

You'll probably have better luck with using string buffers and accessing the content through the raw attribute value

pstr = ctypes.create_string_buffer( 1000 )
f.write( pstr.raw )

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