GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Tech FAQs  >  OOPS
Go To First  |  Previous Question  |  Next Question 
 OOPS  |  Question 6 of 255    Print  
How can I call a function, given its name as a string?

  
Total Answers and Comments: 3 Last Update: June 06, 2007   
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Ilan
 
this strongly depends on the language and library you're using. Some object oriented languages support a notion of reflection or introspection, where you can use a built in api to query objects, iterate over their methods and even invoke them. C++ has RTTI, java has java.lang.reflect or something simillar.

To implement simillar behavior in C, with gcc,

1. compile your program with  --export-dynamic on the gcc command line
2. link with -ldl (dynamic library handling, needed for dlopen and dlsym
3. call dlopen() with a null parameter, meaning you aren't loading symbols from a file but from the current executable
4. call dlsym() with the name of the function you'll be calling. Note that C++ modifies function names, so If you're trying this with C++, you'd have to either declare this function as extern "C", or figure out what name the function has after compiling. (On unix, you could run nm -s on the object file for this).
5. if dlsym() returns non-null, use the returned value as a function pointer to invoke your function.


Above answer was rated as good by the following members:
Rajnikant_Jachak
September 13, 2005 06:05:00   #1  
hi2vino Member Since: September 2005   Contribution: 2    

RE: How can I call a function, given its name as a st...
Just call the function names string as string() if the string function have no parameter if it have parameter then pass the corresponig value through the function call
 
Is this answer useful? Yes | No
April 01, 2007 07:11:52   #2  
Ilan        

RE: How can I call a function, given its name as a st...
this strongly depends on the language and library you're using. Some object oriented languages support a notion of reflection or introspection, where you can use a built in api to query objects, iterate over their methods and even invoke them. C++ has RTTI, java has java.lang.reflect or something simillar.

To implement simillar behavior in C, with gcc,

1. compile your program with  --export-dynamic on the gcc command line
2. link with -ldl (dynamic library handling, needed for dlopen and dlsym
3. call dlopen() with a null parameter, meaning you aren't loading symbols from a file but from the current executable
4. call dlsym() with the name of the function you'll be calling. Note that C++ modifies function names, so If you're trying this with C++, you'd have to either declare this function as extern "C", or figure out what name the function has after compiling. (On unix, you could run nm -s on the object file for this).
5. if dlsym() returns non-null, use the returned value as a function pointer to invoke your function.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
June 06, 2007 02:51:21   #3  
jbanx Member Since: February 2007   Contribution: 38    

RE: How can I call a function, given its name as a st...

Hi, This is a typical example of reflection. If you are using .NET then use reflection for this purpose. In this case you create an object of a particular  type and the invoke methods at run time by passing function name.


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape