简体   繁体   中英

How do I invoke a function using dlsym() where I know the number of arguments but not the type and neither the return type of the function?

I am trying to create a generic tool using dlsym and dlopen with the intention of loading an external library and calling a particular function from it. My current code for the tool is:

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

Now argc and argv will contain the number and instances of the arguments respectively of the function functionname.

How do I call functionname correctly by passing the right arguments and returning the right type?

Some help would be much appreciated.

How do I invoke a function using dlsym() where I know the number of arguments but not the type and neither the return type of the function?

You don't. It's not possible to pass parameters the function expects if you don't know their types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM