Ich habe einen Wrapper für das Corsair Utility Engine SDK geschrieben, aber es gibt eine Funktion, die ich nicht umbrechen konnte. Es ist eine asynchrone Funktion, die eine Callback-Funktion akzeptiert, aber ich kann nicht herausfinden, wie ich diesen Rückruf geben soll.Wie erstelle ich eine Callback-Funktion mit Ctypes in Python?
Die Funktion sieht wie folgt aus:
bool CorsairSetLedsColorsAsync(int size, CorsairLedColor* ledsColors, void (*CallbackType)(void* context, bool result, CorsairError error), void *context)
Dies sind die Implementierungen, die ich versucht habe, so weit:
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)
sowie
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)
Der Code I teste mit ist
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)
Die time.sleep()
ist nur da, um das Programm am Leben zu erhalten.
Beim Ausführen stürzt es unter Windows mit dem Fehlercode 3221225477
(STATUS_ACCESS_VIOLATION
) ab.
If you need to see the actual wrapper, you can find it here.
@ IgnacioVazquez-Abrams Ich bin nicht sicher, was du meinst, können Sie das näher erläutern? – 10se1ucgo
Wie definieren Sie 'CallbackType'? – tynn
@tynn [Dies ist die Dokumentation für die Funktion] (http://i.imgur.com/ZRsrSPW.png), CallbackType wird an keiner anderen Stelle im Handbuch erwähnt. – 10se1ucgo