Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Function from Dynamic Library

What would be the best way to, if possible, load a dynamic library and call one of it's functions, when we don't know the name of the function during compile-time?

For example, is there a way to make a program that reads a string from a file and then loads a DLL and searches for and calls a function with its name being the string read from the file?

Help would be very much appreciated.

like image 707
slartibartfast Avatar asked Apr 02 '11 01:04

slartibartfast


People also ask

How do you call a function in a library?

To call a function in function library, you specify the library and function name. For an example of how to call functions in a library that uses JavaScript, refer to the example in the Load function, see Load. For IPL, use the following example which uses the following format: function_library.

How do you call a function in a library C++?

To call a function funcname in C++ library libname with input arguments arg1,arg2,... and output argument retVal , use the MATLAB clib package. MATLAB automatically loads the library when you type: retVal = clib. libname.

What library is Dlopen in?

dlopen() and dlclose() are present in glibc 2.0 and later. dlmopen() first appeared in glibc 2.3.

How do dynamic libraries work?

Dynamic libraries are linked during the execution of the final executable. Only the name of the dynamic library is placed in the final executable. The actual linking happens during runtime, when both executable and library are placed in the main memory.


1 Answers

There is an example on Wikipedia of all places showing how to use the LoadLibrary() function at runtime. You will see that the function name is specified as a string. You would need to write the code to search for the function name and pass it to similar code.

On Linux you can do this with dlopen() and dlsym() functions.

like image 61
Brian Lyttle Avatar answered Oct 21 '22 21:10

Brian Lyttle