Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ctypes回调函数崩溃Python-“this”指针参数?_Python_C++_This_Ctypes_This Pointer - Fatal编程技术网

ctypes回调函数崩溃Python-“this”指针参数?

ctypes回调函数崩溃Python-“this”指针参数?,python,c++,this,ctypes,this-pointer,Python,C++,This,Ctypes,This Pointer,我正在使用ctypes从Python调用一个C dll,在将正确的参数传递给下面定义的IBSU_RegisterCallbacks函数时遇到了一些问题 int WINAPI IBSU_RegisterCallbacks (const int handle, const IBSU_Events event, void *pCallbackFunction, void *pContext);

我正在使用ctypes从Python调用一个C dll,在将正确的参数传递给下面定义的IBSU_RegisterCallbacks函数时遇到了一些问题

int WINAPI IBSU_RegisterCallbacks
    (const int         handle, 
     const IBSU_Events event, 
     void             *pCallbackFunction,
     void             *pContext);
它将最终参数描述为:

/* pContext Pointer to user context that will be passed to callback function.*/

一个工作C++示例调用这样的函数:

RegisterCallbacks(0, 1, OnCallback, this);
到目前为止,在Python中,我只显示了下面与代码相关的部分,在触发事件并运行回调后不久,该部分将崩溃Python而不进行回溯!是印刷的

from ctypes import *

class IBSU(object):
    _dll = WinDLL('ibsu.dll')

    _C_CALLBACK = CFUNCTYPE(
        None,        
        c_int,                                         # device handle
        c_void_p)                                      # user context

    _register_callbacks = _dll.IBSU_RegisterCallbacks
    _register_callbacks.restype = c_int
    _register_callbacks.argtypes = (c_int,             # device handle
                                    c_int,             # event type
                                    c_void_p,          # callback function
                                    c_void_p)          # user context

    def _get_c_callback(self):
        def callback(handle,                           # device handle
                     p_context):                       # user context
            print 'callback run!'
        return self._C_CALLBACK(callback) 

    def register_callbacks(self):
        # keep ref to prevent garbage collection
        self._c_callback = self._get_c_callback()

        self._register_callbacks(0,                    # device handle
                                 1,                    # event type
                                 self._c_callback,     # callback function
                                 c_void_p())           # user context

在我的Python类中,我可以发送与此等价的内容,而不是在最后一行代码中发送c_void_p?即使在Python回调函数启动后,错误获取此参数是否会导致崩溃?

通常,DLL将始终使用调用约定。您确定回调不应该使用WINFUNCTYPE吗?上下文参数用于较低级别的语言,如C。它对Python几乎没有用处。如果需要上下文,可以传递绑定方法或闭包。@eryksun感谢您指出WINFUNCTYPE,我习惯于使用其他方法。你是在暗示我不需要使用上下文吗?这将使我的生活更轻松,但不确定函数是否要求它有效。那么没有一个就够了吗?如果我确实发送了绑定方法,那么如何将其转换为c_void_p?没有一个方法是足够的;它被转换为空指针。您可以使用任何可调用的Python对象实例化_C_回调原型。这可能是一个函数,它在全局和嵌套范围方面具有闭包上下文;具有自身实例上下文的绑定方法;或者一个带有_调用_方法的对象。非常感谢,很高兴知道。虽然没有和WINFUNCTYPE,但我仍然会崩溃。我开始认为这与用户上下文参数无关。