違い
- POINTER( ctypes型 ) ポインタ型を返す
- pointer( ctypes instance ) ポインタインスタンスを生成。参照渡しにも利用可能だが、重い処理。
- bytef( ctypes instance )
パラメータの参照渡し用の軽い処理。参照渡し用のポインタを返す。
例
from ctypes import *
pclass = POINTER(c_int)
p = pclass(c_int(24))
p.contents = c_int(12)
a = c_int(35)
b = pointer(a)
print( a.value )
c = c_int(40)
windll.user32.poi( byref(a), pointer(c) )
print( a.value, c.value )
関数ファクトリ
ia = (c_int * 5 )(5,3,7,99,1)
qsort = libc.qsort
qsort.restype = None
@CFUNCTYPE(c_int, POINTER(c_int), POINTER(c_int))
def py_cmp_func(a, b):
print("py_cmp_func", a[0], b[0])
return a[0] - b[0]
qsort(ia, len(ia), sizeof(c_int), py_cmp_func)