ctypes: return a pointer to a struct

sturlamolden sturlamolden at yahoo.no
Thu Apr 24 23:27:32 EDT 2008


On Apr 25, 5:15 am, sturlamolden <sturlamol... at yahoo.no> wrote:

> First define a struct type IP2LocationRecord by subclassing from
> ctypes.Structure. Then define a pointer type as
> ctypes.POINTER(IP2LocationRecord) and set that as the function's
> restype attribute. See the ctypes tutorial or reference for details.

Which is to say:

import ctypes

class IP2LocationRecord(ctypes.Structure):
	_fields_ = [
		('country_short', ctypes.c_char_p),
		('country_long', ctypes.c_char_p),
		('region', ctypes.c_char_p),
		('city', ctypes.c_char_p),
		('isp', ctypes.c_char_p),
		('latitude', ctypes.c_float),
		('longitude', ctypes.c_float),
		('domain', ctypes.c_char_p),
		('zipcode', ctypes.c_char_p),
		('timezone', ctypes.c_char_p),
		('netspeed', ctypes.c_char_p),
   	]

IP2LocationRecord_Ptr_t = ctypes.POINTER(IP2LocationRecord)

function.restype = IP2LocationRecord_Ptr_t





More information about the Python-list mailing list