Python ctypes sending a pointer to a structure as a parameter for the built-in library

I am trying to write a shell in my native library on Linux. The problem is this:

definition in c:

int mymethod(mystruct* ptr)

in python:

_lib.mymethod.argtypes = (ctypes.POINTER(mystruct),)
_lib.mymethod.restype = ctypes.c_int

s = mystruct()

_lib.mymethod(ctypes.byref(s))
raises: expected LP_mystruct instance instead of pointer for mystruct

_lib.mymethod(ctypes.pointer(s))
calls the expected instance of LP_mystruct instead of LP_mystruct

mistakes. How to pass a structure as a pointer to its own method?

Thank.

Meta

+5
source share
2 answers

, "POINTER" ctypes Python , " " (ctypes.CArgObject by ctypes.byref), , , ( , ctype adrresof) - , `ctypes.c_voidp, _lib.mymethod(ctypes.addressof(a)) -

, , , Python ( Python - , C-, Python), "", POINTER , :

mystruct_pointer = ctypes.POINTER(mystruct)
_lib.mymethod.argtypes = (mystruct_pointer,)
_lib.mymethod.restype = ctypes.c_int

s = mystruct()

_lib.mymethod(mystruct_pointer.from_address(ctypes.addressof(s)))
+5

( , , , - , .)

ctypes , byref(), :

ctypes byref(), . pointer(), pointer() , -, byref(), - Python.

, (, ) - argtypes , , . , ctypes mystruct, () , . , , pointer(), byref() POINTER()() - ctypes , () .

, , assert(_lib.mymethod.argtypes[0]._type_ == type(s)) .

+3

All Articles