I have a proprietary dll which interfaces with usb device, ctypes interface for it is working fine, but cffi one is crashing after calling a callback.
function SwitchOn(6) should return pointer to a struct if it finds a device and NULL if it doesnt, also if it does not find the device error callback gets called with errno=10.
I am using python27, py33 behaves the same(need to remove 'import thread' to run)
Am I using it right? How do I debug it?
Trying to adapt doc's example for my needs as abarnert suggested. It still crashes. Am I doing it right?
>>> cffi.__version__
'0.7.2'
ctypes example outputs:
10288
(10288, 10L, 1L)
0
cffi example outputs:
4504
(4504, 10L, 1L)
and crashes
cffi_crash.py
import thread
def error(errno, critical):
print(thread.get_ident(), errno, critical)
from cffi import FFI
ffi = FFI()
ffi.cdef('''
void* SwitchOn(int FPort);
typedef void(*type_func_user_error)(unsigned int, unsigned int);
void SetErrorFunction(type_func_user_error);
''')
eeg_dll = ffi.dlopen("EEG4DLL.dll")
err_cb = ffi.callback('type_func_user_error', error)
eeg_dll.SetErrorFunction(err_cb)
print(thread.get_ident())
x = eeg_dll.SwitchOn(6)
print(x)
ctypes_no_crash.py
import thread
def error(errno, critical):
print(thread.get_ident(), errno, critical)
import ctypes
from ctypes import c_uint, WINFUNCTYPE
eeg_dll = ctypes.windll.EEG4DLL
func_user_error = WINFUNCTYPE(None, c_uint, c_uint)
SetErrorFunction = eeg_dll.SetErrorFunction
SetErrorFunction.argtypes = [func_user_error]
SetErrorFunction.restype = None
err_cb = func_user_error(error)
eeg_dll.SetErrorFunction(err_cb)
print(thread.get_ident())
x = eeg_dll.SwitchOn(6)
print(x)
cffi_indirection.py
def error(errno, critical):
print(errno, critical)
from cffi import FFI
ffi2 = FFI()
ffi2.cdef('''
void (*python_callback)(unsigned int, unsigned int);
void *const c_callback;
''')
wr = ffi2.verify('''
static void(*python_callback)(unsigned int x, unsigned int y);
static void c_callback(unsigned int x, unsigned int y) {
python_callback(x, y);
}
''')
err_cb = ffi2.callback('void(unsigned int, unsigned int)', error)
wr.python_callback = err_cb
ffi = FFI()
ffi.cdef('''
void* SwitchOn(int FPort);
typedef void(*type_func_user_error)(unsigned int, unsigned int);
void SetErrorFunction(type_func_user_error);
''')
eeg_dll = ffi.dlopen("EEG4DLL.dll")
eeg_dll.SetErrorFunction(wr.c_callback)
x = eeg_dll.SwitchOn(6)
print(x)