views:

91

answers:

1

i have a c-application which should load a shared object at runtime and call an arbitrary function of the shared object. The shared object was build by the user and gave me the signature of his function.

 int func(int a, int b, double c)

i use dlopen to load the object and dlsym to get a function pointer to the shared object function. Now i have to pass the parameter to the function. I don't now the signature at compile time. Whats the best way to do it? Is there any chance to check signature before i call function in my application?

Thank you

+3  A: 

Please have a look at the C/C++ function pointer tutorial. It should answer all of your questions.

With regard to checking at runtime: No, you cannot check the signature for fit because C does not support reflection. dlsym() usually does not return a proper function pointer but a (void *) which you have to cast. Using arbitrary shared functions whose signature you do not know beforehand is a sign of a bad design. My advice would be to rethink the architecture.

Thorsten79