回调函数(200分)

F

f643208

Unregistered / Unconfirmed
GUEST, unregistred user!
求一个回调函数的例子
说明一下回调函数得特有功效
最好是 纯 C 代码
 
回调函数或钩子就是一个函数指针而已。
举个例子,不太确切,但能说明意思。
struct command_s
{
char* cmd_name;
void (*handler)(char*,int,int);
};
void test_cmd(char* c,int i,int j)
{
printf("%s/n",c);
}
void test1()
{
struct command_s cmd={"cmd",test_cmd};
cmd.handler("hello world.",0,0);
}
而回调函数是系统提供的函数指针,如该指针没有被定义(为空)则系统什么也不做,继续
往下执行。如果不为空,则系统会执行你的函数,相当于把你的代码插入系统的代码。
 
在应用程序中可调用Dll的Function,
如果将函数地址传递给Dll,在Dll也可以用应用程序的Function!
妙极!
 
这个不是回调函数
能在举个例子吗 分数不够可以在家
 
给你
在加点分吧
我就要排第一了
class AFX_EXT_CLASS CMousehook:public CObject
{
public:
CMousehook();
~CMousehook();
BOOL starthook();//封装SetWindowsHookEx( int idHook, HOOK_PROC lpfn, HINSTANCE hMod,DWORD dwThreadID)用来安装钩子
BOOL stophook();
//封装UnhookWindowsHookEx( HHOOK hhk )用来卸载钩子
VOID SetCheck1(UINT i);//处理对话框的选择钩选框1
VOID SetCheck2(UINT i);//处理对话框的选择钩选框2
VOID SetCheck3(UINT i);//处理对话框的选择钩选框3
static BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam);//系统回调的钩子函数
VOID UseForExit();//退出程序时恢复所有隐藏窗口
};
 
回调函数相当于把函数作为参数。
父函数可以不知道要调用什么样的函数,但明确定义了这个函数的类型(原型)
这个具体的函数由用户设计提供。
 
to 老人家
能在说说具体怎样用的吗
分数不够可以在家
 
to f643208
留下你的qq和mail
 
如下:
type
TCallBackFunction = function(s: string): integer;
CallMe(s: string): integer;

procedure TestCallBack(CallBackFunction: TCallBackFunction);
far;
external 'Other';
{ Note that 'other' is a Dll containing the procedure TestCallBack }
function CallMe(s: PChar): integer;
begin
{ what ever you need todo
}
CallMe := 1;
{ What ever you need to return }
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
TestCallBack(CallMe);
end;

type
TMainFunction = function(s: string): integer;
TestCallBack(MainFunc: TMainFunction);
{ in library Other implementation }
TestCallBack(MainFunc: TMainFunction);
var
result: integer;
begin
result:=MainFunc('test');
end;
 
举个例子来说明:
你想在你的 Form 的尺寸变化的时候调整你的界面,你会写 Form 的 OnResize 事件。
这个 OnResize 就是你写的回调函数。在人家设计 TForm 类的时候并不知道你要在 Resize
时调整什么,所以,它们只能留下个回调接口,等你传你的回调函数进入,当它真的
Resize 时,就可以调用你的函数了。
是人家的程序调用的,所以叫回调了。
 
这个问题类似于 delphi的 事件派遣
button1.OnClick :TNotifyEvent
TNotifyEvent = procedure(Sender: TObject) of object
这就是回调函数
求求你给我点分吧
我已经没分了
 
guanzhu 上边说的都不典型
 
给你个例子,这是某嵌入式操作系统底层协议栈的源码:
...
if ((etherOutputHookRtn != NULL) &&
(* etherOutputHookRtn)
(&pDrvCtrl->idr, (struct ether_header *)pMblk->m_data,pMblk->m_len))
{
continue;
}
...
 
接受答案了.
 
顶部