Event handling using callback functions

priyapai's Avatar, Join Date: Mar 2006
Go4Expert Member
Hi All,

I am not vry clear with the usage of callback function for Event handling.
What i understand is that, the Application registers a callback function. This callback func has the code to handle the diffent events that are expected. When one of these events hapen, the Callbak func gets called. But what i want to understand is that, how does the callback function gets to know that it has to get executed? If the same event is handled by two diff functons, then what should happen..

Plz send me links or pointers wher i can study mor about the same topic.

Thanks in advance.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
When the function is called means the function executes and the callee who provide the function callback knows when to call the function call back.
priyapai's Avatar, Join Date: Mar 2006
Go4Expert Member
Thankx for the quick responce Shabbir.

But m still not clear.

int (*function_cb) (arg1, arg2)

return_val = caller_function (arg11,arg22,(int)function_cb);


In the above case, when will function_cb get called?
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
function_cb is a function pointer and it will not get call but depending on the situation the callee will call the function (pointed by the function pointer)

I will give you an example.
1. Say you have a function which updates the progress bar for some copy of files operation.
2. When any chunk of file is copied successfully you call the function.
3. Function does it job to update the progress bar.
priyapai's Avatar, Join Date: Mar 2006
Go4Expert Member
ok..

Could u give me an example in code form, which handles some event by calling a callback function.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Refer to the API CopyFileEx where you can specify your call back function in the third parameter and you will see how the call back function of yours is called
kaustubh's Avatar, Join Date: Aug 2007
Go4Expert Member
hello priya,
do read this links
http://www.codeguru.com/Cpp/Cpp/cpp_...le.php/c10557/

http://www.newty.de/fpt/callback.html

with regards
kaustubh