Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

dynamic - C late binding with unknown arguments

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...