gpt4 book ai didi

python - 如何使用 ctypes 将 void * 来回传递给 python 中的共享库?

转载 作者:行者123 更新时间:2023-11-30 17:46:45 28 4
gpt4 key购买 nike

我认为基本流程是:

from ctypes import *

LEAP = ctypes.CDLL('leap.dylib')

leap_controller = LEAP.leap_controller
leap_controller.res_type = c_void_p

leap_controller_dispose = LEAP.leap_controller_dispose
leap_controller_dispose.argtype = [c_void_p]

但是,当我调用此代码时,它会破坏 void * 指针。从相关的 C 代码调试,我得到:

doug:test doug$ python test.py
Controller init!
returning: 0x7fcf9a0022c0
Created controller

Controller dispose!
argument: 0xffffffff9a0022c0
Segmentation fault: 11

显然 free() 调用失败是因为指针不正确。

我对此尝试了几种变体,例如,定义一个结构,例如:

class LEAP_CONTROLLER(Structure):
_fields_ = [
("data", c_void_p)
]

leap_controller = LEAP.leap_controller
leap_controller.res_type = POINTER(LEAP_CONTROLLER)

leap_controller_dispose = LEAP.leap_controller_dispose
leap_controller_dispose.argtype = [POINTER(LEAP_CONTROLLER)]

...但结果似乎总是一样的。

Python 读取指针,但只保留 32 位数据,并返回一个损坏的指针,其余位全为 1。

两个二进制文件都是 64 位:

Non-fat file: /usr/local/bin/python is architecture: x86_64
Non-fat file: ../../dll/libcleap.dylib is architecture: x86_64

我在这里看到了这个古老的问题: http://bugs.python.org/issue1703286

...但它被标记为已解决。

其他一些随机邮件似乎提到了 ctypes“将 64 位指针截断为 32 位”,但人们似乎都神奇地解决了他们的问题,没有做任何事情(“它现在可以工作了”)或没有得到任何答案对他们的问题。

有人知道我做错了什么吗?

最佳答案

按照评论回答;这是我的代码中的一个拼写错误。

 Per the docs, it's argtypes and restype, not argtype and res_type. –  eryksun Oct 4 at 2:31 

关于python - 如何使用 ctypes 将 void * 来回传递给 python 中的共享库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19171864/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com