我有這段C 代碼可以將基于類Foo的代碼集成到 python 中。
class Foo{
public:
Foo(){};
int do_thing(int arg){ return arg*2; }
};
extern "C" {
Foo* get_foo_obj(){
return new Foo;
}
int do_thing(Foo* ptr, int arg){
return ptr->do_thing(arg);
}
}
現在我想為 python 中的函式分配argtypes和restype。
lib = ctypes.CDLL("mylib.so")
lib.get_foo_obj.restype = <POINTER?>
lib.do_thing.argtypes = (<POINTER?>, c_int)
lib.do_thing.restype = c_int
ctypes我需要在這里使用什么是正確的?
uj5u.com熱心網友回復:
ctypes.c_void_p有效(void*在 C 中),盡管您可以使用不透明指標型別更安全,例如:
import ctypes as ct
class Foo(ct.Structure):
pass
lib = ct.CDLL('mylib.so')
lib.get_foo_obj.argtypes = ()
lib.get_foo_obj.restype = ct.POINTER(Foo)
lib.do_thing.argtypes = ct.POINTER(Foo), ct.c_int
lib.do_thing.restype = ct.c_int
foo = lib.get_foo_obj()
print(lib.do_thing(foo, 5))
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/444622.html
上一篇:什么是C 中的別名?
下一篇:Tabview滾動行為
