Byte arrays and DLLs

Rob Cliffe rob.cliffe at btinternet.com
Sun Jul 3 05:32:09 EDT 2022


That worked.  Many thanks Eryk.
Rob

On 30/06/2022 23:45, Eryk Sun wrote:
> 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