Python ctypes和函数指针 [英] Python ctypes and function pointers

查看:608
本文介绍了Python ctypes和函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与我的其他问题有关,但是我觉得我应该在一个新问题中问这个问题。

This is related to my other question, but I felt like I should ask it in a new question.

基本上,FLAC使用函数指针进行回调,并使用ctypes实现回调,请使用 CFUNCTYPE 进行原型制作,然后使用 prototype()函数创建它们。

Basically FLAC uses function pointers for callbacks, and to implement callbacks with ctypes, you use CFUNCTYPE to prototype them, and then you use the prototype() function to create them.

我遇到的问题是我认为自己会这样创建回调函数(我没有显示自己重新创建的结构,FLAC__Frame是一个结构):

The problem I have with this is that I figured that I would create my callback function as such (I am not showing the structures that I have recreated, FLAC__Frame is a Structure):

write_callback_prototype = CFUNCTYPE(c_int, c_void_p, 
                                     POINTER(FLAC__Frame), 
                                     POINTER(c_int32), v_void_p)

我遇到的问题是实现。 FLAC__Frame永远不会被程序员实例化,只能从初始化函数和处理函数中调用。我必须自己编写回调函数,但是他的问题是我不知道该怎么做,所以如果有人知道我该怎么做,那么将不胜感激。

The problem that I have is the implementation. FLAC__Frame is never instantiated by the programmer, it's only called from from the initialization function, and the processing functions.I have to write the callback function myself, but he problem is that I don't know how I would do this, so if anyone knows how I should do this, then some help would be greatly appreciated.

推荐答案

根据 ctypes回调文档您可以定义python函数

According to the ctypes callback docs you can define python function

def my_callback(a, p, frame, p1, p2)
    pass

,然后创建一个指向C此类可调用函数的指针:

and then create a pointer to a C callable function like this:

callback = write_callback_prototype(my_callback)

然后可以将该函数指针传递给FLAC

This function pointer can then be passed into FLAC

这篇关于Python ctypes和函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆