C late binding with unknown arguments

痞子三分冷 提交于 2019-11-26 18:41:13

问题


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!


回答1:


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/



来源:https://stackoverflow.com/questions/34885868/c-late-binding-with-unknown-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!