晚期绑定与未知参数 [英] C late binding with unknown arguments
问题描述
但是,在运行时,我收到的数据可以让我知道这个函数的参数,甚至可以存储char *数组中的参数。问题是我没有一个函数指针模型来转换。
在高级语言中,我知道有一些函数,如InvokeMethode(String name, Byte [] args)解释字节数组,如参数。因为C中不存在反射,所以我没有希望看到这个函数指针。
我想到的一个解决方案(真的很糟糕)在编译时创建一个函数指针的模型,将以hardcoded方式将ptr转换为正确的类型,使用如下:
void callFunc64Bits(void * funcPtr,long long args);
void callFuncVoid(void * funcPtr);
问题是我必须创建像这样的100个函数才能正确地转换指针。
有更好的方法吗?
非常感谢!
这是一个难题没有,不幸的是,好的或简单的答案。
看到这个以前的问题: gcc(反向va_args / varargs)中的运行时参数
看到这个C常见问题: http://c-faq.com/varargs/invvarargs.html
请通过C常见问题列表维护者查看wacky ideas的集合: http://c-faq.com/varargs/wacky.html
附录:请参阅以前的SO问题:如何通过传递多个参数的指针来调用函数在C?中
...其中提到libffi: http://sourceware.org/libffi/
I am presently in a case where I need to call a lot of function pointers that has been extracted at runtime. The problem is that the arguments are unknown at compilation time.
But, at runtime I receive datas that allows me to know the arguments of the function and I can even store the arguments in a char* array. The problem is that I don't have a function pointer model to cast it into.
In high level language, I know there is function like "InvokeMethode(String name,Byte[] args)" that interpret the bytes array like arguments. Since reflection does not exist in C, I have no hope to see this with a function pointer.
One solution that I have in mind (and it's really bad), is to create a model of function pointer at compilation time that will cast in a "hardcoded way" the ptr to the right type to use like this:
void callFunc64Bits(void* funcPtr,long long args);
void callFuncVoid(void* funcPtr);
The problem is that I will have to create like 100 function like this that will cast the pointer correctly.
Is there a way to do it more efficiently?
Thank you very much!
This is a hard problem without, unfortunately, good or easy answers.
See this former SO question: Run-time parameters in gcc (inverse va_args/varargs)
See this C FAQ question: http://c-faq.com/varargs/invvarargs.html
See this collection of "wacky ideas" by the C FAQ list maintainer: http://c-faq.com/varargs/wacky.html
Addendum: see this former SO question: How to call functions by their pointers passing multiple arguments in C?
...which mentions "libffi": http://sourceware.org/libffi/
这篇关于晚期绑定与未知参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!