use and examples of Dlopen, Dlsym and Dlclose
I've never had time to clean up these three functions before. Take the time to tidy up today.
1. Introduction to function
Dlopen
Basic definition
Function: Open a dynamic link library
Include header file:
#include <dlfcn.h>
function definition:
void * Dlopen (const char * pathname, int mode);
Function Description:
The Dlopen () function opens the specified dynamic connection library file in the specified mode and returns a handle to the calling process. Use Dlclose () to uninstall the Open library.
Mode: Divided into these two types
Rtld_lazy suspend the decision, and then when necessary to solve the symbol
Rtld_now immediately decided to remove all pending symbols before returning.
Rtld_local
Rtld_global allow export of symbols
Rtld_group
Rtld_world
return value:
Open error return NULL
Successful, return library reference
Compile time to join-LDL (Specify DL library)
Dlsym ()
Function: Returns the address of a symbol according to the action handle and symbol of the dynamic link library. Include header file: #include <dlfcn.h> function definition: void*dlsym (void* handle,const char* symbol) function Description: Dlsym based on dynamic link library action handle (handle) and symbol ( Symbol), returns the address of the sign. You can use this function not only to get the function address, but also to get the variable address. Handle is the pointer that is returned after the dynamic link library is opened by Dlopen, and symbol is the name of the function or global variable that you want to get.
Dlclose ()
Dlclose is used to close the dynamic-link library of the specified handle, which is actually uninstalled only if the use count of this dynamic link library is 0 o'clock. These are excerpts, summed up as a link when need to use the DL library, compile the time need to add Dlfcn.h header file. To ensure that the compilation does not error. 2, generate dynamic library hello.c function prototype:
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
typedef struct {
const char *module;
Int (*getvalue) (char *pszval);
Int (*printfhello) ();
} Hello_st_api;
int GetValue (char *pszval)
{
int retval =-1;
if (pszval)
retval = sprintf (pszval, "%s", "123456");
printf ("%s,%d, Pszver =%s\n", __function__, __line__, pszval);
return retval;
}
int Printfhello ()
{
int retval =-1;
printf ("%s,%d, hello everyone\n", __function__, __line__);
return 0;
}
Const HELLO_ST_API Hello = {
. module = "Hello",
GetValue,
Printfhello,
};
Compile with instructions: Gcc-shared-o hello.so hello.c
The above function is pointed to by a global structure, hello. In the DLSYM definition, you can obtain not only the address of a function, but also the address of a global variable. So here's a dlsym to get the address of the global variable. Benefit oneself slowly realize.
3, Dlopen Code
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>
typedef struct {
const char *module;
Int (*getvalue) (char *pszval);
Int (*printfhello) ();
} Hello_st_api;
int main (int argc, char **argv)
{
Hello_st_api *hello;
int i = 0;
void *handle;
Char psvalue[20] = {0};
Handle = Dlopen ("The absolute path of inventory, you can try the relative path is not possible", rtld_lazy);
if (! Handle) {
printf ("%s,%d, NULL = = handle\n", __function__, __line__);
return-1;
}
Dlerror ();
Hello = Dlsym (handle, "Hello");
if (!hello) {
printf ("%s,%d, NULL = = handle\n", __function__, __line__);
return-1;
}
if (Hello && hello->printfhello)
i = Hello->printfhello ();
printf ("%s,%d, i =%d\n", __function__, __line__, i);
if (Hello && hello->getvalue)
i = Hello->getvalue (Psvalue);
if (Hello && hello->module)
{
printf ("%s,%d, module =%s\n", __function__, __line__, hello->module);
}
Dlclose (handle);
return 0;
}
Compiler directive: Gcc-o test HELLO_DLOPEN.C-LDL
Run./test results are as follows.
Printfhello, hello everyone.
Main, +, I = 0
GetValue, Pszver = 123456
Main, all, module = Hello
You can see the results are normal.
See no use? Dlsym find the global structure Hello, you can use this global structure pointer directly to the function in the library, because we sometimes provide a library is not just a two functions, a general library will have more than one function, in this way can be used directly. Otherwise find the function name to write how many dlsym ah.