当前位置: 行情首页 >> 技术文章 >> 技术文章 >> 能否详细讲述一下什么是回调函数以

  • 能否详细讲述一下什么是回调函数以及如何使用回调函数. VC/MFC / 基础类
  • 2007-10-18 15:40:31 杭州电脑数码城 转载来源:csdn.net
  • 社区 - VC/MFC / 基础类

    能否详细讲述一下什么是回调函数以及如何使用回调函数.

    () 2000-02-01 15:52:00在 VC/MFC / 基础类 提问

    THX ALL
    问题点数:50、回复次数:2

    1楼 Laozhang () 回复于 2000-02-01 16:20:00 得分 50


    callback
    The callback attribute declares a static callback function that exists on the client side of the distributed application. Callback functions provide a way for the server to execute code on the client.

    [callback [ , function-attr-list] ] type-specifier [ptr-declarator] function-name(
    [ [attribute-list] ] type-specifier [declarator]
    , ...);

    function-attr-list
    Specifies zero or more attributes that apply to the function. Valid function attributes are local; the pointer attribute ref, unique, or ptr; and the usage attributes string, ignore, and context_handle. Separate multiple attributes with commas.
    type-specifier
    Specifies a base_type, struct, union, enum type, or type identifier. An optional storage specification can precede type-specifier.
    ptr-declarator
    Specifies zero or more pointer declarators. A pointer declarator is the same as the pointer declarator used in C; it is constructed from the * designator, modifiers such as far, and the qualifier const.
    function-name
    Specifies the name of the remote procedure.
    attribute-list
    Specifies zero or more directional attributes, field attributes, usage attributes, and pointer attributes appropriate for the specified parameter type. Separate multiple attributes with commas.
    declarator
    Specifies a standard C declarator such as identifiers, pointer declarators, and array declarators. For more information, see Array and Sized-Pointer Attributes, arrays, and Arrays and Pointers. The parameter-name identifier is optional.
    Remarks
    The callback function is useful when the server must obtain information from the client. If server applications were supported on Windows 3.x, the server could make a call to a remote procedure on the Windows 3.x server to obtain the needed information. The callback function accomplishes the same purpose and lets the server query the client for information in the context of the original call.

    Callbacks are special cases of remote calls that execute as part of a single thread. A callback is issued in the context of a remote call. Any remote procedure defined as part of the same interface as the static callback function can call the callback function.

    Only the connection-oriented and local protocol sequences support the callback attribute. If an RPC interface uses a connectionless (datagram) protocol sequence, calls to procedures with the callback attribute will fail.

    Handles cannot be used as parameters in callback functions. Because callbacks always execute in the context of a call, the binding handle used by the client to make the call to the server is also used as the binding handle from the server to the client.

    Callbacks can nest to any depth.

    Example
    [callback] HRESULT DisplayString([in, string] char * p1);


    2楼 svbird () 回复于 2000-02-10 01:12:00 得分 0


    回调函数是由系统多次调用的函数,
    举个例子,一台PC有一 RIVA128,和一Voodoo2
    系统默认会使用RIVA128,我们如何使用Voodoo2呢,
    这时要使用回调函数进行查找,

    定义全局变量保存我们想要的GUID: GUID voodooGUID = NULL;
    定义回调函数:
    BOOL WINAPI DDEnumCallback(
    GUID FAR *lpGUID,
    LPSTR lpDriverDescription,
    LPSTR lpDriverName,
    LPVOID lpContext
    )
    {
    if(*lpDriverName == RIVA_128) // 注:这里的设备名只是举例而已
    return FALSE; // 继续查找

    if*(lpDriverName == Voodoo2)
    {
    // 正是我们想要的
    voodooGUID = *lpGUID;
    return TRUE; // 结束查找
    }
    }

    主程序:
    if(DirectDrawEnumerate(DDEnumCallback,NULL) == DD_OK)
    {
    // 函数成功返回,已获得Voodoo2的GUID
    // 使用Voodoo2的GUID建立DirectDraw
    DirectDrawCreate(&voodoo2GUID,lplpDD,NULL);
    }
    也就是说系统将多次自动调用回调函数查找每一张显卡,直到找完或由你结束.
    这只是回调函数的一个典型例子.

竟价广告:

    业界行情新闻声明事项:

    • ☉本网转载出于传递更多信息之目的,并不意味着赞同其观点或证实其内容的真实性!
    • ☉如其他媒体、网站或个人从本网下载使用,必须保留本网注明的“稿件来源”,并自负版权等法律责任。如对稿件内容有疑议,请及时与我们联系.
    • ☉如本网转载稿涉及版权等问题,请作者在速来电或来函与杭州电脑数码城网联系.
    • ☉本站网址:http://www.ititt.com/投诉邮箱:6371222@qq.com