我爲Corsair實用工具引擎SDK編寫了一個包裝器,但有一個函數我一直無法包裝。這是一個接受回調函數的異步函數,但我似乎無法弄清楚如何給它回調。如何用Python中的ctypes創建回調函數?
功能如下:
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上崩潰,錯誤代碼爲3221225477
(STATUS_ACCESS_VIOLATION
)。
If you need to see the actual wrapper, you can find it here。
@ IgnacioVazquez - 艾布拉姆斯我不知道你的意思,你能詳細點嗎? – 10se1ucgo
你如何定義'CallbackType'? – tynn
@tynn [這是該函數的文檔](http://i.imgur.com/ZRsrSPW.png),在手冊的其他地方沒有提及CallbackType。 – 10se1ucgo