Cómo llamar dinámicamente a métodos de clase en archivos .so
#include lt; dlfcn.hgt; void *dlopen(const char *filename, int flag); char *dlerr(void); ); int dlclose(void *handle); 123456789
por ejemplo:
dlapi.c
/*
[root@localhost por ejemplo]# gcc main.c -Wl,-rpath=./ -ldl -D_TEST
[root@localhost por ejemplo]# g main.c -Wl,-rpath=./ -ldl -D_TEST
*/#incluye lt; stdio.hgt; #incluye lt; stdlib.hgt; #incluye lt; #incluye lt; int (*PCall_func0)(); typedef int (*PCall_func1)(void *); typedef int (*PCall_func2)(void *, typedef int (*PCall_func3)(void *, void *, void *); typedef int (*PCall_func4)(void *, void *, void *, void *); intdynamic_call_library_func0(char *libName, char *funcName)
{
void *handle; void *error;
PCall_func0 selffunc=NULL; int ret; if(libName == NULL)return -9000; if(funcName == NULL)return -9001; >
handle = dlopen(libName, RTLD_LAZY); if(!handle) { printf("s\n", dlerror());
}
ddlerror(); ; //obtener una función
selffunc = (PCall_func0)dlsym(handle, funcName); = NULL) { fprintf(stderr, "s\n", error); return -9001;
}
ret = selffunc(); DLL de identificador especificado, el sistema realmente lo desinstalará solo cuando el número de usos de la DLL llegue a cero.
dlclose(handle); return ret;
}intdynamic_call_library_func1(char *libName, char *funcName, void *argv1)
{
void * handle; void *error;
PCall_func1 selffunc=NULL; int ret; if(libName == NULL)return -9000; if(funcName == NULL)return -9001; Abra la biblioteca de enlaces dinámicos
handle = dlopen(libName, RTLD_LAZY); if (!handle) { printf("s\n", dlerror() return -9000;
} p >
dlerror(); //Obtener una función
selffunc = (PCall_func1)dlsym(handle, funcName); if ((error = dlerror()) != NULL) { fprintf(stderr) , "s\n", error); return -9001;
}
ret = selffunc(argv1); //dlclose se utiliza para cerrar la biblioteca de enlaces dinámicos especificada. manejar, solo cuando El sistema desinstalará la biblioteca de vínculos dinámicos solo cuando su recuento de uso sea 0.
dlclose(handle); return ret;
}intdynamic_call_library_func2(char *libName, char *funcName, void *argv1, void *argv2)
{
void *handle; void *error;
PCall_func2 selffunc=NULL; int ret; if(libName == NULL)return -9000; 9001; //abre la biblioteca de enlaces dinámicos
handle = dlopen(libName, RTLD_LAZY); if (!handle) { printf("s\n", dlerror()); p>
}
dlerror(); //obtener una función
selffunc = (PCall_func2)dlsym(handle, funcName) if ((error = dlerror()) ! = NULL) { fprintf(stderr, "s\n", error); return
}
ret = selffunc(argv1, argv2); Para cerrar la biblioteca de vínculos dinámicos del identificador especificado, el sistema realmente lo usará solo cuando el número de usos de la biblioteca de vínculos dinámicos sea 0.
dlclose se utiliza para cerrar la biblioteca de enlaces dinámicos del identificador especificado.
dlclose(handle); return ret;
}intdynamic_call_library_func3(char *libName, char *funcName, void *argv1, void *argv2, void *argv3)
{
/*
[root@localhost por ejemplo]# gcc por ejemplo.c -fPIC -shared -o libeggcc.so[root@localhost por ejemplo]# g por ejemplo. c -fPIC -shared -o libegg .so*/#include lt; #include lt; #include lt; socket.hgt; #incluye lt; netinet /in.hgt; #incluye lt; arpa/inet.hgt; int show1(char *src)
{ printf("s\n", src); devolver 100;
}int show2(int *x)
{ printf(" 2d\n", *x);
p.ej.cpp