C_void_p arguments in callbacks using ctypes in Python

I am a frequent user of ctypes, but I came across some things that I cannot explain.

First, when I define the callback using c_void_p, the callback will get a flat integer.

I saw several other posts mentioning wrapping c_void_p in another POINTER when it came to using c_void_p for the return value. So, I thought I could try this for an argument in the definition of a callback: POINTER (c_void_p). I get a pointer when calling a callback, but I still don't know if it points to given values.

Since C_void_p points to a binary data buffer, I don’t think I can use c_char_p. I passed an array of unsigned-chars with the length that I am passing, but then Python tells me that I have a tuple and there is no “content” or “value” property that I can use.

Suggestions?

+4
source share
1 answer

c_void_p . getfunc - , c_void_p Structure. ctypes - , type('my_void_p', (c_void_p,), {}). , .

, p. c_char n, *:

buf = cast(p, POINTER(c_char * n))[0]

# 2.6+
buf = (c_char * n).from_address(p)

, , a char *, POINTER(c_char). , . p[0] p[:n].


* p[n] *(p + n), * , . p.contents p[0], getfunc, .

+6

Source: https://habr.com/ru/post/1537878/


All Articles