473,783 Members | 2,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_TY PE (*generic_funct ion_pointer)(SO ME_OTHER_UNKNOW N_TYPE);

SOME_OTHER_UNKN OWN_TYPE generic_argumen t_list;
SOME_UNKNOWN_TY PE generic_return_ value;
generic_functio n_pointer generic_f_ptr = (generic_functi on_pointer)f_pt r;

/* some magic to set generic_argumen t_list to fake the actual argument
list
*/
generic_return_ value = generic_f_ptr( generic_paramet er_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 3034
In article <pa************ *************** *@virgilio.it>
Francesco Bochicchio <bo*****@virgil io.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_TY PE (*generic_funct ion_pointer)(SO ME_OTHER_UNKNOW N_TYPE);

SOME_OTHER_UNKN OWN_TYPE generic_argumen t_list;
SOME_UNKNOWN_TY PE generic_return_ value;
generic_functio n_pointer generic_f_ptr = (generic_functi on_pointer)f_pt r;

/* some magic to set generic_argumen t_list to fake the actual argument
list
*/
generic_return_ value = generic_f_ptr( generic_paramet er_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 "technicall y correct" English; but since when was rock & roll "technicall y 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*****@virgil io.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
3337
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 arbitrary data structure. I'd like to do the same with javascript. print Data::Dumper(document)
7
2549
by: am_ggh | last post by:
Is there a PHP equivalent of TCL's "upvar" ? I will appreciate any insights. Andy
8
3454
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; int ind;
6
3850
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 fact that va_arg requires a type to decide how far to advance to subsequent arguments. So, essentially what I want in the variable argument function is something that just assigns to a character type, and then copy a predefined number of bytes to...
9
1829
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 values. Below is the function: #include <stdarg.h> .. ..
3
5127
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 (int))va_rounded_size #define va_start(AP, LASTARG) \ (__builtin_saveregs (), \ AP = ((char *) &(LASTARG) + __va_rounded_size (LASTARG)))
13
2144
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." ); return 0; }
18
15928
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, html elements might have things like onclick="buttonClicked()" to indicate that the function buttonClicked should be run when that element is clicked. The analogue can be done in PHP (on Windows) using com_event_sink. In other words, you can...
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10315
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9946
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.