如何在Python中使用ctypes创建回调函数?

时间:2016-04-28 20:49:09

标签: python c ctypes

我为Corsair Utility Engine SDK编写了一个包装器,但是有一个我无法包装的功能。它是一个接受回调函数的异步函数,但我似乎无法弄清楚如何给它回调。

该功能如下所示:

bool CorsairSetLedsColorsAsync(int size, CorsairLedColor* ledsColors, void (*CallbackType)(void* context, bool result, CorsairError error), void *context)

这些是我到目前为止尝试过的实现:

def SetLedsColorsAsync(self, size, led_color, callback, context):
    c_func = CFUNCTYPE(c_void_p, c_void_p, c_bool, c_int)
    c_callback = c_func(callback)
    self._libcue.CorsairSetLedsColorsAsync.restype = c_bool
    self._libcue.CorsairSetLedsColorsAsync.argtypes = [c_int, POINTER(CorsairLedColor), c_void_p, c_void_p]
    return self._libcue.CorsairSetLedsColorsAsync(size, led_color, c_callback, context)

以及

def SetLedsColorsAsync(self, size, led_color, callback, context):
    c_func = CFUNCTYPE(None, c_void_p, c_bool, c_int)
    c_callback = c_func(callback)
    self._libcue.CorsairSetLedsColorsAsync.restype = c_bool
    self._libcue.CorsairSetLedsColorsAsync.argtypes = [c_int, POINTER(CorsairLedColor), c_func, c_void_p]
    return self._libcue.CorsairSetLedsColorsAsync(size, led_color, c_callback, context)

我测试的代码是

from cue_sdk import *
import time


def test(context, result, error):
    print context, result, error
    return 0

Corsair = CUE("CUESDK.x64_2013.dll")
Corsair.RequestControl(CAM_ExclusiveLightingControl)
Corsair.SetLedsColorsAsync(1, CorsairLedColor(CLK_H, 255, 255, 255), test, 1)

while True:
time.sleep(1)

time.sleep()只是为了让程序保持活力。

运行时,它会在Windows上崩溃并显示错误代码3221225477STATUS_ACCESS_VIOLATION)。

If you need to see the actual wrapper, you can find it here

1 个答案:

答案 0 :(得分:0)

我完全忘记了垃圾收集的问题,直到eryksun提醒我。他建议我创建一个永久回调处理程序,它将存储所有回调并在必要时调用+弹出它们。这就是我所做的。

函数原型如下所示:

self._callback_type = CFUNCTYPE(None, c_void_p, c_bool, c_int)
self._callback = self._callback_type(self._callback_handler)
self._libcue.CorsairSetLedsColorsAsync.restype = c_bool
self._libcue.CorsairSetLedsColorsAsync.argtypes = [c_int, POINTER(CorsairLedColor), self._callback_type, c_void_p]

_callback_handler函数如下所示:

def _callback_handler(self, context, result, error):
    if context is not None and context in self._callbacks:
        self._callbacks.pop(context)(context, result, error)

实际功能如下所示。

def SetLedsColorsAsync(self, size, led_color, callback=None, context=None):
    if callback:
        if context is None:
            context = id(callback)
        self._callbacks[context] = callback
    return self._libcue.CorsairSetLedsColorsAsync(size, led_color, self._callback, context)

_callback_type是包装永久回调(_callback_handler)的实际CFUNCTYPE,它是原型的argtype之一。调用SetLedsColorsAsync时,callback参数将放入字典中(上下文或函数的ID为关键字)。而不是将回调提供给函数,而是传递永久回调。一旦调用了永久回调,它将调用正确的函数并将其从字典中删除。

我使用的测试:

#!python3
import time

from cue_sdk import *


def test(context, result, error):
    print(context, result, error)
    assert context == id(test)


Corsair = CUE("CUESDK.x64_2013.dll")
Corsair.RequestControl(CAM_ExclusiveLightingControl)
Corsair.SetLedsColorsAsync(1, CorsairLedColor(CLK_H, 255, 255, 255), test)

while True:
    time.sleep(1)

示例输出:

2969710418936 True 0

If I'm not making sense, the commit is here.