将PyCapsule转换为ctypes.c_void_p



我在一个库中有一个python方法,它返回一个图像句柄作为PyCapsule。我需要将句柄传递给另一个期望它作为c_void_p的库。如何将胶囊转换为c_void_p以促进此互操作?

我在Python中尝试了以下强制转换,但无济于事

addr = ctypes.py_object(capsule)

谢谢。下面的实现工作了。

ctypes.pythonapi.PyCapsule_GetName.restype = ctypes.c_char_p
ctypes.pythonapi.PyCapsule_GetName.argtypes = [ctypes.py_object]
name = ctypes.pythonapi.PyCapsule_GetName(capsule)
ctypes.pythonapi.PyCapsule_GetPointer.restype = ctypes.c_void_p
ctypes.pythonapi.PyCapsule_GetPointer.argtypes = [ctypes.py_object, ctypes.c_char_p]
addr = ctypes.pythonapi.PyCapsule_GetPointer(capsule, name)
addr = ctypes.c_void_p(addr)

最新更新