假设我有一个指向函数的指针(例如由 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 作为编译器。
谢谢!
最佳答案
您可能需要使用 libffi .
关于在 C 中构建函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2818424/