# c_byte: 2 bytes # c_short: 4 bytes # c_int: 8 bytes # c_long: 16 bytes import ctypes import os.path filename = os.path.abspath('libpoint.so') CoordType = ctypes.c_long class Point(ctypes.Structure): _fields_ = ( ('x', CoordType), ('y', CoordType), ) def __repr__(self): return f'' libpoint = ctypes.cdll.LoadLibrary(filename) point_test = libpoint.point_test point_test.argtypes = () point_test.restype = None point_new = libpoint.point_new point_new.argtypes = (CoordType, CoordType) point_new.restype = Point point_reset_copy = libpoint.point_reset_copy point_reset_copy.argtypes = (Point,) point_reset_copy.restype = None point_reset_ref = libpoint.point_reset_ref point_reset_ref.argtypes = (ctypes.POINTER(Point),) point_reset_ref.restype = None p = point_new(1, 2) print(f"p = {p!r}") point_reset_copy(p) print(f"p = {p!r}") assert p.x == 1 and p.y == 2 point_reset_ref(p) print(f"p = {p!r}") assert p.x == 0 and p.y == 0 point_test() print("sizeof(Point) = %s bytes" % ctypes.sizeof(p))