2012-04-26 43 views
1

函数来自一个DLL(C语言)link("parameters", &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)

为什么呢?

+0

你确定你应该寻找在'result'而不是'status'? – yak 2012-04-26 13:40:36

+0

'connection_t'是'ctypes.c_uint32',但第一个'func.argtypes'是'ctypes.c_ulong'? – martineau 2012-04-26 16:56:59

+0

牦牛,对不起,我的意思是'身份'的价值。但是如果'状态'不正确,'结果'值也不正确。 – KLM 2012-04-26 19:04:03

回答

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)) 
相关问题