Python Bytes to CTypes Void Pointer

I.F. Adams

I have a c function that takes as arguments a void * pointer and an integer length for the size of the buffer pointed to.

e.g.

char* myfunc(void *mybuffer, int buflen)

On the python side I have a bytes object of binary data read from a file.

What I am trying to figure out is the right conversions to be able to call the c function from python, and am struggling a bit.

I understand the conversions for dealing with simple string data (e.g. encoding to utf-8 and using a char_p type) but dealing with a bytes object has been a bit of a struggle....

Thanks in advance!

Mark Tolonen

Given your commented description, you can just use the obvious types if you don't need to free the returned char* memory. You can pass a bytes object to a void*. Here's a quick demo:

test.c

#include <stdio.h>
#include <stdint.h>

#ifdef _WIN32
#   define API __declspec(dllexport)
#else
#   define API
#endif

API char* myfunc(void *mybuffer, int buflen) {
    const uint8_t* tmp = (const uint8_t*)mybuffer;
    for(int i = 0; i < buflen; ++i) // show the passed bytes
        printf("%02X\n", tmp[i]);
    return "output"; // static string no deallocation required
}

test.py

import ctypes as ct
import os

dll = ct.CDLL('./test')
dll.myfunc.argtypes = ct.c_void_p, ct.c_int
dll.myfunc.restype = ct.c_char_p

buf = bytes([1,2,0,0xaa,0x55])  # including embedded null
ret = dll.myfunc(buf, len(buf))
print(ret)

Output:

01
02
00
AA
55
b'output'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

python ctypes - wrap void pointer

From Dev

Saving a void pointer in an array of bytes

From Dev

Python ctypes throws TypeError: one character bytes, bytearray or integer expected for functions with pointer references

From Dev

Python ctypes pointer and allocated memory

From Dev

Python Ctypes passing pointer for data

From Dev

Pointer argument passing in python ctypes

From Java

Numpy error when converting array of ctypes types to void pointer

From Dev

Declare NULL void pointer locally and pass it to API in ctypes

From Dev

convert c char* to python bytes with ctypes

From Java

how to specify a JNR Pointer like that of python ctypes

From Dev

Python Ctypes Double De-reference Pointer

From Dev

How to cast a ctypes pointer to an instance of a Python class

From Dev

Python ctypes arguments with DLL - pointer to array of doubles

From Dev

Python ctypes passing string pointer / buffer

From Dev

Why it is possible to modify immutable bytes object using ctypes in python 3?

From Dev

Ctypes pointer on element in ctypes array

From Dev

Converting Python instance to ctypes.c_void_p

From Dev

Convert multiprocessing.Array to ctypes.c_void_p in Python

From Dev

python: ctypes, read POINTER(c_char) in python

From Dev

How do you go from a sip.voidptr (QImage.constBits()) to a ctypes void or char pointer?

From Dev

How to convert ctypes to bytes

From Dev

How to dereference void* in ctypes?

From Dev

Python ctypes: how to define a callback having a buffer pointer and a length in argument?

From Dev

Ctypes: fast way to convert a return pointer to an array or Python list

From Dev

Mallocing and Freeing in C, but passing the pointer through Python via ctypes

From Dev

expected LP_SHFILEOPSTRUCTW instance instead of pointer to SHFILEOPSTRUCTW (python ctypes)

From Dev

Update values of a struct pointer in ctypes python without using a C function

From Dev

Python ctypes pass pointer in structure field to Fortran derived types

From Dev

How to get value of char pointer from Python code wtih ctypes

Related Related

  1. 1

    python ctypes - wrap void pointer

  2. 2

    Saving a void pointer in an array of bytes

  3. 3

    Python ctypes throws TypeError: one character bytes, bytearray or integer expected for functions with pointer references

  4. 4

    Python ctypes pointer and allocated memory

  5. 5

    Python Ctypes passing pointer for data

  6. 6

    Pointer argument passing in python ctypes

  7. 7

    Numpy error when converting array of ctypes types to void pointer

  8. 8

    Declare NULL void pointer locally and pass it to API in ctypes

  9. 9

    convert c char* to python bytes with ctypes

  10. 10

    how to specify a JNR Pointer like that of python ctypes

  11. 11

    Python Ctypes Double De-reference Pointer

  12. 12

    How to cast a ctypes pointer to an instance of a Python class

  13. 13

    Python ctypes arguments with DLL - pointer to array of doubles

  14. 14

    Python ctypes passing string pointer / buffer

  15. 15

    Why it is possible to modify immutable bytes object using ctypes in python 3?

  16. 16

    Ctypes pointer on element in ctypes array

  17. 17

    Converting Python instance to ctypes.c_void_p

  18. 18

    Convert multiprocessing.Array to ctypes.c_void_p in Python

  19. 19

    python: ctypes, read POINTER(c_char) in python

  20. 20

    How do you go from a sip.voidptr (QImage.constBits()) to a ctypes void or char pointer?

  21. 21

    How to convert ctypes to bytes

  22. 22

    How to dereference void* in ctypes?

  23. 23

    Python ctypes: how to define a callback having a buffer pointer and a length in argument?

  24. 24

    Ctypes: fast way to convert a return pointer to an array or Python list

  25. 25

    Mallocing and Freeing in C, but passing the pointer through Python via ctypes

  26. 26

    expected LP_SHFILEOPSTRUCTW instance instead of pointer to SHFILEOPSTRUCTW (python ctypes)

  27. 27

    Update values of a struct pointer in ctypes python without using a C function

  28. 28

    Python ctypes pass pointer in structure field to Fortran derived types

  29. 29

    How to get value of char pointer from Python code wtih ctypes

HotTag

Archive