Equivalent of stdarg.h but on the calling side ?

  • Thread starter Francesco Bochicchio
  • Start date
F

Francesco Bochicchio

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
 
C

Chris Torek

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

Peter Shaggy Haywood

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"?
 
F

Francesco Bochicchio

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

Francesco Bochicchio

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!


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top