I have used dlopen
to load an object and dlsym
to get a function pointer to a shared object function. Everything works fine. I have tested it calling then the shared function which (for now) only prints and it works-prints fine in the main program calling it. Now I want to pass two arguments to this function. An int
and a char *
.Can anyone help me understand how can I pass arguments to a shared function? I have searched in the web but I cannot understand how it works.
Load the function:
int (*func)(int x, char *y) = dlsym(dl_handle, "your_function");
You might well decide you need a cast on the return from dlsym()
; it is one of the uglinesses with dynamically loaded libraries.
Invoke it:
int i = 37;
char buffer[64];
int result1 = (*func)(i, buffer); // Old school — pre-C89 (but still works and is explicit)
int result2 = func(i+1, buffer); // New school — can leave you looking for the wrong thing.