【发布时间】:2013-11-05 05:33:09
【问题描述】:
我有一个与 USB 设备接口的专有 dll,它的 ctypes 接口工作正常,但 cffi 调用回调后崩溃。 如果找到设备,函数 SwitchOn(6) 应该返回指向结构的指针,如果没有,则返回 NULL,如果它没有找到设备错误回调,则使用 errno=10 调用。
我使用的是 python27,py33 的行为相同(需要删除“导入线程”才能运行)
我用对了吗?如何调试?
按照 abarnert 的建议,尝试根据我的需要调整 doc 的示例。它仍然崩溃。我做得对吗?
>>> cffi.__version__
'0.7.2'
ctypes 示例输出:
10288
(10288, 10L, 1L)
0
cffi 示例输出:
4504
(4504, 10L, 1L)
然后崩溃
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)
【问题讨论】:
-
从函数返回时崩溃看起来就像你调用
cdecl函数,就好像它是stdcall函数一样,反之亦然。如果您可以运行调试器(如 MSVC、windbg 等;而不是 Python 调试器),您应该能够验证它是调用后的堆栈清理,可以轻松处理所有内容。
标签: python ctypes cpython python-cffi