2

我有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。在回调函数中传递field1、field2和data参数(data通常等于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))
4

1 回答 1

1

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

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);

下面是一些使用 Python 的 ctypes 示例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 代码所做的)。

于 2012-05-24T16:00:43.773 回答