2

我正在尝试使用 dlsym 和 dlopen 创建一个通用工具,目的是加载外部库并从中调用特定函数。我当前的工具代码是:

void bootload(string libraryname, string functionname, int argc, char** argv) {
    void *handle;
    char *error;

    handle = dlopen(libraryname.c_str(), RTLD_LAZY);

    if (!handle) {
        fprintf(stderr, "%s\n", dlerror());
        exit(EXIT_FAILURE);
    } else {
        cout << "\nSuccessfuly opened " << libraryname << endl;
    }

    dlerror();

    typedef void (*bootload_t)();
    bootload_t bl_function = (bootload_t) dlsym(handle, functionname.c_str());

    if ((error = dlerror()) != NULL)  {
        fprintf(stderr, "%s\n", error);
        exit(EXIT_FAILURE);
    }

    bl_function();
    dlclose(handle);
}

现在 argc 和 argv 将分别包含函数 functionname 的参数的数量和实例。

如何通过传递正确的参数并返回正确的类型来正确调用函数名?

一些帮助将不胜感激。

4

1 回答 1

2

如何使用 dlsym() 调用函数,其中我知道参数的数量但不知道类型,也不知道函数的返回类型?

你没有。如果您不知道函数的类型,则无法传递函数期望的参数

于 2014-01-11T21:40:05.313 回答