【发布时间】:2010-05-12 11:44:25
【问题描述】:
鉴于我有一个指向函数的指针(例如由dlsym() 提供)和一个类型化参数的链接列表,我如何使用这些参数构造一个 C 函数调用?
例子:
struct param {
enum type { INT32, INT64, STRING, BOOL } type;
union { int i32; long long i64; char *str; bool b; } value;
struct param *next;
};
int call_this(int (*function)(), struct param *args)
{
int result;
/* magic here that calls function(), which has a prototype of
f(int, long long, char *, bool); , when args consist of a linked list of
INT32, INT64, STRING, BOOL types. */
return result;
}
操作系统是 Linux。我希望该解决方案能够跨 MIPS、PPC 和 x86(全 32 位)架构移植,使用 GCC 作为编译器。
谢谢!
【问题讨论】:
-
...准备好进行一些程序集黑客攻击,因为据我所知,纯 C 不会那样做。
-
...或者您可以使用方便的库。
标签: c linux gcc function arguments