CFFI 将回调作为参数传递给另一个函数

发布于 2025-01-09 01:46:03 字数 958 浏览 3 评论 0原文

这是我的误解的解释:

我有一个名为 Codex 的类:

class Codex:
    ffi = FFI()
    def __init__(self):
        self.ffi = FFI()
        self.ClibDec = self.ffi.dlopen("...")
        self.ffi.cdef("""int CRD_Set(int handle, int property, void *value);
                         int CRD_Get(int handle, int property, void *value);
                      """)

    def funct1(self):
        handle = self.ClibDec."something"
        self.ClibDec.CRD_Set(handle, 0, funct2)

    @ffi.callback("int(*)(int, int)")
    def funct2(self, handle):

        status = 0
        self.ClibDec.CRD_Get(handle, 420, status)
        # do something

        return 0

我的问题是如何在 funct1 的 self.ClibDec.CRD_Set 内部调用 funct2 ?

我检查了几个类似的问题和文档: https://cffi.readthedocs.io/en/latest/ using.html#callbacks-old-style

但我还是不明白callback的用法(旧风格)

Here is an explanation of my misunderstanding:

I have a class named Codex:

class Codex:
    ffi = FFI()
    def __init__(self):
        self.ffi = FFI()
        self.ClibDec = self.ffi.dlopen("...")
        self.ffi.cdef("""int CRD_Set(int handle, int property, void *value);
                         int CRD_Get(int handle, int property, void *value);
                      """)

    def funct1(self):
        handle = self.ClibDec."something"
        self.ClibDec.CRD_Set(handle, 0, funct2)

    @ffi.callback("int(*)(int, int)")
    def funct2(self, handle):

        status = 0
        self.ClibDec.CRD_Get(handle, 420, status)
        # do something

        return 0

My question is how can I call funct2 inside self.ClibDec.CRD_Set in my funct1 ?

I check several similar issues and the doc:
https://cffi.readthedocs.io/en/latest/using.html#callbacks-old-style

But I still not understand the usage of callback (old style)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠佳期 2025-01-16 01:46:03

也许您正在寻找这样的代码?这将是最接近您要编写的解决方案。

class MyClass:

    def funct1(self):
        handle = self.ClibDec."something"
        # make a cdata function pointer object that will call
        # back self.funct2(handle)
        cfnptr = ffi.callback("int(*)(int)", self.funct2)
        self.ClibDec.CRD_Set(handle, 0, cfnptr)
        # the cfnptr pointer must not be called after
        # the variable 'cfnptr' goes out of scope,
        # which occurs when funct1() returns here.

    def funct2(self, handle):
        ...
        return 0

Maybe you are looking for code like this? This would be the solution closest to what you're trying to write.

class MyClass:

    def funct1(self):
        handle = self.ClibDec."something"
        # make a cdata function pointer object that will call
        # back self.funct2(handle)
        cfnptr = ffi.callback("int(*)(int)", self.funct2)
        self.ClibDec.CRD_Set(handle, 0, cfnptr)
        # the cfnptr pointer must not be called after
        # the variable 'cfnptr' goes out of scope,
        # which occurs when funct1() returns here.

    def funct2(self, handle):
        ...
        return 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文