By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,384 Members | 714 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,384 IT Pros & Developers. It's quick & easy.

How to Implement Callback Function

P: 3

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 );
//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;
  7. //call the callback
  8. SetSessionStateCallback(my_callback);
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.
Nov 1 '10 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 5K+
P: 8,916
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

P: 3
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

Post your reply

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