lispとrubyとpythonと その4 Cライブラリの呼び出し(python) の追記
lispとrubyとpythonと その4 Cライブラリの呼び出し(python) - テンポラリにfgshunさんからコメントをもらったので試してみた。
呼び出しているcライブラリはこんどもlispとrubyとpythonと その4 Cライブラリの呼び出し(lisp) - テンポラリと同じもの。
呼び出し先のcで確保した領域はcontentsに入ってくるけど、元の変数にセットされない。
分かるような、納得いかないような。
p = pp.contents
で自分でセットしなおしといた。
import ctypes from ctypes import * so = ctypes.CDLL("./libffi-test.so") so.f.argtypes = [c_int] so.f.restype = c_int def f(i): return so.f(i) print(f(5)) so.f0.argtypes = [c_char_p,c_int] so.f0.restype = c_int def f0(buf,len): return so.f0(buf,len) buf0 = create_string_buffer(10) f0(buf0,10) print(buf0.value) so.f1.argtype = [POINTER(c_char_p)] so.f1.restype = c_int def f1(buf): return so.f1(pointer(buf)) buf1 = c_char_p() f1(buf1) print(buf1.value) so.f2.restype = c_char_p def f2(): return so.f2() print(f2()) class Person(Structure): _fields_ = [("name",c_char_p), ("age",c_int)] so.f3.argtype = [POINTER(c_void_p),c_int] so.f3.restype = c_int def f3(person): person.name = cast(create_string_buffer(16),c_char_p) return so.f3(pointer(person),16) p1 = Person() f3(p1) print(p1.name) print(p1.age) so.f4.argtype = [POINTER(POINTER(c_void_p)),c_int] so.f4.restype = c_int def f4(person): return so.f4(person) p = Person() pp = pointer(p) ppp = pointer(pp) f4(ppp) print("ppp:" + ppp.contents.contents.name) print("pp:" + pp.contents.name) #print("p:" + p.name) p = pp.contents print("p:" + p.name) #->ppp:名前いいい # pp:名前いいい # p:名前いいい