473,395 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Equivalent of stdarg.h but on the calling side ?

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
5 3011
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 (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.
Nov 13 '05 #2
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: horos | last post by:
hey all, I'm a heavy perl user, not so much a java script user, and was wondering... perl has an extremely nice utility called Data::Dumper, which allows you to dump out the contents of an...
7
by: am_ggh | last post by:
Is there a PHP equivalent of TCL's "upvar" ? I will appreciate any insights. Andy
8
by: Klaus Schneider | last post by:
Hi all! I'm having trouble with a template function with variable arguments when I parse an enum type as variable argument. Example: template <class T> bool test(int num, ...) { va_list ap;...
6
by: Clint Olsen | last post by:
I had this crazy idea to use stdarg macros to copy data in a generic fashion, regardless of type. However, I'm not sure it will work in part due to the ANSI C default argument promotions and the...
9
by: Mac A. Cody | last post by:
Hello, I'm encountering a problem with using stdarg that I cannot figure out. I'm trying to develop a function for a linux driver module that takes a variable-length sequence of u8-type...
3
by: cman | last post by:
Can somebody explain the following code segment, from stdargs.h (from linux 0.01) #define __va_rounded_size(TYPE) \ (((sizeof (TYPE) + sizeof (int) - 1) / sizeof (int)) * sizeof...
13
by: nariknahom | last post by:
Hi, Can someone tell me what is wrong with the below program? ------------------------------------------- int main() { f1( 25 50,"testing stdarg. ", "it is working", "if it is working." );...
18
by: Csaba Gabor | last post by:
Is there a straightforward way of implementing a PHP equivalent to window.setTimeout? In a javascript web app, it's often the case that things are done on an event driven basis. For example,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.