使用 ctypes 的回调函数

Callback functions using ctypes

本文关键字:函数 回调 ctypes 使用      更新时间:2023-10-16

我有 C 语言的代码:

typedef result function_callback(struct mes_t* message, void* data) 
struct mes_t
{
uint32_t field1
uint32_t field2
void* data
};
function_one(&function_callback, data)

应用程序调用用户定义的(在 function_one 中)回调函数function_callback。在回调函数中传递了字段1、字段2和数据参数(数据通常等于0)

此示例的 python 上的代码是否正确编写?

class mes_t(ctypes.Structure):
    pass
mes_t._fields_ = [
    ('field1', ctypes.c_uint32),
    ('dfield2', ctypes.c_uint32),
    ('data', ctypes.POINTER(ctypes.c_void_p))]
data_t=ctypes.c_void_p
data=data_t()
CALLBACK=CFUNCTYPE(ccg_msg, data_t)
cb_func=CALLBACK()
result = function_one(ctypes.byref(cb_func), ctypes.byref(data))

我在这里猜到了解释代码的正确方法。此处调整了示例片段:

typedef int /* or whatever */ result;
struct mes_t
{
    uint32_t field1;
    uint32_t field2;
    void* data;
};
typedef result function_callback(struct mes_t* message, void* data);
result function_one(function_callback fcb, void* data);

这里有一些例子 ctypes Python 用于利用function_one()

class mes_t(ctypes.Structure):
    _fields_ = (
        ('field1', ctypes.c_uint32),
        ('field2', ctypes.c_uint32),
        ('data', ctypes.c_void_p))
result_t = ctypes.c_int; # or whatever
callback_type = ctypes.CFUNCTYPE(result_t, ctypes.POINTER(mes_t), ctypes.c_void_p)
function_one.argtypes = (callback_type, ctypes.c_void_p)
function_one.restype = result_t
data_p = ctypes.c_char_p('whatever')
def the_callback(mes_p, data_p):
    my_mes = mes_p[0]
    my_data_p = ctypes.cast(data_p, ctypes.c_char_p)  # or whatever
    my_data = my_data_p.value
    print "I got a mes_t object! mes.field1=%r, mes.field2=%r, mes.data=%r, data=%r" 
          % (my_mes.field1, my_mes.field2, my_mes.data, my_data)
    return my_mes.field1
result = function_one(callback_type(the_callback), ctypes.cast(data_p, ctypes.c_void_p))

你会看到这和你的代码之间有很多差异;可能太多了,无法给出所有内容的完整解释。但是,如果有一些看起来特别令人困惑的部分,我可以解释一些特定的部分。不过,一般来说,重要的是要很好地理解 ctypes 指针的工作原理(例如,你可能不希望指向 void 指针的指针,但这就是你的 python 代码所做的)。