Byte arrays and DLLs
Eryk Sun
eryksun at gmail.com
Thu Jun 30 18:45:49 EDT 2022
On 6/30/22, Rob Cliffe via Python-list <python-list at python.org> wrote:
>
> AKAIK it is not possible to give ctypes a bytearray object and persuade
> it to give you a pointer to the actual array data, suitable for passing
> to a DLL.
You're overlooking the from_buffer() method. For example:
>>> ba = bytearray(10)
>>> ca = (ctypes.c_char * len(ba)).from_buffer(ba)
>>> ca.value = b'spam&eggs'
>>> ba
bytearray(b'spam&eggs\x00')
Note that the bytearray can't be resized while a view of the data is
exported. For example:
>>> ba.append(97)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
BufferError: Existing exports of data: object cannot be re-sized
>>> del ba[-1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
BufferError: Existing exports of data: object cannot be re-sized
More information about the Python-list
mailing list