ctypes回调函数的结果类型无效

Nak*_*ked 8 python ctypes

使用ctypes实现时遇到问题.我有2个C函数:

antichain** decompose_antichain(antichain*, int, char (*)(void*, void*), void** (*)(void*));
counting_function** decompose_counting_function(counting_function*);
Run Code Online (Sandbox Code Playgroud)

其中antichain和counting_function是两种结构.可以看到一个antihain就像一个集合,包含未知类型的元素(在这个例子中,counting_function).decompose_antichain函数将用于分解antichain包含的元素的函数(以及其他内容)作为参数( - >原型为void的函数**(*)(void*)).

现在我想使用Python中的decompose_antichain.我使用了ctypes:

lib = cdll.LoadLibrary("./mylib.dylib")
#CountingFunction, Antichain and other definitions skipped
DECOMPOSE_COUNTING_FUNCTION_FUNC = CFUNCTYPE(POINTER(c_void_p), POINTER(CountingFunction))
decompose_counting_function_c = lib.decompose_counting_function
decompose_counting_function_c.argtypes = [POINTER(CountingFunction)]
decompose_counting_function_c.restype = POINTER(c_void_p)
decompose_antichain_c = lib.decompose_antichain
decompose_antichain_c.argtypes = [POINTER(Antichain), c_int, DECOMPOSE_COUNTING_FUNCTION_FUNC, COMPARE_COUNTING_FUNCTIONS_FUNC]
decompose_antichain_c.restype = POINTER(POINTER(Antichain))

(...)

antichains_list = decompose_antichain_c(antichain, nb_components, COMPARE_COUNTING_FUNCTIONS_FUNC(compare_counting_functions_c), DECOMPOSE_COUNTING_FUNCTION_FUNC(decompose_counting_function_c))
Run Code Online (Sandbox Code Playgroud)

最后一行产生错误:回调函数的结果类型无效.

我看不出问题出在哪里.谁能帮我?谢谢

ric*_*t1k 1

您需要确保 argtypes 和结果类型匹配。看起来您交换了 decompose_antichain_c 的参数类型。您的DECOMPOSE_COUNTING_FUNCTION_FUNC, COMPARE_COUNTING_FUNCTIONS_FUNCargtypes 与您上面给出的 C 函数的声明不匹配。然后你尝试用COMPARE_COUNTING_FUNCTIONS_FUNC第一和DECOMPOSE_COUNTING_FUNCTION_FUNC第二来调用它。

DECOMPOSE_COUNTING_FUNCTION_FUNC看起来也不对。它可能应该CFUNCTYPE(POINTER(c_void_p), c_void_p)只是从代码的其余部分猜测。

COMPARE_COUNTING_FUNCTIONS_FUNC如果您提供创建和的代码,我可以给出更详细的答案CountingFunction