By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,712 Members | 1,540 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,712 IT Pros & Developers. It's quick & easy.

Calling Function at runtime

P: n/a
How can i call arbirary functions at runtime, that with arbirary
parameters and types?

Can BOOST.Bind do that?

Mar 10 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
leaf wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Mar 10 '06 #2

P: n/a
On 10/03/2006, leaf wrote:
How can i call arbirary functions at runtime, that with arbirary
parameters and types?


You need a functor. The most useful example I've seen is from Andrei
Alexandrescu's book "Modern C++ Design (ISBN 0201704315).

Some useful info here:
http://www.codeproject.com/cpp/genfunctors.asp

--
Simon Elliott http://www.ctsn.co.uk
Mar 10 '06 #3

P: n/a
In article <11**********************@i40g2000cwc.googlegroups .com>,
"leaf" <ad********@gmail.com> wrote:
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.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 10 '06 #4

P: n/a
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.
)

Mar 10 '06 #5

P: n/a

leaf wrote:
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

Mar 11 '06 #6

P: n/a
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' ....

---
LEAF

Ayon kay JH Trauntvein:
leaf wrote:
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


Mar 11 '06 #7

P: n/a
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.

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

Can BOOST.Bind do that?


Mar 12 '06 #8

P: n/a

leaf wrote:
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 An**********@gmail.com:
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.

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

Can BOOST.Bind do that?

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.

Mar 13 '06 #9

P: n/a
I'll try it.

Mar 13 '06 #10

P: n/a
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

Apr 3 '06 #11

P: n/a
* leaf:
I'll try it.


Please quote what you're responding to; please read the FAQ and the
welcome message.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 4 '06 #12

P: n/a
* krbyxtrm:
[Windows-specific, off-topic]


Please don't post completely off-topic messages in this newsgroup;
please read the FAQ and the welcome message.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 4 '06 #13

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

-krbyxtrm-
Please no more comments.

Apr 4 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.