传递对象作为函数的参数

2024-03-28 19:29:12 发布

您现在位置:Python中文网/ 问答频道 /正文

DLL(C语言)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))
...

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

^{pr2}$

在这个例子中result=0,但是在这个代码的C变体中,我收到了一个正确的值(不是0)

为什么?在


Tags: 对象函数字符串参数connectlinkconnectionctypes
1条回答
网友
1楼 · 发布于 2024-03-28 19:29:12

根据您对C API的描述:

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

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

^{pr2}$

相关问题 更多 >