简体   繁体   中英

Calling function with variable arguments dynamically

Is there a possibility to call function with variable arguments from the C code dynamically?

For example I have text file which contains data (written using this scheme: function_name arguments) for example:

func1 1 2 3
func2 1
func3

My program written in C is parsing this file and looks in a populated array (which holds function name in string and target native function pointer) for function with given name by comparing the string and calls a pointer of this function with arguments from the text file. For example functions like that:

void func1(int a, int b, int c) { }
void func2(int a, int b) { }
void func3() { }

The problem is that even if I know the number of arguments, I don't know how to write in C function pointer call with dynamic number of arguments. Is there a possibility to populate va_list (I know that this is NOT a container or a typical array!) then pass to the native function or any other way to do this? The only way which came into my mind is populating dynarec block with x86 code for calling native function with variable arguments, but it's not a clean solution. Is such thing even possible in plain C?

If it is hard to understand just write and I'll try to explain better. And if you want to write "use va_list" - then read carefully my post once again.

Thanks in advance.

I like your way of thinking, because obviously, you are a true hacker, but...

do not try to do it like this .

The proper way of doing this is to go alter these functions so that each one of them accepts an array of int instead of individual int parameters. But I suppose that if you had the freedom to change them, you would have done it already and you would not be asking.

The next best way of doing it is to write a number of functions, conv1() , conv2() , conv3() etc, each accepting an array of int , and a pointer to a function which accepts individual int parameters. So, convN() accepts an array of N integers, and a pointer to a function which accepts N individual int parameters. It reads each int from the array and passes it to the function as a parameter. It can do this, because it has been specifically written to work with a function of precisely that number of parameters. Then, in your table with function names and pointers to functions, add a pointer to the right convN() function, depending on the number of parameters that the target function expects.

Don't hack it.

I'm self answering, because this will be a solution for other people. If you want to call functions with variable arguments dynamically without writing direct machine code just use avcall library from FFCALL . Quick reference guide of avcall library can be found here . It's a crossplatform library that makes this possible. For example to call function named func1 with return type void which takes three arguments and all of them are of type int just do this:

#include <avcall.h>

int a = 1, b = 2, c = 3;

av_alist alist;
av_start_void(alist,&func1);
av_int(alist,a);
av_int(alist,b);
av_int(alist,c);
av_call(alist);

You can of course use this for functions which returns value or takes arguments of different type, for more just look at avcall library manual page . :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM