带有未知参数的 C 后期绑定 [英] C late binding with unknown arguments

查看:19
本文介绍了带有未知参数的 C 后期绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前需要调用很多在运行时提取的函数指针.问题是参数在编译时是未知的.

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.

但是,在运行时我收到数据,让我知道函数的参数,我什至可以将参数存储在 char* 数组中.问题是我没有一个函数指针模型可以将它转换成.

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.

在高级语言中,我知道有像InvokeMethode(String name,Byte[] args)"这样的函数可以像参数一样解释字节数组.由于C中不存在反射,所以我不希望用函数指针看到这一点.

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.

我想到的一个解决方案(它真的很糟糕)是在编译时创建一个函数指针模型,该模型将以硬编码方式"将 ptr 转换为正确的类型,如下所示:

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);

问题是我必须创建像这样的 100 个函数来正确投射指针.

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?

非常感谢!

推荐答案

不幸的是,这是一个难题,没有好的或简单的答案.

This is a hard problem without, unfortunately, good or easy answers.

参见这个以前的 SO 问题:运行时参数gcc(反 va_args/varargs)

See this former SO question: Run-time parameters in gcc (inverse va_args/varargs)

请参阅此 C 常见问题解答:http://c-faq.com/varargs/invvarargs.html

See this C FAQ question: http://c-faq.com/varargs/invvarargs.html

请参阅 C 常见问题列表维护者的古怪想法"集合:http://c-faq.com/varargs/wacky.html

See this collection of "wacky ideas" by the C FAQ list maintainer: http://c-faq.com/varargs/wacky.html

附录:见这个以前的问题:在C中如何通过传递多个参数的指针来调用函数?

Addendum: see this former SO question: How to call functions by their pointers passing multiple arguments in C?

...其中提到了libffi":http://sourceware.org/libffi/

...which mentions "libffi": http://sourceware.org/libffi/

这篇关于带有未知参数的 C 后期绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆