带有函数和结构指针的ctypes中出错 我尝试用Python使用C++函数(在DLL中)。要做到这一点,我使用库 我的C++代码是使用A导出一组C函数的库。
这是我想要使用的函数:带有函数和结构指针的ctypes中出错 我尝试用Python使用C++函数(在DLL中)。要做到这一点,我使用库 我的C++代码是使用A导出一组C函数的库。,python,c++,c,ctypes,Python,C++,C,Ctypes,这是我想要使用的函数: /*! 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 *hGr
/*! 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.
调用函数:
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.\u grabber\u handle))
),有时release函数接受对象,但有时会给出此错误:
_dllref.IC_ReleaseGrabber(ctypes.byref(_grabber_handle))
WindowsError: exception: access violation reading 0x5A694F44
您可以设置
IC\u CreateGrabber
的返回类型,以便在调用IC\u 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))
通过设置库函数的
restype
和argtypes
,ctypes知道如何处理C端的值。您不需要为HGRABBER\u t
定义\u字段,因为它只是用于创建不透明指针类型。好的,我将测试没有此字段的代码,谢谢!这个答案是正确的,但在我看来,重要的是要说明这个答案只解决了我问题的一部分。我犯了两个错误。我将编辑我的帖子来解释it@carlos.baez是的,这看起来是正确的。当我看原始问题时,我错过了HGRABBER*
。抱歉@ebarr,我认为它在工作,但不幸的是,它随机抛出一个指针异常异常:访问冲突读取0x5A694F44
。。。。
_dllref.IC_ReleaseGrabber(ctypes.byref(_grabber_handle))
WindowsError: exception: access violation reading 0x5A694F44
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))