Calling Function at runtime

L

leaf

How can i call arbirary functions at runtime, that with arbirary
parameters and types?

Can BOOST.Bind do that?
 
B

Ben Pope

leaf said:
How can i call arbirary functions at runtime, that with arbirary
parameters and types?

Can BOOST.Bind do that?

Arbitrary? In what way? Do you not know the type of the function at
compile time?

Can you show some code to demonstrate what you want to achieve?

Can you describe the problem you're trying to solve?

Ben Pope
 
D

Daniel T.

"leaf said:
How can i call arbirary functions at runtime, that with arbirary
parameters and types?

Can BOOST.Bind do that?

No. Based on your earlier questions, what you need is a map with the key
being the function signature and the value being a pointer to the
function.
 
L

leaf

Sorry for not being specific,
all i need to achieve is to call a function using some information
available only during runtime ( eg. function_id, parameter values etc.
)
 
J

JH Trauntvein

leaf said:
Sorry for not being specific,
all i need to achieve is to call a function using some information
available only during runtime ( eg. function_id, parameter values etc.
)

There are some languages (mostly interpreted languages I think) that
are able to take an arbitrary string and execute it as code. Neither C
nor C++ offer this as a language feature. That being said, it is not
terribly difficult (and is actually quite fun) to write a simple
expression parser and execution engine. I have written a parser that
converts an infix expression to postfix notation and a postfix
evaluation engine. Bjarne Stroustrup implemented a similar engine (a
calculator I believe) using recursive descent in his "The C++ Language)
third edition. I don't think that it would be difficult at all to
extend his example in any way that want. Yet another alternative is to
bind a scripting language to your program. Perl, Python, Javascript,
and many other languages provide facilities for doing precisely this.

Regards,

Jon Trauntvein
 
L

leaf

Indeed, what will i end up is creating a parser fo my application...
then, scripting engine.
I already had found metaprogram counterpart of the "calculator" example
you're referring.
Found it in Boost.Spirit quick start example.

Anyway, this is why i end up looking for boost implementation of
calling a 'function
at runtime' ....
 
A

AnonMail2005

1. Declare a function identifier (e.g. a string or
some type of int).

2. Declare a common function signature for the thing
you are trying to accomplish.

3. Define a container (e.g. a map) that can associate
a function identifier with a pointer to your signature
declared in #2.

4. Define your various functions.

5. Register your functions by inserting their unique
identifier along with their function pointer into
the map defined in #3.

6. Pass you worker function the function id and the
arguments the function needs. It will look up the
appropriate function to call in the map and invoke it.

The above will need to have all possible functions
that can be called defined. But the actual functions
called can be configurable with something external
to the program (e.g. a configuration file).

You can define an arbitrary function as arbitrarily
as you would like in #2 above. Function objects
may be useful in making existing functions conform
to your function signature.
 
A

AnonMail2005

leaf said:
Indeed you're method is correct, but problem arise when 'function
pointer' is not available, what i mean is the 'regular function
pointers' for declared functions/methods in our application...
Functions reside in a DLL linked using LIB file.
And an accompanying HEADER file for Client App usage...



Ayon kay (e-mail address removed):
It sounds like you are saying that a platform specific linker won't
link in a function from a shared library (DLL) if only a pointer to
a function is used? This doesn't sound correct but I have no
direct knowledge. Try it and see.

And if that is indeed the case, I'm sure there are methods to
pull in the function some other way. But that's OT to this NG.
 
K

krbyxtrm

a guide to CALLING ARBITRARY FUNCTIONS at RUNTIME
=====================================
Here's how:
1. Get the Function address from the DLL module (loader in memory) via
LoadLibrary
To get it, it may need some knowledge of Win32 PE Header, its easy
don't worry.
Specifically it can be found in the _IMAGE_EXPORT_DIRECTORY (see
MSDN for details)
2. There you may iterate, the AddressOfNameOrdinals, AddressOfNames,
and AddressOfFunctions using the NumberOfFunctions (these are all
members of IMAGE_EXPORT_DIRECTORY)
3. What is important to get is the Name, Address.
the name you will get is a mangled name, what that means is like
jumbled....
anyway, theres an API that can unmangle it to be a completly
readable. use UndecorateSymbolName, see MSDN for details,
ex. unmangled: void __cdecl Foo()

4. Now you may call the function using the address you got.
5. Use BOOST.Bind or ASM to call the function, whatever you prefer,
but take note that this assumes that you know the parameters of the
function,
if you need a better implementation, consider parsing the Unmangled
name.


krby_xtrm
 
A

Alf P. Steinbach

* krbyxtrm:
[Windows-specific, off-topic]

Please don't post completely off-topic messages in this newsgroup;
please read the FAQ and the welcome message.
 
K

krbyxtrm

Sorry alf, i use google groups, just replied here so quickly, that i
was not able to check, sory...

-krbyxtrm-
Please no more comments.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top