Calling dynamically?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Lyman

    Calling dynamically?

    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



  • Thomas Matthews

    #2
    Re: Calling dynamically?

    Alex Lyman wrote:
    [color=blue]
    > 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)[/color]

    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(voi d);

    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[i])(); // Call via function pointer.
    }
    return EXIT_SUCCESS;
    }

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

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

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

    --
    Thomas Matthews

    C++ newsgroup welcome message:

    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.l earn.c-c++ faq:

    Other sites:
    http://www.josuttis.com -- C++ STL Library book

    Comment

    • Bob Hairgrove

      #3
      Re: Calling dynamically?

      On Tue, 11 Nov 2003 06:34:45 GMT, "Alex Lyman" <alex lyman @
      earthlink.net> wrote:
      [color=blue]
      >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)[/color]

      <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
      NoSpamPlease@Ho me.com

      Comment

      • Alex Lyman

        #4
        Re: Calling dynamically?

        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



        Comment

        • Gianni Mariani

          #5
          Re: Calling dynamically?

          Alex Lyman wrote:[color=blue]
          > 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.[/color]

          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.

          Comment

          • Alex Lyman

            #6
            Re: Calling dynamically?

            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" <gi2nospam@mari ani.ws> wrote in message
            news:bot186$35l @dispatch.conce ntric.net...[color=blue]
            > Alex Lyman wrote:[color=green]
            > > 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[/color][/color]
            to[color=blue][color=green]
            > > 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[/color][/color]
            data-collection[color=blue][color=green]
            > > part is working 100%; and I can parse input statements and search for[/color][/color]
            the[color=blue][color=green]
            > > 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[/color][/color]
            mention[color=blue][color=green]
            > > that my target platform is 16-bit DOS, and, as of yet, there isn't a[/color][/color]
            libffi[color=blue][color=green]
            > > implimentation for it. Sure it will probably come in handy in the[/color][/color]
            future,[color=blue][color=green]
            > > though, if I ever need to do something similar in VC++ or GCC. Might[/color][/color]
            even[color=blue][color=green]
            > > figure out what I need to know from its source, maybe.[/color]
            >
            > 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.
            >[/color]


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


            Comment

            Working...