[英]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.