Reputation: 170480
I have to define a callback function in Python, one that will be called from a DLL.
BOOL setCallback (LONG nPort, void ( _stdcall *pFileRefDone) (DWORD nPort, DWORD nUser), DWORD nUser);
I tried this code, that seems to work in Python 2.5 but with Python 2.7 it crashes and I assume I did something wrong.
import ctypes, ctypes.wintypes
from ctypes.wintypes import DWORD
def cbFunc(port, user_data):
print "Hurrah!"
CB_Func = ctypes.WINFUNCTYPE(None, DWORD, DWORD)
mydll.setCallback(ctypes.c_long(0), CB_Func(cbFunc), DWORD(0))
Where is the mistake?
Note: The platform is running on 32bit only (both Windows and Python). The DLL loads successfully and also other functions from inside do work just fine while called from Python.
A full sample code that reproduces this is available at https://github.com/ssbarnea/pyHikvision - just run Video.py under py25 and py27.
Upvotes: 4
Views: 3051
Reputation: 414265
In the context of Video class using a nested function:
class Video(object):
def __init__(self, ...):
self.__cbFileRefDone = []
def open(self, filename):
@WINFUNCTYPE(None, DWORD, DWORD)
def cbFileRefDone(port, user_data):
print "File indexed.", filename
self.__cbFileRefDone.append(cbFileRefDone) # save reference
if not self.hsdk.PlayM4_SetFileRefCallBack(
c_long(self.port), cbFileRefDone, DWORD(0)):
logging.error("Unable to set callback for indexing")
return False
It is somewhat ugly but a more natural variant fails with TypeError during callback:
#XXX this won't work
@WINFUNCTYPE(None, DWORD, DWORD)
def cbFileRefDone(self, port, user_data):
print "File indexed."
To fix that a special function descriptor could be created:
def method(prototype):
class MethodDescriptor(object):
def __init__(self, func):
self.func = func
self.bound_funcs = {} # hold on to references to prevent gc
def __get__(self, obj, type=None):
assert obj is not None # always require an instance
try: return self.bound_funcs[obj,type]
except KeyError:
ret = self.bound_funcs[obj,type] = prototype(
self.func.__get__(obj, type))
return ret
return MethodDescriptor
Usage:
class Video(object):
@method(WINFUNCTYPE(None, DWORD, DWORD))
def cbFileRefDone(self, port, user_data):
print "File indexed."
def open(self, filename):
# ...
self.hsdk.PlayM4_SetFileRefCallBack(
c_long(self.port), self.cbFileRefDone, DWORD(0))
Additionally MethodDescriptor saves references to C functions to prevent them being garbage collected.
Upvotes: 3
Reputation: 177685
Your CB_Func(cbFunc) parameter gets garbage-collected right after the setCallback function. That object has to persist as long as the callback can be called (15.17.1.17. Callback functions, last paragraph). Assign it to a variable and keep it around. Here's my working example:
typedef unsigned int DWORD;
typedef long LONG;
typedef int BOOL;
#define TRUE 1
#define FALSE 0
typedef void (__stdcall *CALLBACK)(DWORD,DWORD);
CALLBACK g_callback = 0;
DWORD g_port = 0;
DWORD g_user = 0;
BOOL __declspec(dllexport) setCallback (LONG nPort, CALLBACK callback, DWORD nUser)
{
g_callback = callback;
g_port = nPort;
g_user = nUser;
return TRUE;
}
void __declspec(dllexport) Fire()
{
if(g_callback)
g_callback(g_port,g_user);
}
from ctypes import *
def cb_func(port,user):
print port,user
x = CDLL('x')
CALLBACK = WINFUNCTYPE(None,c_uint,c_uint)
#cbfunc = CALLBACK(cb_func)
x.setCallback(1,CALLBACK(cb_func),2)
x.Fire()
from ctypes import *
def cb_func(port,user):
print port,user
x = CDLL('x')
CALLBACK = WINFUNCTYPE(None,c_uint,c_uint)
cbfunc = CALLBACK(cb_func)
x.setCallback(1,cbfunc,2)
x.Fire()
Edit: Also, since CALLBACK
is a function returning a function, it can be used as a decorator for the Python callback, eliminating problem of the callback going out of scope:
from ctypes import *
CALLBACK = WINFUNCTYPE(None,c_uint,c_uint)
@CALLBACK
def cb_func(port,user):
print port,user
x = CDLL('x')
x.setCallback(1,cb_func,2)
x.Fire()
Upvotes: 4