gpt4 book ai didi

python - 模拟 python ctypes byref 参数 2 pre 2.6

转载 作者:太空宇宙 更新时间:2023-11-04 06:08:01 24 4
gpt4 key购买 nike

我正在使用 Python ctypes 与一些 C 代码交互。

对于我尝试调用的特定 C 函数,我需要传递一个指向结构中字段的指针。该字段不是第一个字段。看起来 byref() 函数会执行此操作,所以我正在尝试使用;

 byref(obj,myObject.data.offset)

myObject 的定义如下:

class myObject(Structure):
_fields_ = [
('meta1',c_void_p),
('meta2',c_int),
('meta3',c_int),
('data',c_char * 10)
]

然而,这个调用失败了,因为它需要运行的系统只运行 Python 2.5 版本,并且在 2.6 中添加了添加可选偏移参数的能力。

我试过 byref(obj.data) 但这会导致错误:TypeError: byref() argument must be a ctypes instance, not '_ctypes.CField'.

我怎样才能在版本 < 2.6 中获得所需的结果(总是需要 2.6 是不切实际的,因为这需要在并不总是可以升级的系统中工作)。

最佳答案

在 2.5 中,ctypes 有 addressof ,因此您可以使用以下内容:

obj = myObject()
arg = c_char_p(addressof(obj) + myObject.data.offset)

关于python - 模拟 python ctypes byref 参数 2 pre 2.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20752303/

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