Re: weird "bytes missing" error for a callback definition

"Mark Tolonen" <[email protected]>
Newsgroups gmane.comp.python.ctypes
Message-ID <[email protected]>
"Patricio Stegmann" <[email protected]> wrote in message 
news:[email protected]...

> 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



------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.