2012-02-04 98 views
5

什麼是正確的方式(我越來越seg.fault)到uint8緩衝器發送到C函數定義爲:蟒蛇ctypes的UINT8緩衝區的使用情況

void f(uint8* p, size_t len) 

_lib.f.argtypes=[ctypes.POINTER(ctypes.c_ubyte), ctypes.c_uint] 
+0

哪裏數據你實際上是試圖發送?它告訴benice看看你迄今爲止所嘗試過的。 – jsbueno 2012-02-04 20:33:06

+0

我試過(cbytes.c_ubyte * 256)() – mete 2012-02-04 20:56:38

回答

2

通過你把方式創建的數組對象你的評論 - (cbytes.c_ubyte * 256)()都很好。 但在ctypes「專業術語」中,此對象不等同於ctypes.POINTER(ctypes.c_ubyte) - 當您嘗試以數組作爲參數調用函數時,ctypes應警告您。無論如何,可能當你將創建的數組作爲參數傳遞時,ctypes不會傳遞對其內容的引用,而是對C(可能是Python對象的地址,而不是其內容)的其他內容。

如果你投的一個指針數組來創建的指針類型的內容,它可能會工作:

pointer_type = ctypes.POINTER(ctypes.c_ubyte) 
_lib.f.argtypes=[pointer_type), ctypes.c_uint] 
data_block = (ctypes.c_ubyte * 256)() 
pointer = ctypes.cast(ctypes.addressof(data_block, pointer_type)) 
_lib.f(pointer, 256) 
+0

我會試試這個。除了ctypes數組之外,還有其他方法可以創建這樣的緩衝區嗎? – mete 2012-02-05 08:48:36

+0

我意識到seg.fault是由於別的。現在我直接發送緩衝區作爲參數,並沒有問題。 – mete 2012-02-06 15:12:33