Re: [ctypes-users] weird "bytes missing" error for a callback definition
Brought to you by:
theller
From: Mark T. <met...@gm...> - 2010-03-19 06:33:31
|
"Patricio Stegmann" <kp...@ho...> wrote in message news:SNT...@ph...l... > Hello to all, > > I am doing a little wrapper for a library that performs some live image > acquisition. > This library has a callback mechanism which is called when there is an > available live image. > > The headers are quite simple: > # ------------------------ > > DLLIMPORT SetImageCallBack(void (*ptr_ImageCallback)(int, int, char * ) ); > > This got translated to: > # ------------------------ > > STRING = c_char_p > SetImageCallBack = _stdcall_libraries['mylib.dll'].SetImageCallBack > SetImageCallBack.restype = c_int > SetImageCallBack.argtypes = [CFUNCTYPE(None, c_int, c_int, STRING)] > > However I am getting this error: > # ------------------------ > > ValueError: Procedure probably called with not enough arguments (4 bytes > missing) > > This is the simplified code that makes this error happen: > # ------------------------ > > # initialize the callbacks dictionnary > callbacks = {} > > # define python function to be called by the callback > def ViewFinderCallback(int_1, int_2, charp_1): > print 'viewfindercallback' > return 0 # return None does the same thing > > # declaration > SetImageCallBack = _stdcall_libraries['mylib.dll'].SetImageCallBack > SetImageCallBack.restype = c_int > CallbackFunctionImageCallback = CFUNCTYPE(None, c_int, c_int, STRING) > SetImageCallBack.argtypes = [CallbackFunctionImageCallback] > callbacks['ViewFinderCallback'] = > CallbackFunctionImageCallback(self.ViewFinderCallback) > > # then registry of the callback > SetImageCallBack(callbacks['ViewFinderCallback']) > > This gives me the error ... I dont know what is wrong here. > > I hope someone can help me please, Your code is not a functional example, but I suspect from "self.ViewFinderCallback" that you've made your callback a member of a class, and there is an implied "self" parameter that is missing when your callback is called. Here's a rough example. I'm running Windows XP. ============== DLL code compiled with VS2008 "cl /LD /W4 x.c" =========== void (*g_func)(int,int,char*) = 0; __declspec(dllexport) int SetImageCallBack(void (*ptr_ImageCallback)(int, int, char * ) ) { g_func = ptr_ImageCallback; return 1; } __declspec(dllexport) void doit() { g_func(1,2,"Hello"); } ==================== Python code ================================== # DLLIMPORT SetImageCallBack(void (*ptr_ImageCallback)(int, int, char * ) ); import ctypes as c STRING = c.c_char_p dll = c.CDLL('x') SetImageCallBack = dll.SetImageCallBack SetImageCallBack.restype = c.c_int CallbackFunctionImageCallback = c.CFUNCTYPE(None, c.c_int, c.c_int, STRING) SetImageCallBack.argtypes = [CallbackFunctionImageCallback] # initialize the callbacks dictionnary callbacks = {} # define python function to be called by the callback def ViewFinderCallback(int_1, int_2, charp_1): print 'viewfindercallback',int_1,int_2,charp_1 return 0 # return None does the same thing # declaration callbacks['ViewFinderCallback'] = CallbackFunctionImageCallback(ViewFinderCallback) # then registry of the callback SetImageCallBack(callbacks['ViewFinderCallback']) dll.doit() ============= OUTPUT =========== viewfindercallback 1 2 Hello ================================ -Mark |