对这就是为什么
ctypes
支架
defining your own structs
,并定义函数的原型。
您需要对结构进行Python级别的定义,例如:
from ctypes import Structure, c_int, POINTER
class iperf_test(Structure):
_fields_ = [("server_port", c_int),
("bind_port", c_int)]
然后,在调用C函数之前
set its
restype
正确地:
# Load library as normal
self.lib = cdll.LoadLibrary("lib.so")
# New, so Python knows how to interpret result
self.lib.iperf_new_test.restype = POINTER(iperf_test)
# Call, and Python returns pointer to Python definition of struct
self._test = self.lib.iperf_new_test()
现在你可以用了
by dereferencing
[0]
因为Python缺少
*
指针解引用运算符)并直接在解引用的结构上设置属性:
self._test[0].bind_port = new_bind_port