一、动态链接库介绍(DLL)
动态链接库英文为DLL, 是Dynamic Link Library的缩写形式。DLL是一个包含可由多个程序同时使用的代码和数据的库。DLL不是可执行文件。DLL包含一个或多个已被编译、链接并与使用它们的进程分开存储的函数。DLL有助于共享数据和资源。多个应用程序可以同时访问内存中单个DLL副本的内容。
DLL
Dynamic-link library(also written unhyphenated), or DLL, is Microsoft's implementation of the shared library concept in the Microsoft Windows and OS/2 operating systems. These libraries ususlly have the file extension DLL, OCX (for libraries containing ActiveX controls), or DRV (for legacy system drivers). The file formats for DLLs are the same as for Windows EXE files -- that is, Portable Executable(PE) for 32-bit and 64-bit Windows, and New Executable (NE) for 16-bit Windows. As with EXEs, DLLs can contain code, data and resources, in any combination.
Data files with the same file format as a DLL, but with different file extensions and possibly containing only resource sections, can be called resource DLLs. Examples of such DLLs include icon libraries, sometimes having the extension ICL, and font files, having the extensions FON and FOT.
Features of DLL
Since DLLs are essentially the same as EXEs, the choice of which to produce as part of the linking process is for clarity, since it is possible to export functions and data from either.
It is not possible to directly execute a DLL, since it requires and EXE for the operating system to load it through an entry point, hence the existence of utilities like RUNDLL.EXE or RUNDLL32.EXE which provide the entry point and minimal framework for DLLs that contain enough functionality to execute without much support.
DLLs provide a mechanism for shared code and data, allowing a developer of shared code/data to upgrade functionality without requiring applications to be re-linked or re-compiled.
DLLs execute in the memory space of the calling process and with the same access permissions which means there is little overhead in their use but also that there is no protection for the calling EXE if the DLL has any sort of bug.
Memory Management
In Windows API, the DLL files are organized into sections. Each section has its own set of attributes, such as being writable or read-only, executable (for code) or non-executable (for data), and so on.
The code in a DLL is usually shared among all the processes that use the DLL, that is, they occupy a single place in physical memory, and do not take up space in the page file.
In contrast to code sections, the data sections of a DLL are usually private, that is, each process using the DLL has its own copy of the DLL's data.
Explicit run-time linking
DLL files may be explicitly loaded at run-time, a process referred to simply as run-time dynamic linking by Microsoft, by using the LoadLibrary (or LoadLibraryEx) API function.The GetProcAddress API function is used to look up exported symbols by name, and FreeLibrary - to unload the DLL. These functions are analogous to dlopen, dlsym, and dlclose in the POSIX standard API.
Delayed loading
Normally, and application that was liked against a DLL's import library will fail to start if the DLL cannot be found, because Windows will not run teh application unless it can find all of the DLLs that the application may need. However, an application may be linked against an import library to allow delayed loading of the dynamic library.
The delay-loading mechanism also provides notification hooks, allowing the application to perform additional processing or error handling when the DLL is loaded and/or any DLL function is called.
1. Non-MFC DLL:
不用MFC的类库,直接用c语言写的DLL,其输出的函数一般用的是标准c接口,并能被非MFC或MFC编写的应用程序所调用。
2.Regular DLL:
和Extension DLLs一样,是用MFC类库编写的。明显的特点是在源文件有一个继承CWinApp的类。
这其中,又分为静态链接到MFC和动态链接到MFC。但,静态链接到MFC的动态链接库只被VC的专业版和企业版作支持(小编未验证)。
静态链接到MFC的动态链接库:这种DLL在链接过程中会将使用到的MFC类库复制一份到最终的DLL文件中,最终生成的DLL比较庞大且加载时不是很方便,但,它可以在没有MFC类库DLL的机器上正常使用。
动态链接库使用共享MFC DLL:这种DLL不会讲用到的MFC类库复制到最终生成的DLL中,因此最终的DLL比较小,加载也很方便,但,在没有MFC类库DLL文件的机器上无法使用,必须有MFC类库的支持。
3.Extension DLL
MFC 扩展DLL,可以实现从MFC所继承下来的类重新利用,可以到处C++类以及MFC派生类,使用这种DLL必须有MFC类库的支持,也就是说它只被用MFC类库所编写的应用程序所调用。
4.编译过程:
①VS2010新建dll工程:File→new→Project→Win32 Application→application setting 选择dll
②在自己需要放入dll的函数名前用 __declspec(dllimport)标识,并放入头文件。
③在对应的CPP文件中实现自己要放入dll的函数,因为头文件已经标识,不需要进行再标识。
④编译就可以得到对应的文件,要用到的就三个:对应dll的头文件,dll文件,对应dll的lib文件(隐式调用要用的)
5.调用过程:
隐式调用
①在调用函数的文件中,引入dll的头文件,另外用#pragma comment (lib, "dll_lib.lib")
显示调用
......
6.编写注意事项
如果使用到了win32 API,则应该使用调用方式为"__stdcall"。
在将C++生成的DLL供标准C语言使用,输出文件需要用"extern "C""修饰,否则不能被标准C语言调用。如果 使用"__stdcall"调用方式,可能产生C不识别的修饰名,所以设置导出函数时要采用.def文件形式,而不是__declspec(dllexport)形式。后者会进行修饰名转换,C语言无法识别函数。
二、LIB
1.编译过程:
①VS2010新建dll工程:File→new→Project→Win32 Application→application setting 选择lib 去掉Precompiled header
②和普通工程是一样的,不需要添加任何参数。
③进行正常编译就好。有效的文件(1)头文件 (2)lib库
2.调用过程
①把头文件和生成的lib文件拷贝到要调用工程目录下。
②在调用的文件内,添加#praga comment( lib, "*.lib") 编译就可以实现。