I am trying to use C++ function (in a dll) with python. To do this, i use ctypes library.
My C++ code is library to use a webcam which exports a set of C functions.
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:
//////////////////////////////////////////////////////////////////////////
/*! 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)
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!).
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:
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
I missed to add ctypes.byref when i call the function, it has to be:
self._dllref.IC_ReleaseGrabber(ctypes.byref(self._grabber_handle))
UPDATE 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:
_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
.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.