Equivalent of stdarg.h but on the calling side ?

Discussion in 'C Programming' started by Francesco Bochicchio, Jul 1, 2003.

  1. Hi all,

    anybody knows if there is a (standard, portable) way to dinamically build
    a list of parameters to call a C function? Something like va_start & co,
    but to be used on the calling side?

    In other words, suppose that I have a generic pointer :
    void * f_ptr;

    I know that this pointer points to a fuction. I also know the function
    parameter types and return types, but only at run time, so I cannot cast
    the pointer to something more specific, but have to find a way to call the
    pointed function in a generic way, doing something like :

    typedef SOME_UNKNOWN_TYPE (*generic_function_pointer)(SOME_OTHER_UNKNOWN_TYPE);

    SOME_OTHER_UNKNOWN_TYPE generic_argument_list;
    SOME_UNKNOWN_TYPE generic_return_value;
    generic_function_pointer generic_f_ptr = (generic_function_pointer)f_ptr;

    /* some magic to set generic_argument_list to fake the actual argument
    list
    */
    generic_return_value = generic_f_ptr( generic_parameter_list );
    /*
    * some more magic to extract from generic_return_value the actual
    * return value
    */

    The reason for this is that I would like to be able to do the following:
    - load a Linux/Unix dynamic library with dlopen
    - given a function name, get the relevant handle (a void *pointer) with dlsym
    - by parsing the include files related to the dynamic library, find out
    the function parameters and return type (that are therefore known only
    at run-time)
    - call the fuction using its handle in a 'generic' way, as I tried to
    explain above.


    I guess I'm asking too much from C, but one never knows :)


    Thanks for any hint you can give me.


    Ciao
    -----
    FB
     
    Francesco Bochicchio, Jul 1, 2003
    #1
    1. Advertising

  2. Francesco Bochicchio

    Chris Torek Guest

    In article <>
    Francesco Bochicchio <> writes:
    >... I would like to be able to ...
    >given a function name [or a pointer to it, found at runtime] ...
    >[then] find out the function parameters and return type (that
    >are therefore known only at run-time)
    >- call the fuction using its handle in a 'generic' way


    FAQ, 15.13.

    I see Steve Summit just posted the FAQ today. It should be on
    your local news server.
    --
    In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://67.40.109.61/torek/index.html (for the moment)
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, Jul 2, 2003
    #2
    1. Advertising

  3. Groovy hepcat Francesco Bochicchio was jivin' on Tue, 01 Jul 2003
    21:32:15 GMT in comp.lang.c.
    Equivalent of stdarg.h but on the calling side ?'s a cool scene! Dig
    it!

    >anybody knows if there is a (standard, portable) way to dinamically build
    >a list of parameters to call a C function? Something like va_start & co,
    >but to be used on the calling side?


    There's no way built into the language. However, you can create such
    a thing.
    One way might be to create a stack, and push your arguments onto it.
    Pass (a pointer to) the stack to your function, and have it pop items
    off.

    >In other words, suppose that I have a generic pointer :
    > void * f_ptr;
    >
    >I know that this pointer points to a fuction. I also know the function


    No, you don't know any such thing. In fact, a pointer to void cannot
    point to a function. You need a function pointer for that.

    >parameter types and return types, but only at run time, so I cannot cast


    Perhaps a function lookup table is in order. I don't know. The
    problem sounds a little vague. It's hard to say what the best solution
    might be.

    >the pointer to something more specific, but have to find a way to call the
    >pointed function in a generic way, doing something like :
    >
    > typedef SOME_UNKNOWN_TYPE (*generic_function_pointer)(SOME_OTHER_UNKNOWN_TYPE);
    >
    > SOME_OTHER_UNKNOWN_TYPE generic_argument_list;
    > SOME_UNKNOWN_TYPE generic_return_value;
    > generic_function_pointer generic_f_ptr = (generic_function_pointer)f_ptr;
    >
    > /* some magic to set generic_argument_list to fake the actual argument
    > list
    > */
    > generic_return_value = generic_f_ptr( generic_parameter_list );
    > /*
    > * some more magic to extract from generic_return_value the actual
    > * return value
    > */
    >
    >The reason for this is that I would like to be able to do the following:
    >- load a Linux/Unix dynamic library with dlopen
    >- given a function name, get the relevant handle (a void *pointer) with dlsym
    >- by parsing the include files related to the dynamic library, find out
    > the function parameters and return type (that are therefore known only
    > at run-time)
    >- call the fuction using its handle in a 'generic' way, as I tried to
    >explain above.


    I'm not sure what this means. What do you intend to pass to this
    function? Functions are generally used by passing a set of inputs
    (determined beforehand) and reading the output, in order to perform a
    certain task for a certain reason. But you seem to want to pick a
    random function, read its interface from a header, and call it with
    random data.

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
     
    Peter Shaggy Haywood, Jul 4, 2003
    #3
  4. On Fri, 04 Jul 2003 02:30:20 +0000, Peter "Shaggy" Haywood wrote:

    >
    >>anybody knows if there is a (standard, portable) way to dinamically build
    >>a list of parameters to call a C function? Something like va_start & co,
    >>but to be used on the calling side?

    >
    > There's no way built into the language. However, you can create such
    > a thing.
    > One way might be to create a stack, and push your arguments onto it.
    > Pass (a pointer to) the stack to your function, have it pop items off.
    >


    This was my idea, sort of, but my 'problem' is that the called function
    shall be left unaware of the trick. So I was wondering if there is some way
    to call a function by dynamically building something that can 'fake' the
    function arguments.
    I was almost sure that the answer was "you can't" (as the other answer to
    my post suggests), but it was worth asking.

    >>In other words, suppose that I have a generic pointer :
    >> void * f_ptr;
    >>
    >>I know that this pointer points to a fuction. I also know the function

    >
    > No, you don't know any such thing. In fact, a pointer to void cannot
    > point to a function. You need a function pointer for that.
    >


    I know. But do a 'man dlopen' on a Linux/Unix machine. You will see that
    the return value of 'dlsym' _is_ a void *, that yu can cast to whatever
    function pointer you want and then use it (of course if the function
    pointer does not correspond to the actual function interface, you are in
    trouble).


    ....

    >
    > I'm not sure what this means. What do you intend to pass to this
    > function? Functions are generally used by passing a set of inputs
    > (determined beforehand) and reading the output, in order to perform a
    > certain task for a certain reason. But you seem to want to pick a random
    > function, read its interface from a header, and call it with random data.


    Yes, except for the random data part.
    What I was thinking of is a generic way to allow interpreted languages
    (python, perl, ... ) to call C functions in a dynamic library without having
    to write a specific wrapper module for it. In other words, write a sort of
    meta-module wich allows to interface any C library compiled in one or more
    dynamic libraries).
    The general idea would be (on a Linux/Unix platform):
    1. dynamically load the library with dlopen
    2. get the function pointer of the function to call with dlsym
    3. figure the function interface from its prototype in the library include file(s)
    4. call the function by 'faking' its argument list

    The triky, may be impossible, part is step 4.

    Thanks anyway.

    Ciao
    -----
    FB
     
    Francesco Bochicchio, Jul 4, 2003
    #4
  5. On Fri, 04 Jul 2003 02:30:20 +0000, Peter "Shaggy" Haywood wrote:

    > Groovy hepcat Francesco Bochicchio was jivin' on Tue, 01 Jul 2003 21:32:15
    > GMT in comp.lang.c.
    > Equivalent of stdarg.h but on the calling side ?'s a cool scene! Dig it!
    >
    >>anybody knows if there is a (standard, portable) way to dinamically build
    >>a list of parameters to call a C function? Something like va_start & co,
    >>but to be used on the calling side?

    >
    > There's no way built into the language. However, you can create such
    > a thing.
    > One way might be to create a stack, and push your arguments onto it.
    > Pass (a pointer to) the stack to your function, and have it pop items off.
    >


    I found what I was looking for : libffi (part of the GNU compiler).

    http://sources.redhat.com/libffi/

    Ciao
    -----
    FB
     
    Francesco Bochicchio, Jul 4, 2003
    #5
  6. Francesco Bochicchio

    Mark Gordon Guest

    On Fri, 04 Jul 2003 15:13:52 GMT
    "Francesco Bochicchio" <> wrote:

    > On Fri, 04 Jul 2003 02:30:20 +0000, Peter "Shaggy" Haywood wrote:
    >
    > > Groovy hepcat Francesco Bochicchio was jivin' on Tue, 01 Jul 2003
    > > 21:32:15 GMT in comp.lang.c.
    > > Equivalent of stdarg.h but on the calling side ?'s a cool scene! Dig
    > > it!
    > >
    > >>anybody knows if there is a (standard, portable) way to dinamically
    > >>build a list of parameters to call a C function? Something like
    > >>va_start & co, but to be used on the calling side?

    > >
    > > There's no way built into the language. However, you can create
    > > such
    > > a thing.
    > > One way might be to create a stack, and push your arguments onto
    > > it.
    > > Pass (a pointer to) the stack to your function, and have it pop
    > > items off.
    > >

    >
    > I found what I was looking for : libffi (part of the GNU compiler).
    >
    > http://sources.redhat.com/libffi/


    Just remember that it is *not* standard C so it is probably only going
    to work with gcc. It is also OT here, so you will have to find somewhere
    else to ask for any help you need with it.
    --
    Mark Gordon
    Paid to be a Geek & a Senior Software Developer
    Although my email address says spamtrap, it is real and I read it.
     
    Mark Gordon, Jul 4, 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. Clint Olsen

    Using stdarg with unknown types

    Clint Olsen, Oct 27, 2003, in forum: C Programming
    Replies:
    6
    Views:
    420
    Sheldon Simms
    Oct 29, 2003
  2. Mac A. Cody

    Confusion with stdarg

    Mac A. Cody, Jan 3, 2005, in forum: C Programming
    Replies:
    9
    Views:
    452
    Lawrence Kirby
    Jan 4, 2005
  3. Klaus Schneider

    template with stdarg: enum problem

    Klaus Schneider, Oct 12, 2005, in forum: C++
    Replies:
    8
    Views:
    1,028
    Klaus Schneider
    Oct 12, 2005
  4. cman

    stdarg definitions

    cman, Mar 20, 2007, in forum: C Programming
    Replies:
    3
    Views:
    514
    Chris Torek
    Mar 25, 2007
  5. Obnoxious User

    stdarg problems

    Obnoxious User, Jul 1, 2007, in forum: C++
    Replies:
    14
    Views:
    651
    James Kanze
    Jul 3, 2007
Loading...

Share This Page