Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ctypes, function returning pointer to a structure

Tags:

python

ctypes

My C code is returning a pointer to the structure, this is how I have defined it in python

class CONTEXT(ctypes.Structure):
  _fields_ = [
                ("sockfd", ctypes.c_int), 
                ("eidSeq", ctypes.c_longlong)
             ]
# API
# connect
PY_connect=NativeDll.gf_connect
# connect input and output parameter declaration
PY_connect.argtype = [ 
                          ctypes.c_char_p, 
                          ctypes.c_char_p, 
                          ctypes.POINTER(ctypes.c_int)
                        ]
PY_connect.restype = [
                          ctypes.POINTER(CONTEXT)
                        ]

But I am getting following error for the restype

TypeError: restype must be a type, a callable, or None

like image 616
Avinash Avatar asked Sep 20 '25 22:09

Avinash


1 Answers

As DaveP already correctly guessed in comments, restype must not be a list of types.

PY_connect.restype = ctypes.POINTER(CONTEXT)

Note also that argument types are set by the argtypes attribute, not argtype.

like image 85
Janne Karila Avatar answered Sep 22 '25 11:09

Janne Karila