LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Call back method for shared library (https://www.linuxquestions.org/questions/programming-9/call-back-method-for-shared-library-419299/)

arunka 02-24-2006 09:54 PM

Call back method for shared library
 
Hi All,

i wrote an application which loads shared libraries at run time.
my question : is there any way a shared libray can call a method/funtion which is defines in the application?

Can we do this ?if yes how to do it.

Can any one help me?

thanks in adavnce
Arun.

paulsm4 02-25-2006 01:10 AM

Sure - just use a "callback":

1. Your library function has a parameter that's a pointer to a function.

2. You program that calls the library function defines the actual function
(which is the "callback function:).

3. When your program calls the library function, it passes the callback.

4. Your library invokes the callback.

Examples of "callbacks" are numerous:

* signal handlers (in the Linux world)
* WinProc (in "classic Windows" programming)
* GTK+ event handlers
* etc etc

Try "man signal", for example.

Hko 02-25-2006 11:43 AM

The code of cpufreqd is a nice real-world example about how to load shared libraries as 'plugins' and let them register several call-back functions that get called from the main program.

As it is a complete program, it may look a bit tedious to dive into, but the code is quite clean IMHO, and not too big.


All times are GMT -5. The time now is 07:01 PM.