473,387 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to Implement Callback Function

Hi,

I have a prototype of a callback function and of a function which calls it by recieving the callback as a parameter as follows:

Expand|Select|Wrap|Line Numbers
  1. typedef void (* tFNSessionState)(ULONG state, ULONG reason );
  2. ULONG SetSessionStateCallback( tFNSessionState pCallback );
  3.  
//ULONG is defined as unsigned long

The callback should return values into state and reason and these should be stored into a structure like:
Expand|Select|Wrap|Line Numbers
  1. typedef struct session_s 
  2. {                 
  3.     ULONG s_state;
  4.     ULONG s_reason;
  5. }session_t;
  6.  
  7. //call the callback
  8. SetSessionStateCallback(my_callback);
  9.  
how do i implement my_callback function so it will return a filled structure of type session_t though SetSessionStateCallback should return ULONG?

Thanks in advance.
Eran
Nov 1 '10 #1
2 3277
Banfa
9,065 Expert Mod 8TB
I believe you have mis-understood how call backs work. It is unlikely that your function my_callback will be called when SetSessionStateCallback is called. You are just registering my_callback with the system for it to use later.

my_callback returns void so it can return nothing, if you want to store the pass parameters, state and reason in a structure and make it available to the rest of your code then you should implement some sort of external store to put them into and read them from.

If you where using C++ I would suggest a STL container like std::deque.

If you are using C then setting up some store of linked list type affair.

In both cases making sure if you have multiple threads (which is likely if you are using callbacks) that you use some sort of semaphore/mutex/critical section interprocess communications to prevent store corruption by access from multiple threads simultaneously.
Nov 1 '10 #2
Hi Banfa,

Thanks for the quick reply.

Okay, so SetSessionStateCallback(my_callback) will not actually call my_callback, it will just register it for later use. it is now clear.
Who however will later call it and when? i mean, if the programmer decides when and how to call it, it doesn't have to be registered in the first place. its a normal function and not a callback.
i figured some event will rise at the thread and this will generate the call to the registerd ballback function. But, if there are several callbacks how will the system know which callback to invoke?

I work with C and you suggested a linked list will be in need in order to read/write parameters. Probably it will also act as a mechnism to decide on the appropriate callback to be used.
what should the list members be like? they should not be of a specific structure type since different callbacks will likely return different structure types. so, how to connect between the event and the appropriate callback?
Nov 1 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Pratik | last post by:
what are callback functions? Where we require callback functions? In what scenario we require callback functions?
11
by: ajay.sonawane | last post by:
Hello ther I read somewhere that callback function should be global or static member function. 1: I could use static member functions but how could I access members of class which ar not static....
2
by: Fabiano Maciel | last post by:
Hi, I am needing information about CallBack Functions / Delegates in the VB.NET. If somebody has some material on the subject, favor to indicate me where I can get, or to send me for email. ...
2
by: Daniele Baroncelli | last post by:
Hi there, I am struggling to find examples on the web that implement Javascript callback functions of an embedded Real Player. There are lots of examples for Visual Basic, but I couldn't find...
2
by: Tyler | last post by:
Hi all, I am using some components from a JS library (scriptalicious), where callback functions are arguments to the constructors of the components like so: someClass x (name) { this.name =...
2
by: Chris Bore | last post by:
If I have a self-contained server component, that specifies callback functions, then would it be good practice for: 1) the server component itself to provide a header file with the callback...
1
by: kikivenkat | last post by:
Hi, I understand the concept of function pointers. I am a little confused with the call back functions. 1.If a function is invoked using a function pointer, then does it mean the function...
7
by: prashant.khade1623 | last post by:
What are callback function ? how are they implemented and where they are useful ? what are the advantages of function pointer ?
5
by: Stewart Robert Hinsley | last post by:
Yesterday I prototyped some code to read an XML file, and convert it into a web page, using the expat XML parser. I then packaged it into a class, and found that there are problems with using...
0
by: judarvin | last post by:
Hi there! Iīm invoking some functions from a dll in C, and I donīt know how to use callback functions from the dll. Itīs a driver library. How can I threat a callback function? Thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.