Calling dynamically?

Discussion in 'C++' started by Alex Lyman, Nov 11, 2003.

  1. Alex Lyman

    Alex Lyman Guest

    Does anyone have any code handy (or know what a good direction for me to
    head in), to call functions, if you have an address of the function, its
    declspec (for my app, it's limited to _stdcall and thiscall), and what
    parameters it expects? I know all about the argument ordering on the stack,
    but I don't really know enough ASM to work with it. Does anyone out there
    know of a cheap way to do it in more standardized C++? (efficiency doesn't
    matter to to the project, really, so any random musings would be helpful)


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.536 / Virus Database: 331 - Release Date: 11/3/2003
     
    Alex Lyman, Nov 11, 2003
    #1
    1. Advertising

  2. Alex Lyman wrote:

    > Does anyone have any code handy (or know what a good direction for me to
    > head in), to call functions, if you have an address of the function, its
    > declspec (for my app, it's limited to _stdcall and thiscall), and what
    > parameters it expects? I know all about the argument ordering on the stack,
    > but I don't really know enough ASM to work with it. Does anyone out there
    > know of a cheap way to do it in more standardized C++? (efficiency doesn't
    > matter to to the project, really, so any random musings would be helpful)


    Look up "function pointers" or "pointers to functions" in your favorite
    C++ reference and in the C++ FAQ below. A function pointer is very
    similar to an address of a function, execpt that it _may_ have more
    information associated with it.

    #include <iostream>
    #include <cstdlib>
    using namespace std;

    typedef void (*P_VOID_FUNC)(void); // function pointer type.

    void hello(void);
    void goodbye(void);
    void like_my_hat(void);

    P_VOID_FUNC func_tbl[] =
    {
    hello, like_my_hat, hello, goodbye
    };
    const unsigned int NUM_FUNCS =
    sizeof(func_tbl) / sizeof(func_tbl[0]);

    int main(void)
    {
    for (unsigned i = 0; i < NUM_FUNCS; ++i)
    {
    (*func_tbl)(); // Call via function pointer.
    }
    return EXIT_SUCCESS;
    }

    void hello(void)
    {
    cout << "Hello.\n";
    return;
    }

    void like_my_hat(void)
    {
    cout << "Do you like my hat?\n";
    return;
    }

    void goodbye(void)
    {
    cout << "Goodbye.\n";
    return;
    }

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Nov 11, 2003
    #2
    1. Advertising

  3. On Tue, 11 Nov 2003 06:34:45 GMT, "Alex Lyman" <alex lyman @
    earthlink.net> wrote:

    >Does anyone have any code handy (or know what a good direction for me to
    >head in), to call functions, if you have an address of the function, its
    >declspec (for my app, it's limited to _stdcall and thiscall), and what
    >parameters it expects? I know all about the argument ordering on the stack,
    >but I don't really know enough ASM to work with it. Does anyone out there
    >know of a cheap way to do it in more standardized C++? (efficiency doesn't
    >matter to to the project, really, so any random musings would be helpful)


    <random-musings>
    If you know what parameters the function expects, there's no need to
    use ASM to do it. Just call the function.

    It becomes tricky when you have a *variable* number of parameters ...
    not for the function, which can be declared with the C++ syntax
    similar to:
    void foo(int, ...);
    or merely
    void foo(...);
    although it might be a good idea to have the count of arguments passed
    as the first argument.

    The problem, of course, is for the caller who might get input from,
    say, an XML file which has a list of arguments, the name of the
    function, and perhaps the name of the library as well. Somehow, the
    function call must be constructed in order to call the function in the
    library (assuming that it is in a shared library).

    Also, if the caller must call various types of functions, it will need
    to have a generic type (perhaps void* would work) or else need to have
    other knowledge about the types of arguments passed. If they are all
    of the same type, I might pass an array of pointers with the last
    element a NULL pointer. Also, you need to devise a way to handle input
    and output (or also in/out) arguments...

    Another approach would be to implement some kind of "COM" interface,
    i.e. a mechanism with a generic calling convention which allows
    clients to "discover" functions and the parameters they expect.

    HTH
    </random-musings>


    --
    Bob Hairgrove
     
    Bob Hairgrove, Nov 11, 2003
    #3
  4. Alex Lyman

    Alex Lyman Guest

    Perhaps I should describe my project in more detail:

    I want to open alot of standard C/C++ code up to be run from the console
    (for now -- a virtual machine is the ultimate project goal), so I need to
    have some way of associating text, with a function pointer and an actual
    description of the function (callspec, arguments, return type, and [for
    methods] class). I've gotten to the point where all of the data-collection
    part is working 100%; and I can parse input statements and search for the
    record of the function. All I have left is actually calling a function.

    Thanks for the tip of libffi, Gianni Mariani. However, I forgot to mention
    that my target platform is 16-bit DOS, and, as of yet, there isn't a libffi
    implimentation for it. Sure it will probably come in handy in the future,
    though, if I ever need to do something similar in VC++ or GCC. Might even
    figure out what I need to know from its source, maybe.

    Anyways, thanks for all the help y'all :)

    - Alex


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.536 / Virus Database: 331 - Release Date: 11/3/2003
     
    Alex Lyman, Nov 11, 2003
    #4
  5. Alex Lyman wrote:
    > Perhaps I should describe my project in more detail:
    >
    > I want to open alot of standard C/C++ code up to be run from the console
    > (for now -- a virtual machine is the ultimate project goal), so I need to
    > have some way of associating text, with a function pointer and an actual
    > description of the function (callspec, arguments, return type, and [for
    > methods] class). I've gotten to the point where all of the data-collection
    > part is working 100%; and I can parse input statements and search for the
    > record of the function. All I have left is actually calling a function.
    >
    > Thanks for the tip of libffi, Gianni Mariani. However, I forgot to mention
    > that my target platform is 16-bit DOS, and, as of yet, there isn't a libffi
    > implimentation for it. Sure it will probably come in handy in the future,
    > though, if I ever need to do something similar in VC++ or GCC. Might even
    > figure out what I need to know from its source, maybe.


    The concept is really very simple. You have an array that describes the
    args, you call an ASM routine that sets up a stack frame, and calls
    another C function to "fill in" the stack frame, when it returns the asm
    loads whatever registers need to be filled in and it calls the desired
    function, upon return the return value is stuffed in the right place.
     
    Gianni Mariani, Nov 12, 2003
    #5
  6. Alex Lyman

    Alex Lyman Guest

    Well, after lots of trial-and-error and digging through (and trying to
    comprehend) libffi, I figured some ASM code (definitly pushed my ASM
    know-how on this one) for __cdecl-type function calls -- so it shouldn't be
    too hard to add support for _stdcall and thiscall functions in the near
    future. Thanks, guys for all the help and direction-pointing!

    - Alex

    "Gianni Mariani" <> wrote in message
    news:bot186$...
    > Alex Lyman wrote:
    > > Perhaps I should describe my project in more detail:
    > >
    > > I want to open alot of standard C/C++ code up to be run from the console
    > > (for now -- a virtual machine is the ultimate project goal), so I need

    to
    > > have some way of associating text, with a function pointer and an actual
    > > description of the function (callspec, arguments, return type, and [for
    > > methods] class). I've gotten to the point where all of the

    data-collection
    > > part is working 100%; and I can parse input statements and search for

    the
    > > record of the function. All I have left is actually calling a function.
    > >
    > > Thanks for the tip of libffi, Gianni Mariani. However, I forgot to

    mention
    > > that my target platform is 16-bit DOS, and, as of yet, there isn't a

    libffi
    > > implimentation for it. Sure it will probably come in handy in the

    future,
    > > though, if I ever need to do something similar in VC++ or GCC. Might

    even
    > > figure out what I need to know from its source, maybe.

    >
    > The concept is really very simple. You have an array that describes the
    > args, you call an ASM routine that sets up a stack frame, and calls
    > another C function to "fill in" the stack frame, when it returns the asm
    > loads whatever registers need to be filled in and it calls the desired
    > function, upon return the return value is stuffed in the right place.
    >



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.536 / Virus Database: 331 - Release Date: 11/3/2003
     
    Alex Lyman, Nov 17, 2003
    #6
    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. Ryan Ternier
    Replies:
    4
    Views:
    4,043
    Cowboy \(Gregory A. Beamer\) [MVP]
    Jul 29, 2004
  2. =?Utf-8?B?TWFydGluIExlZQ==?=

    Calling FormsAuthentication.SignOut() after calling Response.Flush

    =?Utf-8?B?TWFydGluIExlZQ==?=, Sep 28, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    742
    =?Utf-8?B?RXRoZW0gQXp1bg==?=
    Sep 28, 2004
  3. Andreas Lagemann
    Replies:
    8
    Views:
    539
    Mike Wahler
    Jan 10, 2005
  4. tiwy
    Replies:
    0
    Views:
    461
  5. Klaus Friese

    c++ calling java calling c++ ...

    Klaus Friese, Aug 12, 2005, in forum: C++
    Replies:
    7
    Views:
    614
    Roland Pibinger
    Aug 15, 2005
Loading...

Share This Page