首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

为 Linux 应用程序编写 DLL --清单(应用程序和 dll)

为 Linux 应用程序编写 DLL --清单(应用程序和 dll)

清单(应用程序和 dll)dlTest.c:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*************************************************************/
/*     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     */
main( argc, argv )
{
  int   rc;             /*  return codes            */
  char HelloMessage[] = "HeLlO WoRlD\n";
  
/*                              */
/* 2-print the original message                 */
/*                              */
  printf("  dlTest  2-Original message \n");
  printf("%s", HelloMessage);
/*                                               */
/*  3-Open Dynamic Loadable Libary with absolute path      */
/*                                              */
  FunctionLib = dlopen("/home/dlTest/UPPERCASE.so",RTLD_LAZY);
  dlError = dlerror();
  printf("  dlTest  3-Open Library with absolute path return-%s- \n", dlError);
  if( dlError ) exit(1);
/*                              */
/* 4-Find the first loaded function */
/*                              */
  Function    = dlsym( FunctionLib, "printUPPERCASE");
  dlError = dlerror();
  printf("  dlTest  4-Find symbol printUPPERCASE return-%s- \n", dlError);
  if( dlError ) exit(1);
/*                              */
/* 5-Execute the first loaded function              */
/*                              */
  rc = (*Function)( HelloMessage );
  printf("  dlTest  5-printUPPERCASE return-%s- \n", dlError);
/*                              */
/* 6-Close the shared library handle                    */
/* Note:  after the dlclose, "printUPPERCASE" is not loaded     */
/*                              */
  rc = dlclose(FunctionLib);
  dlError = dlerror();
  printf("  dlTest  6-Close handle return-%s-\n",dlError);
  if( rc ) exit(1);
/*                              */
/*  7-Open Dynamic Loadable Libary using LD_LIBRARY path        */
/*                              */
  FunctionLib = dlopen("lowercase.so",RTLD_LAZY);
  dlError = dlerror();
  printf("  dlTest  7-Open Library with relative path return-%s- \n", dlError);
  if( dlError ) exit(1);
/*                              */
/* 8-Find the second loaded function                */
/*                              */
  Function    = dlsym( FunctionLib, "printLowercase");
  dlError = dlerror();
  printf("  dlTest  8-Find symbol printLowercase return-%s- \n", dlError);
  if( dlError ) exit(1);
/*                              */
/* 8-execute the second loaded function             */
/*                              */
  rc = (*Function)( HelloMessage );
  printf("  dlTest  9-printLowercase return-%s- \n", dlError);
/*                              */
/* 10-Close the shared library handle               */
/*                              */
  rc = dlclose(FunctionLib);
  dlError = dlerror();
  printf("  dlTest 10-Close handle return-%s-\n",dlError);
  if( rc ) exit(1);
  return(0);
}




UPPERCASE.c:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/************************************************/
/*      Function to print input string as UPPER case.         */
/*      Returns 1.                                                            */
/*********************************************** */
int printUPPERCASE ( inLine )
char inLine[];
{
   char UPstring[256];
   char *inptr, *outptr;
   
   inptr = inLine;
   outptr = UPstring;
   while ( *inptr != '\0' )
      *outptr++ = toupper(*inptr++);
  *outptr++ = '\0';
   printf(UPstring);
   return(1);
}




lowercase.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/********************************************/
/*     Function to print input string as lower case.      */
/*     Returns 2.                                                      */
/******************************************* */
int printLowercase( inLine )
char inLine[];
{
   char lowstring[256];
   char *inptr, *outptr;
   inptr = inLine;
   outptr = lowstring;
   while ( *inptr != '' )
      *outptr++ = tolower(*inptr++);
  *outptr++ = '';
   printf(lowstring);
   return(2);
}

返回列表