VC ++链接库(三)非MFC动态库(后续)

来源:互联网 发布:mysql out 编辑:程序博客网 时间:2024/05/22 03:20

 DllMain函数


  Windows在加载DLL的时候,需要一个入口函数,就如同控制台或DOS程序需要main函数、WIN32程序需要WinMain函数一样。在前面的例子中,DLL并没有提供DllMain函数,应用工程也能成功引用DLL,这是因为Windows在找不到DllMain的时候,系统会从其它运行库中引入一个不做任何操作的缺省DllMain函数版本,并不意味着DLL可以放弃DllMain函数。

  根据编写规范,Windows必须查找并执行DLL里的DllMain函数作为加载DLL的依据,它使得DLL得以保留在内存里。这个函数并不属于导出函数,而是DLL的内部函数。这意味着不能直接在应用工程中引用DllMain函数,DllMain是自动被调用的。

  我们来看一个DllMain函数的例子(单击此处下载本工程附件)。

Cpp代码  
  1. BOOL APIENTRY DllMain( HANDLE hModule,   
  2.   
  3. DWORD ul_reason_for_call,   
  4.   
  5. LPVOID lpReserved  
  6.   
  7. )  
  8.   
  9. {  
  10.   
  11. switch (ul_reason_for_call)  
  12.   
  13. {  
  14.   
  15. case DLL_PROCESS_ATTACH:  
  16.   
  17. printf("\nprocess attach of dll");  
  18.   
  19. break;  
  20.   
  21. case DLL_THREAD_ATTACH:  
  22.   
  23. printf("\nthread attach of dll");  
  24.   
  25. break;  
  26.   
  27. case DLL_THREAD_DETACH:  
  28.   
  29. printf("\nthread detach of dll");  
  30.   
  31. break;  
  32.   
  33. case DLL_PROCESS_DETACH:  
  34.   
  35. printf("\nprocess detach of dll");  
  36.   
  37. break;  
  38.   
  39. }  
  40.   
  41. return TRUE;  
  42.   

  DllMain函数在DLL被加载和卸载时被调用,在单个线程启动和终止时,DLLMain函数也被调用,ul_reason_for_call指明了被调用的原因。原因共有4种,即PROCESS_ATTACH、PROCESS_DETACH、THREAD_ATTACH和THREAD_DETACH,以switch语句列出。

  来仔细解读一下DllMain的函数头BOOL APIENTRY DllMain( HANDLE hModule, WORD ul_reason_for_call, LPVOID lpReserved )。

  APIENTRY被定义为__stdcall,它意味着这个函数以标准Pascal的方式进行调用,也就是WINAPI方式;

  进程中的每个DLL模块被全局唯一的32字节的HINSTANCE句柄标识,只有在特定的进程内部有效,句柄代表了DLL模块在进程虚拟空间中的起始地址。在Win32中,HINSTANCE和HMODULE的值是相同的,这两种类型可以替换使用,这就是函数参数hModule的来历。

  执行下列代码:

Cpp代码  
  1. hDll = LoadLibrary("..\\Debug\\dllTest.dll");  
  2.   
  3. if (hDll != NULL)  
  4.   
  5. {  
  6.   
  7. addFun = (lpAddFun)GetProcAddress(hDll, MAKEINTRESOURCE(1));  
  8.   
  9. //MAKEINTRESOURCE直接使用导出文件中的序号  
  10.   
  11. if (addFun != NULL)  
  12.   
  13. {  
  14.   
  15. int result = addFun(2, 3);  
  16.   
  17. printf("\ncall add in dll:%d", result);  
  18.   
  19. }  
  20.   
  21. FreeLibrary(hDll);  
  22.   
  23. }

  我们看到输出顺序为:

  process attach of dll

  call add in dll:5

  process detach of dll

  这一输出顺序验证了DllMain被调用的时机。

  代码中的GetProcAddress ( hDll, MAKEINTRESOURCE ( 1 ) )值得留意,它直接通过.def文件中为add函数指定的顺序号访问add函数,具体体现在MAKEINTRESOURCE ( 1 ),MAKEINTRESOURCE是一个通过序号获取函数名的宏,定义为(节选自winuser.h):

  1. #define MAKEINTRESOURCEA(i) (LPSTR)((DWORD)((WORD)(i)))  
  2.   
  3. #define MAKEINTRESOURCEW(i) (LPWSTR)((DWORD)((WORD)(i)))  
  4.   
  5. #ifdef UNICODE  
  6.   
  7. #define MAKEINTRESOURCE MAKEINTRESOURCEW  
  8.   
  9. #else  
  10.   
  11. #define MAKEINTRESOURCE MAKEINTRESOURCEA</span>  


 __stdcall约定


  如果通过VC++编写的DLL欲被其他语言编写的程序调用,应将函数的调用方式声明为__stdcall方式,WINAPI都采用这种方式,而C/C++缺省的调用方式却为__cdecl。__stdcall方式与__cdecl对函数名最终生成符号的方式不同。若采用C编译方式(在C++中需将函数声明为extern "C"),__stdcall调用约定在输出函数名前面加下划线,后面加“@”符号和参数的字节数,形如_functionname@number;而__cdecl调用约定仅在输出函数名前面加下划线,形如_functionname。

  Windows编程中常见的几种函数类型声明宏都是与__stdcall和__cdecl有关的(节选自windef.h):

Cpp代码  
  1. #define CALLBACK __stdcall //这就是传说中的回调函数  
  2.   
  3. #define WINAPI __stdcall //这就是传说中的WINAPI  
  4.   
  5. #define WINAPIV __cdecl  
  6.   
  7. #define APIENTRY WINAPI //DllMain的入口就在这里  
  8.   
  9. #define APIPRIVATE __stdcall  
  10.   
  11. #define PASCAL __stdcall</span>  
  在lib.h中,应这样声明add函数:

Cpp代码  
  1. int __stdcall add(int x, int y);  

  在应用工程中函数指针类型应定义为:

Cpp代码  
  1. typedef int(__stdcall *lpAddFun)(intint);

  若在lib.h中将函数声明为__stdcall调用,而应用工程中仍使用typedef int (* lpAddFun)(int,int),运行时将发生错误(因为类型不匹配,在应用工程中仍然是缺省的__cdecl调用),弹出如图7所示的对话框。

图7 调用约定不匹配时的运行错误

  图8中的那段话实际上已经给出了错误的原因,即“This is usually a result of …”。

  单击此处下载__stdcall调用例子工程源代码附件。



 DLL导出变量


  DLL定义的全局变量可以被调用进程访问;DLL也可以访问调用进程的全局数据,我们来看看在应用工程中引用DLL中变量的例子(单击此处下载本工程附件)。

Cpp代码  
  1. /* 文件名:lib.h */  
  2.   
  3. #ifndef LIB_H  
  4.   
  5. #define LIB_H  
  6.   
  7. extern int dllGlobalVar;  
  8.   
  9. #endif

 

Cpp代码  
  1. /* 文件名:lib.cpp */  
  2.   
  3. #include "lib.h"    
  4. #include <windows.h>   
  5. int dllGlobalVar;  
  6.     
  7. BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)    
  8. {   
  9. switch (ul_reason_for_call)    
  10. {  
  11.   
  12. case DLL_PROCESS_ATTACH:  
  13.   
  14. dllGlobalVar = 100; //在dll被加载时,赋全局变量为100  
  15.   
  16. break;  
  17.   
  18. case DLL_THREAD_ATTACH:  
  19.   
  20. case DLL_THREAD_DETACH:  
  21.   
  22. case DLL_PROCESS_DETACH:  
  23.   
  24. break;  
  25.   
  26. }  
  27.   
  28. return TRUE;  
  29.   
  30. }  
  31.   
  32.   
  33. ;文件名:lib.def  
  34.   
  35. ;在DLL中导出变量  
  36.   
  37. LIBRARY "dllTest"  
  38.   
  39. EXPORTS  
  40.   
  41. dllGlobalVar CONSTANT  
  42.   
  43. ;或dllGlobalVar DATA  
  44.   
  45. GetGlobalVar


  从lib.h和lib.cpp中可以看出,全局变量在DLL中的定义和使用方法与一般的程序设计是一样的。若要导出某全局变量,我们需要在.def文件的EXPORTS后添加:

Cpp代码  
  1. 变量名 CONSTANT   //过时的方法

  或

Cpp代码  
  1. 变量名 DATA     //VC++提示的新方法  

  在主函数中引用DLL中定义的全局变量:

Cpp代码  
  1. #include <stdio.h>  
  2.   
  3. #pragma comment(lib,"dllTest.lib")  
  4.   
  5. extern int dllGlobalVar;  
  6.   
  7. int main(int argc, char *argv[])  
  8.   
  9. {  
  10.   
  11. printf("%d ", *(int*)dllGlobalVar);  
  12.   
  13. *(int*)dllGlobalVar = 1;  
  14.   
  15. printf("%d ", *(int*)dllGlobalVar);  
  16.   
  17.   
  18. return 0;  
  19.   
  20. }
  特别要注意的是用extern int dllGlobalVar声明所导入的并不是DLL中全局变量本身,而是其地址,应用程序必须通过强制指针转换来使用DLL中的全局变量。这一点,从*(int*)dllGlobalVar可以看出。因此在采用这种方式引用DLL全局变量时,千万不要进行这样的赋值操作:

Cpp代码  
  1. dllGlobalVar = 1;

  其结果是dllGlobalVar指针的内容发生变化,程序中以后再也引用不到DLL中的全局变量了。

  在应用工程中引用DLL中全局变量的一个更好方法是:

Cpp代码  
  1. #include <stdio.h>  
  2.   
  3. #pragma comment(lib,"dllTest.lib")  
  4.   
  5. extern int _declspec(dllimport) dllGlobalVar; //用_declspec(dllimport)导入  
  6.   
  7. int main(int argc, char *argv[])  
  8.   
  9. {  
  10.   
  11. printf("%d ", dllGlobalVar);  
  12.   
  13. dllGlobalVar = 1; //这里就可以直接使用, 无须进行强制指针转换  
  14.   
  15. printf("%d ", dllGlobalVar);  
  16.   
  17. return 0;  
  18.   
  19. }

  通过_declspec(dllimport)方式导入的就是DLL中全局变量本身而不再是其地址了,笔者建议在一切可能的情况下都使用这种方式

 

DLL导出类


  DLL中定义的类可以在应用工程中使用。

  下面的例子里,我们在DLL中定义了point和circle两个类,并在应用工程中引用了它们(单击此处下载本工程附件)。

//文件名:point.h,point类的声明
Cpp代码  
  1. <span style="font-size: small;">#ifndef POINT_H  
  2.   
  3. #define POINT_H  
  4.   
  5. #ifdef DLL_FILE  
  6.   
  7. class _declspec(dllexport) point //导出类point  
  8.   
  9. #else  
  10.   
  11. class _declspec(dllimport) point //导入类point  
  12.   
  13. #endif  
  14.   
  15. {  
  16.   
  17. public:  
  18.   
  19. float y;  
  20.   
  21. float x;  
  22.   
  23. point();  
  24.   
  25. point(float x_coordinate, float y_coordinate);  
  26.   
  27. };  
  28.   
  29. #endif  

//文件名:point.cpp,point类的实现

Cpp代码  
  1. #ifndef DLL_FILE  
  2.   
  3. #define DLL_FILE  
  4.   
  5. #endif  
  6.   
  7. #include "point.h"  
  8.   
  9. //类point的缺省构造函数  
  10.   
  11. point::point()  
  12.   
  13. {  
  14.   
  15. x = 0.0;  
  16.   
  17. y = 0.0;  
  18.   
  19. }  
  20.   
  21. //类point的构造函数  
  22.   
  23. point::point(float x_coordinate, float y_coordinate)  
  24.   
  25. {  
  26.   
  27. x = x_coordinate;  
  28.   
  29. y = y_coordinate;  
  30.   
  31. }  

 
//文件名:circle.h,circle类的声明

Cpp代码  
  1. <span style="font-size: small;">#ifndef CIRCLE_H  
  2.   
  3. #define CIRCLE_H  
  4.   
  5. #include "point.h"   
  6.   
  7. #ifdef DLL_FILE  
  8.   
  9. class _declspec(dllexport)circle //导出类circle  
  10.   
  11. #else  
  12.   
  13. class _declspec(dllimport)circle //导入类circle  
  14.   
  15. #endif  
  16.   
  17. {  
  18.   
  19. public:  
  20.   
  21. void SetCentre(const point ¢rePoint);  
  22.   
  23. void SetRadius(float r);  
  24.   
  25. float GetGirth();  
  26.   
  27. float GetArea();  
  28.   
  29. circle();  
  30.   
  31. private:  
  32.   
  33. float radius;  
  34.   
  35. point centre;  
  36.   
  37. };  
  38.   
  39. #endif  

 
//文件名:circle.cpp,circle类的实现

Cpp代码  
  1. <span style="font-size: small;">#ifndef DLL_FILE  
  2.   
  3. #define DLL_FILE  
  4.   
  5. #endif  
  6.   
  7. #include "circle.h"  
  8.   
  9. #define PI 3.1415926  
  10.   
  11. //circle类的构造函数  
  12.   
  13. circle::circle()  
  14.   
  15. {  
  16.   
  17. centre = point(0, 0);  
  18.   
  19. radius = 0;  
  20.   
  21. }  
  22.   
  23. //得到圆的面积  
  24.   
  25. float circle::GetArea()  
  26.   
  27. {  
  28.   
  29. return PI *radius * radius;  
  30.   
  31. }  
  32.   
  33. //得到圆的周长  
  34.   
  35. float circle::GetGirth()  
  36.   
  37. {  
  38.   
  39. return 2 *PI * radius;  
  40.   
  41. }  
  42.   
  43. //设置圆心坐标  
  44.   
  45. void circle::SetCentre(const point ¢rePoint)  
  46.   
  47. {  
  48.   
  49. centre = centrePoint;  
  50.   
  51. }  
  52.   
  53. //设置圆的半径  
  54.   
  55. void circle::SetRadius(float r)  
  56.   
  57. {  
  58.   
  59. radius = r;  
  60.   
  61. }  

 

类的引用:

Cpp代码  
  1. <span style="font-size: small;">#include "..\circle.h"  //包含类声明头文件  
  2.   
  3. #pragma comment(lib,"dllTest.lib");  
  4.   
  5.   
  6. int main(int argc, char *argv[])  
  7.   
  8. {  
  9.   
  10. circle c;  
  11.   
  12. point p(2.0, 2.0);  
  13.   
  14. c.SetCentre(p);  
  15.   
  16. c.SetRadius(1.0);  
  17.   
  18. printf("area:%f girth:%f", c.GetArea(), c.GetGirth());  
  19.   
  20.   
  21. return 0;  
  22.   
  23. }

 


  从上述源代码可以看出,由于在DLL的类实现代码中定义了宏DLL_FILE,故在DLL的实现中所包含的类声明实际上为:

Cpp代码  
  1. <span style="font-size: small;">class _declspec(dllexport) point //导出类point  
  2.   
  3. {  
  4.   
  5. …  
  6.   
  7. }

  

Cpp代码  
  1. <span style="font-size: small;">class _declspec(dllexport) circle //导出类circle  
  2.   
  3. {  
  4.   
  5. …  
  6.   
  7. }

 

  而在应用工程中没有定义DLL_FILE,故其包含point.h和circle.h后引入的类声明为:

Cpp代码  
  1. <span style="font-size: small;">class _declspec(dllimport) point //导入类point  
  2.   
  3. {  
  4.   
  5. …  
  6.   

  和

Cpp代码  
  1. <span style="font-size: small;">class _declspec(dllimport) circle //导入类circle  
  2.   
  3. {  
  4.   
  5. …  
  6.   

 

不错,正是通过DLL中的

Cpp代码  
  1. <span style="font-size: small;">class _declspec(dllexport) class_name //导出类circle   
  2.   
  3. {  
  4.   
  5. …  
  6.   

 

  与应用程序中的

Cpp代码  
  1. <span style="font-size: small;">class _declspec(dllimport) class_name //导入类  
  2.   
  3. {  
  4.   
  5. …  
  6.   
  7. }   

 
  匹对来完成类的导出和导入的!
  我们往往通过在类的声明头文件中用一个宏来决定使其编译为class _declspec(dllexport) class_name还是class _declspec(dllimport) class_name版本,这样就不再需要两个头文件。本程序中使用的是:

Cpp代码  
  1. #ifdef DLL_FILE  
  2.   
  3. class _declspec(dllexport) class_name //导出类  
  4.   
  5. #else  
  6.   
  7. class _declspec(dllimport) class_name //导入类  
  8.   
  9. #endif   
  实际上,在MFC DLL的讲解中,您将看到比这更简便的方法,而此处仅仅是为了说明_declspec(dllexport)与_declspec(dllimport)匹对的问题。

  由此可见,应用工程中几乎可以看到DLL中的一切,包括函数、变量以及类,这就是DLL所要提供的强大能力。只要DLL释放这些接口,应用程序使用它就将如同使用本工程中的程序一样!

  本章虽以VC++为平台讲解非MFC DLL,但是这些普遍的概念在其它语言及开发环境中也是相同的,其思维方式可以直接过渡。


原创粉丝点击