473,394 Members | 1,865 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,394 software developers and data experts.

function name to function address translation?

DOA
Help! I've been doing C language development for a long time, but this
problem has me stumped.

I would like the user to be able to enter the name of a function that
has been linked into the application they are currently running, and
have the application call the function they have requested.

Example code:

char fn_name[80];
int (*p_fn)(int,int,int *);
int res1,res2;

printf("Function name? ");
scanf("%s",fn_name);

p_fn = translate_function_name_to_function_address(fn_nam e);
if (p_fn)
res2 = (*p_fn)(5,20,&res1);
else
printf("ERROR - requested function not found\n");

Question: is a function to do the name-to-address translation
available in most (or any) C libraries and associated development
environments? If not, how would you do it?

Thanks in advance for any hints!
Steve

P.S. Ideally, I wouldn't have to do any custom programming to support
this functionality (e.g. build and search a function name to function
address translation table myself).
Nov 14 '05 #1
2 3309
do*******@aol.com (DOA) writes:
I would like the user to be able to enter the name of a function that
has been linked into the application they are currently running, and
have the application call the function they have requested.


This is a FAQ.

20.6: If I have a char * variable pointing to the name of a function,
how can I call that function?

A: The most straightforward thing to do is to maintain a
correspondence table of names and function pointers:

int func(), anotherfunc();

struct { char *name; int (*funcptr)(); } symtab[] = {
"func", func,
"anotherfunc", anotherfunc,
};

Then, search the table for the name, and call via the associated
function pointer. See also questions 2.15, 18.14, and 19.36.

References: PCS Sec. 11 p. 168.
--
"When in doubt, treat ``feature'' as a pejorative.
(Think of a hundred-bladed Swiss army knife.)"
--Kernighan and Plauger, _Software Tools_
Nov 14 '05 #2
From the book Advanced Linux Programming:

--open quote--
2.3.6 Dynamic Loading and Unloading

Sometimes you might want to load some code at run time without
explicitly linking in that code. For example, consider an application
that supports "plug-ins" modules, such as a Web browser...

This Functionality is avaliable under Linux by using the dlopen()
function. You could open a shared library named libtest.so by calling
dlopen() like this:

dlopne("libtest.so", RTLD_LAZY)

(The second parameter is a flag that indicates how to bind symbols in
the shared library. You can consult the man pages for dlopen if you
want more information, but RTLD_LAZY is usually the setting that you
want.) Yo use dynamic loading functions, include the <dlfcn.h> header
file and link with the -ldl option to pick up the libdl library.

The return value from this function is a void * that is used as a
handle for the shared library. You can pass this value to the dlsym()
function to obtain the address of a function that has been loaded with
the shared library. For example, if libtest.so defines a function
named my_function(), you could call it like this:

void* handle = dlopen("libtest.so", RTLD_LAZY);
void (*test)() = dlsym(handle, "my_function");
(*test)();
dlclose(handle);

The dlsym() system call can also be used to obtain a pointer to a
static variable in the shared library.

Both dlopen() and dlsym() return NULL if they do not succeed. In that
event you can call dlerror() (with no parameters) to obtain a
human-readable error message describing the problem.

The dlclose() function unloads the shared library. Technically,
dlopen() actually loads the library only if it is not already
loaded. If the library has already been loaded, dlopen() simply
increments the library reference count. Similarly, dlclose()
decrements the reference count and then unloads the library only if
the reference count has reached zero.
--close quote--

Maybe that might help.
Regards
--
Gustavo G. Rondina
http://gustgr.freeshell.org
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
9
by: Netocrat | last post by:
Any comments on the correctness of the statements 1, 2a, 2b, 3 and 4 in the code below? If they are correct, then the definition of an object as well as that of an lvalue is broken in C99 by the...
40
by: vishnu | last post by:
Hi friend, i have a problem in my program what is the use of static function in C lang? plz help me
23
by: I.M. !Knuth | last post by:
A while back, I was mucking around with a recursive function. For brevity's sake, I'll define it like this: int func_recurs(int arg1, int arg2, int prev_pos) { int crnt_pos = 0; int result; ...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
14
by: jg | last post by:
Does C++ standard require an inline function be generated all the time ? For example, #include <iostream> using namespace std; inline int foo() {
6
by: Ravi | last post by:
What is the difference between following function definitions: static int f1() { /* code here */ } and int f1()
9
by: Yannick | last post by:
Hi everyone - I am not quite sure to understand what is really going on when a function defined in one translation unit calls a function defined in a different translation unit without knowing...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.