Skip to content
Advertisement

Pointer argument passing in python ctypes

I have the following c function.

JavaScript

This function returns outbuf, the output length is unknown before calling the function so the function receives a pointer to the length as an argument outbuf_len, also the caller is responsible to free outbuf.

I want to get the result of this function from python, so I started writing the following code:

JavaScript

The problems i have is:

  1. I didn’t find pointer to uint in ctypes types, so how can I pass the outbuf_len pointer to the C function?
  2. when printing the outbuf, only the first 4 bytes that are pointed by the pointer are printed.
  3. How do I free() the outbuf buffer from python?

I have the source of the C function so it is possible to change how arguments are passed the the C function.

Thanks.

Advertisement

Answer

If you’ll be passing Python byte strings as the input buffer, here’s a way to do it. I made a minimal example of the C call:

test.c

JavaScript

test.py

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement