ctype problem

Nick Craig-Wood nick at craig-wood.com
Wed Jan 14 08:32:07 EST 2009


Grimson <grimson at gmx.de> wrote:
>  hello out there,
>  I have a problem with c-types.
>  I made a c-library, which expects a pointer to a self defined structure.
> 
>  let the funtion call myfunction(struct interface* iface)
> 
>  and the struct:
>  struct interface
>  {
>      int a;
>      int b;
>      char *c;
>  }
> 
>  the Python ctype port of this structur would be:
> 
>  class INTERFACE(Structure):
>      _fields_ = [("a"         c_int),
>                        ("b",        c_int),
>                        ("c",         c_char)]
> 
>  in my python-struct a create a instance of INTERFACE
> 
>  myiface = INTERFACE()
>  myiface.a = ctypes.c_int(80)
>  myiface.b = ctypes.c_int(22)
>  ...
>  than I make a pointer onto it.
>  p_iface = ctypes.pointer(myiface)
>  and I tried it also with a reference
>  r_iface = ctypes.byref(myiface)
> 
>  but neither myclib.myfunction(p_iface) nor myclib.myfunction(r_iface)
>  works properly. The function is been called but it reads only zeros (0)
>  for each parameter (member in the struct).
> 
>  Where is my fault?

You didn't (or you didn't show) defining the argument types of the
function.

myclib = CDLL("myclib.so") # or whatever
myclib.myfunction.argtypes = [ POINTER(INTERFACE) ]
myclib.myfunction.restype = c_int # or whatever

If you do that then you should be able to pass in myiface directly or
byref(myiface).

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list