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.
You don't. It's not possible to pass parameters the function expects if you don't know their types.