簡體   English   中英

具有函數和結構指針的ctypes中的錯誤

[英]Error in ctypes with a function and a structure pointer

我試圖將C ++函數(在dll中)與python一起使用。 為此,我使用ctypes庫。

我的C ++代碼是使用網絡攝像頭的庫,該攝像頭可導出一組C函數。

我要使用的功能:

/*! Release the grabber object. Must be called, if the calling application
    does no longer need the grabber.
    @param hGrabber The handle to grabber to be released.
    @sa IC_CreateGrabber
*/
void AC IC_ReleaseGrabber( HGRABBER *hGrabber ); ///< Releas an HGRABBER object.

是釋放記憶功能

這個HGRABBER結構:

//////////////////////////////////////////////////////////////////////////
/*! This is the handle of an grabber object. Please use the HGRABBER type to access
    this object.
*/
typedef struct HGRABBER_t__ { int unused; } HGRABBER_t; ///<Internal structure of the grabber object handle.
#define HGRABBER HGRABBER_t* ///< Type of grabber object handle. Used for all functions. 

我的代碼是:

必要的結構HGRABBER(在我的情況下為HGRABBER_TYPE)

class HGRABBER_T(ctypes.Structure):
    _fields_ = [("unused", ctypes.c_int)] 

HGRABBER_TYPE = ctypes.POINTER(HGRABBER_T)

調用函數:

self._dllref =  ctypes.windll.LoadLibrary(DLL_PATH)
self._grabber_handle = self._dllref.IC_CreateGrabber() 
 ....
 ...
 ....
self._dllref.IC_ReleaseGrabber(ctypes.pointer(HGRABBER_TYPE(self._grabber_handle)))

最后,我收到的錯誤:

self._dllref.IC_ReleaseGrabber(ctypes.byref(HGRABBER_TYPE(self._grabber_handle)))
TypeError: expected HGRABBER_T instead of int

我檢查了其他相關的帖子,例如這個 ,但它並沒有幫助我..

感謝您的幫助!

更新:

我應用了restype和argtypes來指定參數並返回值(謝謝!)。

經過修改,代碼為:

self._dllref =  ctypes.windll.LoadLibrary(DLL_PATH)
self._dllref.IC_CreateGrabber.restype = HGRABBER_TYPE        
self._grabber_handle = self._dllref.IC_CreateGrabber() 
...
..
self._dllref.IC_ReleaseGrabber.argtypes = [HGRABBER_TYPE]
self._dllref.IC_ReleaseGrabber(self._grabber_handle)

我應該有多個錯誤,現在我的錯誤是:

self._dllref.IC_ReleaseGrabber(self._grabber_handle)
WindowsError: exception: access violation writing 0x6E657137

我檢查了函數的參數(HGRABBER * hGrabber),release函數的argtypes應該是:

self._dllref.IC_ReleaseGrabber.argtypes = [ctypes.POINTER(HGRABBER_TYPE)]

通過此修改,我得到另一個不同的錯誤:

self._dllref.IC_ReleaseGrabber(self._grabber_handle)
WindowsError: exception: access violation reading 0x6B0F1FE0

我正在搜索這些錯誤,這似乎是我不理解的指針轉換錯誤,結構看起來非常簡單,並且我看不到我錯過的內容。

更新2

我在調用函數時錯過了添加ctypes.byref,它必須是:

 self._dllref.IC_ReleaseGrabber(ctypes.byref(self._grabber_handle))

更新3

不幸的是,我遇到與指針參數( (ctypes.byref(self._grabber_handle)) )相關的隨機錯誤,有時釋放函數接受該對象,但有時會出現以下錯誤:

    _dllref.IC_ReleaseGrabber(ctypes.byref(_grabber_handle))
WindowsError: exception: access violation reading 0x5A694F44

您可以設置IC_CreateGrabber的返回類型, IC_CreateGrabber在調用IC_ReleaseGrabber時不需要重鑄。

例如:

self._dllref =  ctypes.windll.LoadLibrary(DLL_PATH)

# here set the return type
self._dllref.IC_CreateGrabber.restype = HGRABBER_TYPE

# here set the argtypes
self._dllref.IC_ReleaseGrabber.argtypes = [ctypes.POINTER(HGRABBER_TYPE)]

self._grabber_handle = self._dllref.IC_CreateGrabber() 

self._dllref.IC_ReleaseGrabber(ctypes.byref(self._grabber_handle))

通過設置庫函數的restypeargtypes ,ctypes知道如何從C端處理值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM