Teen Programmers Unite  
 

 

Return to forum top

Strings to Function Calls

Posted by DarkVengeance [send private reply] at April 23, 2001, 02:30:06 AM

Basically I have a string, lets say "DoFunction". How do I take that string and then tell the program to execute the function with that same name? I don't want to use a look-up table or a bunch of if statements or anything else right now, I just want to see how to do it, if it's even possible. I'm writing my own scripting engine and I want it to be as flexible as possible, meaning hard-coing as little as possible. I don't know if it can be done since I've never tried before.

Posted by lordaerom [send private reply] at April 23, 2001, 09:10:57 AM

The answer is 'no'. It's 'no' because, as I read what you've said, you don't indicate what language you're doing this in. If it's C or C++, then, the answer is no, you can't passs "foo(23,42,23)" and have function foo executed with those parameters, at least without breaking it up, mapping the string "foo" to a function pointer, and even then, it'd get tricky passing arguments in that manner. The best you could get most likely would be a hastable of some kind, where you lookup the function name, then obtain a pointer/struct with a pointer to the function, executing via that.

Posted by taubz [send private reply] at April 23, 2001, 09:43:37 AM

Of course, if you're doing this in Perl, then exec("foo(23,42,23)")! :-)

- taubz

Posted by Psion [send private reply] at April 23, 2001, 09:54:46 AM

Java has an ample reflection API that lets you do that and more, however. :-)

Posted by taubz [send private reply] at April 24, 2001, 02:16:12 PM

On Sunday I started to delve into just that. I was trying to see how complex it was to make my own version of MS's .NET in Java by wrapping function calls through an invoke routine that either send the commands (through the reflection API) to a local object or through a socket to a remote object.

That was fun. Realized there were some interesting problems to deal with, but it doesn't seem like .NET is anything terribly special, from a design standpoint, anymore.

- taubz

Posted by gian [send private reply] at April 26, 2001, 02:36:14 AM

I think the whole .NET thing is a final push by Microsoft to take over the WORLD!!

(Or it might just be me and my paranoid delusions)

Posted by Psion [send private reply] at April 26, 2001, 09:40:56 AM

Paranoid delusions are beautiful things. Don't let anybody crush your dreams.

Posted by pramod [send private reply] at April 26, 2001, 01:24:58 PM

if your program is under win32, you could use the GetModuleHandle and the GetProcAddress functions to do something of this sort. But for this you'll have to '_export' all the functions you want to call.

Try:
HMODULE hMod = GetModuleHandle(NULL);
FARPROC foo = GetProcAddress(hMod, "doFunction"); // or whatever else it is

I think a similar dynamic loading method should be possible under other API's as well.

Posted by taubz [send private reply] at April 27, 2001, 12:17:47 PM

To use that you'd need to do a lot of type-checking and maniplation of parameters to the dynamically accessed functions anyway, so I doubt that would help a new language much. But, good idea.

- taubz

Posted by faboo [send private reply] at June 06, 2001, 12:57:03 AM

Hrrmmm...you know, theoretically, that should be possible in C/C++. There's a lookup table in the binary (and most probably in memory as well) of all of the function names (as well as variables) and their addresses. Granted, if you're talking about a prog that lookup table is usually stripped from the executable. _If_ you knew how you might access that table, however, you could put all the relevent functions in an .so/.dll, which absolutely _require_ such a lookup table to be in place.
How you would do it, I have no idea. Obviously, it would be different on different platforms, and even then you might have different binary types (witness ELF and a.out on linux).

faboo

Posted by pramod [send private reply] at June 10, 2001, 01:12:47 PM

I don't think there's anything like lookup table. Of course, there is a symbol table at compile time but NOT at runtime. There is something like a lookup table used to implement virtual functions but it doesn't have function names.

About the dll methods, what I've said is exactly what faboo has said, but mine applies only to Win32. If you put the functions in a dll, then you'll just have to use LoadLibrary in the beginning. But I'm beginning to have serious doubts about the overhead of calling the function each time you process a function call. I think the best method would be use a hashtable or a sorted vector with coupled with a binary search based lookpup algorithm seems to be the best idea.

Posted by Psion [send private reply] at June 10, 2001, 01:18:45 PM

<DeadHorse> STOP BEATING ME TO DEATH

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.