By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,515 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

Equivalent of stdarg.h but on the calling side ?

P: n/a
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_TYP E);

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

Nov 13 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In article <pa****************************@virgilio.it>
Francesco Bochicchio <bo*****@virgilio.it> 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 (4039.22'N, 11150.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.
Nov 13 '05 #2

P: n/a
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_TYP E);

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"?
Nov 13 '05 #3

P: n/a
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


Nov 13 '05 #4

P: n/a
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
Nov 13 '05 #5

P: n/a
On Fri, 04 Jul 2003 15:13:52 GMT
"Francesco Bochicchio" <bo*****@virgilio.it> 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.
Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.