简体   繁体   English

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

[英]passing object as an argument of function

There is a function from a DLL (C language) link("parameters", &connection); 有一个来自DLL(C语言) link("parameters", &connection);的函数link("parameters", &connection); which takes a string parameter and initializes a connection. 它采用字符串参数并初始化连接。

There is a function connect(connection) , where connection is the object initialized with a call to link() . 有一个函数connect(connection) ,其中connection是通过调用link()初始化的对象。

I pass Python connection object to function connect() as an argument 我将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))
...

But if I transfer the 'connection' object to any other function of mydll library, the function returns a value, but the value is incorrect. 但是,如果我将“连接”对象转移到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))

In this example result=0 , but in the C-variant of this code I receive a correct value (not 0) 在此示例中, result=0 ,但是在此代码的C变量中,我收到了正确的值(不是0)

Why? 为什么?

Based on your comment describing the C apis: 根据您描述C API的评论:

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

Since func takes a pointer to a connection type, the code should be something like: 由于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