简体   繁体   English

具有函数和结构指针的ctypes中的错误

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

I am trying to use C++ function (in a dll) with python. 我试图将C ++函数(在dll中)与python一起使用。 To do this, i use ctypes library. 为此,我使用ctypes库。

My C++ code is library to use a webcam which exports a set of C functions. 我的C ++代码是使用网络摄像头的库,该摄像头可导出一组C函数。

This the function that I want use: 我要使用的功能:

/*! 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.

It is the release memory function 是释放记忆功能

this the HGRABBER structure: 这个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. 

My code is: 我的代码是:

Necessary Structure HGRABBER (it is named HGRABBER_TYPE in my case) 必要的结构HGRABBER(在我的情况下为HGRABBER_TYPE)

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

HGRABBER_TYPE = ctypes.POINTER(HGRABBER_T)

The call function: 调用函数:

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)))

and finally, the error that i receive: 最后,我收到的错误:

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

I checked other related posts, for example this but it didn't help me.. 我检查了其他相关的帖子,例如这个 ,但它并没有帮助我..

I appreciate any help! 感谢您的帮助!

UPDATE: 更新:

I applied restype and argtypes in order to specify argument and return values (thanks!). 我应用了restype和argtypes来指定参数并返回值(谢谢!)。

With the modifications, the code is: 经过修改,代码为:

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)

I should have multiple mistakes, now my error is: 我应该有多个错误,现在我的错误是:

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

I checked the argument for the function (HGRABBER *hGrabber), argtypes for the release function should be: 我检查了函数的参数(HGRABBER * hGrabber),release函数的argtypes应该是:

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

With this modification, i get another different error: 通过此修改,我得到另一个不同的错误:

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

I am searching these errors, it seems a bad conversion of the pointer which i don't understand, the structure seems very simple to cast and i don't see what i miss.. 我正在搜索这些错误,这似乎是我不理解的指针转换错误,结构看起来非常简单,并且我看不到我错过的内容。

UPDATE 2 更新2

I missed to add ctypes.byref when i call the function, it has to be: 我在调用函数时错过了添加ctypes.byref,它必须是:

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

UPDATE 3 更新3

Unfortunately, i am getting a random error related with pointer argument ( (ctypes.byref(self._grabber_handle)) ), sometimes the release function accepts the object but sometimes give this error: 不幸的是,我遇到与指针参数( (ctypes.byref(self._grabber_handle)) )相关的随机错误,有时释放函数接受该对象,但有时会出现以下错误:

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

You can set the return type of IC_CreateGrabber such that you don't need the recast when you call IC_ReleaseGrabber . 您可以设置IC_CreateGrabber的返回类型, IC_CreateGrabber在调用IC_ReleaseGrabber时不需要重铸。

For example: 例如:

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))

By setting the restype and argtypes of the library functions, ctypes knows how to handle the values from the C side of things. 通过设置库函数的restypeargtypes ,ctypes知道如何从C端处理值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM