繁体   English   中英

将对象作为函数的参数传递

[英]passing object as an argument of function

有一个来自DLL(C语言) link("parameters", &connection);的函数link("parameters", &connection); 它采用字符串参数并初始化连接。

有一个函数connect(connection) ,其中connection是通过调用link()初始化的对象。

我将Python连接对象传递给函数connect()作为参数

connection_t = ctypes.c_uint32
link = mydll.link
link.argtypes=(ctypes.c_char_p, ctypes.POINTER(connection_t) )
connect = mydll.connect
connect.argtypes=(connection_t,)
...
connection = connection_t()
link ("localhost: 5412", ctypes.byref(connection))
...

但是,如果我将“连接”对象转移到mydll库的任何其他函数中,则该函数返回一个值,但该值不正确。

func=mydll.func
status_t=ctypes.c_uint32
status=status_t()
func.argtypes=(ctypes.c_ulong,ctypes.POINTER(status_t))
result=func(connection, ctypes.byref(status))

在此示例中, result=0 ,但是在此代码的C变量中,我收到了正确的值(不是0)

为什么?

根据您描述C API的评论:

link(const char* set, conn_type* connection );
func(conn_type* connection, uint32_t* status);

由于func使用指向连接类型的指针,因此代码应类似于:

mydll=ctypes.CDLL('mydll')
connection_t = ctypes.c_uint32
link = mydll.link
link.argtypes=(ctypes.c_char_p, ctypes.POINTER(connection_t) )
connection = connection_t()
link("localhost: 5412", ctypes.byref(connection))

func=mydll.func
status_t=ctypes.c_uint32
status=status_t()
func.argtypes=(ctypes.POINTER(connection_t),ctypes.POINTER(status_t))
result=func(ctypes.byref(connection), ctypes.byref(status))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM