English 中文(简体)
使用 ctypes 的回召功能
原标题:Callback functions using ctypes

我有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)

The application calls the user-defined (in the function_one) callback function function_callback. In the callback function passed field1, field2 and data parameters (data is usually equal to 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);

使用 force_ one_ one () 的 ctypes Python 示例如下:

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

你可以看到,这个代码和你的代码之间有许多不同之处;可能太多无法对一切做出充分解释。 但是,如果有些内容似乎特别令人困惑,我可以解释几个特定部分。 总的来说,必须很好地了解Ctype 指针是如何工作的(例如,你可能不想让指针指针无效,但这正是你的 Python 代码所做的 ) 。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签