📄 dltest.c
字号:
/*************************************************************/
/* Test Linux Dynamic Function Loading */
/* */
/* void *dlopen(const char *filename, int flag) */
/* Opens dynamic library and return handle */
/* */
/* const char *dlerror(void) */
/* Returns string describing the last error. */
/* */
/* void *dlsym(void *handle, char *symbol) */
/* Return pointer to symbol's load point. */
/* If symbol is undefined, NULL is returned. */
/* */
/* int dlclose (void *handle) */
/* Close the dynamic library handle. */
/* */
/* */
/* */
/*************************************************************/
#include<stdio.h>
#include <stdlib.h>
/* */
/* 1-dll include file and variables */
/* */
#include <dlfcn.h>
void *FunctionLib; /* Handle to shared lib file */
int (*Function)(); /* Pointer to loaded routine */
const char *dlError; /* Pointer to error string */
char dllName[10][128];
char dllNameSo[128];
main( argc, argv )
{
int rc; /* return codes */
char HelloMessage[] = "HeLlO WoRlD\n";
FILE *fp;
int i = 0, j;
if((fp = fopen("dll.txt", "r")) == NULL){
printf("\nopen dll.txt fail");
exit(0);
}
while(!feof(fp) && i < 10){
fscanf(fp, "%s\n", dllName[i]);
printf("\nread file \"%s\"", dllName[i]);
i++;
}
printf("\nread %d dll file", i);
/* */
/* 2-print the original message */
/* */
printf("\n dlTest 2-Original message \n");
printf("%s", HelloMessage);
/* */
/* 3-Open Dynamic Loadable Libary with absolute path */
/* */
for(j = 0; j < i; j++){
sprintf(dllNameSo, "%s.so", dllName[j]);
printf("\n Now use dll %s", dllNameSo);
FunctionLib = dlopen(dllNameSo, RTLD_LAZY);
dlError = dlerror();
printf(" dlTest 3-Open Library %s return-%s- \n", dllNameSo, dlError);
if( dlError )
exit(1);
Function = dlsym(FunctionLib, dllName[j]);
dlError = dlerror();
printf(" dlTest 4-Find symbol print %s return-%s- \n", dllNameSo, dlError);
if( dlError )
exit(1);
rc = (*Function)( HelloMessage );
printf(" dlTest 5-print %s return-%s- \n", dllNameSo, dlError);
rc = dlclose(FunctionLib);
dlError = dlerror();
printf(" dlTest 6-Close handle return-%s-\n",dlError);
if( rc )
exit(1);
}
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -