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

Calling Function at runtime

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

Can BOOST.Bind do that?

Mar 10 '06 #1
13 3320
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
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
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
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

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
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
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

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
I'll try it.

Mar 13 '06 #10
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
* 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
* 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: komal | last post by:
hi all basically my problem is i have to write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
4
by: Zeebra3 | last post by:
Here goes: I have a web form with several asp:dropdownlists, with which, when selection is changed I want to fire an event defined in some clientside js. The content of the clientside code is...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
4
by: Edwin Gomez | last post by:
I'm a C# developer and I'm new to Python. I would like to know if the concept of Asynchronous call-backs exists in Python. Basically what I mean is that I dispatch a thread and when the thread...
10
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
12
by: mcarthybn | last post by:
hi , I am working on huge C programming project.I am doin debugging only by printf measn i dont have any dubugger .So it is very difficult know who is actually calling a function and i am stuck...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.