gpt4 book ai didi

python - c_void_p 值无效*

转载 作者:太空宇宙 更新时间:2023-11-04 03:54:51 25 4
gpt4 key购买 nike

我已经定义了 ctypes 结构,像这样:

class MyStruct(Structure):
_fields_ = [('x', ctypes.c_ulonglong), ('y', ctypes.c_ulonglong)]

然后我在 python 中创建 ctypes 结构对象,并将该对象传递给 cython 函数。

struct_instance = MyStruct(4, 2)
some_cy_func(struct_instance)

在 cython 函数中,我需要调用接受 MyStruct 类型参数的 C 函数。我们需要按值传递参数,而不是按引用传递。函数调用将使用 cython,而不是通过 ctypes。

我的问题是,如何从 ctypes 中获取 C 结构的实际值,然后使用 cython 将其传递给 C 函数。

目前我有这样的东西:

ptr = ctypes.cast(ctypes.addressof(struct_instance), ctypes.POINTER(ctypes.c_void_p))
prt_content = ptr.contents

在 prt_content 中我有 c_void_p(4),但这对我没有帮助。有谁知道如何将 cytpes 结构传递给通过 cython 包装的 C 函数,或者这根本不可能?

最佳答案

Cython 不知道 ctypes。您必须使用 Cython 结构:

cdef struct MyStruct:
unsigned long long x
unsigned long long y

cdef MyStruct struct_instance

struct_instance.x = 4
struct_instance.y = 2

some_cy_func(struct_instance)

关于python - c_void_p 值无效*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17473669/

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