Calling Function at runtime

Discussion in 'C++' started by leaf, Mar 10, 2006.

  1. leaf

    leaf Guest

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

    Can BOOST.Bind do that?
     
    leaf, Mar 10, 2006
    #1
    1. Advertising

  2. leaf

    Ben Pope Guest

    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...
     
    Ben Pope, Mar 10, 2006
    #2
    1. Advertising

  3. 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
     
    Simon Elliott, Mar 10, 2006
    #3
  4. leaf

    Daniel T. Guest

    In article <>,
    "leaf" <> 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.
     
    Daniel T., Mar 10, 2006
    #4
  5. leaf

    leaf Guest

    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.
    )
     
    leaf, Mar 10, 2006
    #5
  6. 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
     
    JH Trauntvein, Mar 11, 2006
    #6
  7. leaf

    leaf Guest

    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
     
    leaf, Mar 11, 2006
    #7
  8. leaf

    Guest

    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, 2006
    #8
  9. leaf

    Guest

    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 :
    > > 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, 2006
    #9
  10. leaf

    leaf Guest

    I'll try it.
     
    leaf, Mar 13, 2006
    #10
  11. leaf

    krbyxtrm Guest

    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
     
    krbyxtrm, Apr 3, 2006
    #11
  12. * 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?
     
    Alf P. Steinbach, Apr 4, 2006
    #12
  13. * 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?
     
    Alf P. Steinbach, Apr 4, 2006
    #13
  14. leaf

    krbyxtrm Guest

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

    -krbyxtrm-
    Please no more comments.
     
    krbyxtrm, Apr 4, 2006
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anatoly
    Replies:
    1
    Views:
    9,041
    =?Utf-8?B?TmlsZXNodw==?=
    Feb 17, 2004
  2. Andreas Lagemann
    Replies:
    8
    Views:
    542
    Mike Wahler
    Jan 10, 2005
  3. tiwy
    Replies:
    0
    Views:
    465
  4. John Friedland
    Replies:
    18
    Views:
    567
    Adam Warner
    Jul 12, 2006
  5. John Friedland
    Replies:
    11
    Views:
    528
    Nicholas Howe
    Jul 14, 2006
Loading...

Share This Page