Call a function in a function with name of a function as parameter 
Author Message
 Call a function in a function with name of a function as parameter

Hello,

In C on Digital Unix, I want to do the following:
I call a function with 3 parameters: the first parameter is a char
string which is the name of the function to be called.  The second and
third parameters, both with type char, are parameters for that function
to be called. F.e.:

main()
{
  ...
   func("NameFunc", p1, p2);
  ...

Quote:
}

func(char *csFunc, char *p1, char *p2)
{
       call function with name csFunc(p1, p2)

Quote:
}

This program will be linked with a shared library, which can be modified
by another person, thus also adding new functions in this shared
library.  When there are new functions, this program above must call the
new function with parameter the name of the function, without modifying
this program !  This means modifying the reference in the compiled
program above.  Only the shared library could be modified.

How can I resolve this problem ?




Tue, 30 Jan 2001 03:00:00 GMT  
 Call a function in a function with name of a function as parameter

           gatekeeper.ordab.ordab.be "Stefan Smeets" writes:

Quote:
>Hello,

>In C on Digital Unix, I want to do the following:
>I call a function with 3 parameters: the first parameter is a char
>string which is the name of the function to be called.  The second and
>third parameters, both with type char, are parameters for that function
>to be called. F.e.:

>main()
>{
>  ...
>   func("NameFunc", p1, p2);
>  ...
>}

>func(char *csFunc, char *p1, char *p2)
>{
>       call function with name csFunc(p1, p2)
>}

>This program will be linked with a shared library, which can be modified
>by another person, thus also adding new functions in this shared
>library.  When there are new functions, this program above must call the
>new function with parameter the name of the function, without modifying
>this program !  This means modifying the reference in the compiled
>program above.  Only the shared library could be modified.

In C you can select what function to call at runtime using function pointers.
The problem is how to map the name string to the appropriate function
pointer. One solution to this would be to add a function to the shared
library that does this. It can know about what functions the library supports
therefore you could write it to take a string and return the appropriate
function pointer (it would use some sort of lookup table). Even simpler
build a version of func() into the library so that when it is called it
proceeds to call the relevant function. There may also need to be a
mechanism for the library to tell the application what function names are
valid for this purpose.

Your platform may support non-standard approaches e.g. Unix platforms with
synamic library support will usually have functions like dlopen() and dlsym()
which allow you to access library symbols.

--
-----------------------------------------


-----------------------------------------



Tue, 30 Jan 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Calling DLL functions without known function name, parameter-list until run-time

2. Question : A generic function to retrieve the call parameters of a function

3. Question : A generic function to retrieve the call parameters of a function

4. Function Parameters Change when function called?!

5. Function Parameters Change when function called?!

6. function parameters and return values aren't changed by function call

7. Function Parameters Change when function called?!

8. Can i call a function using a variable that contains a function name

9. DLL function calling problems (explicit - only knowing function name at runtime)

10. Condition breakpoints and function calls and function calls in Immediate window

11. Passing C++ Class Member Function to as a C Callback Function Parameter

12. How to use a m.function as a parameter for another m.function

 

 
Powered by phpBB® Forum Software