在WINDOWS中,由于有多个程序同时运行,如果你的程序非常大的话,那将消耗相当多的内存。WINDOWS的解决办法是:使用动态链接库。动态链接库从表面上看也是一大堆的通用函数,不过即使有多个程序调用了它,在内存中也仅仅只有动态链接库的唯一一份拷贝。WINDOWS是通过分页机制来作到这一点的。当然,库的代码只有一份,但是每一个应用程序要有自己单独的数据段,要么就会乱掉。
不象旧时的静态链接库,它并不会把这些函数的可执行代码放入到应用程序中去,而是当程序已经在内存中运行时,如果需要调用该函数时才调入内存也即链接。这也就是为什么把它叫做“动态”的原因所在。另外你还可以动态地卸载动态链接库,当然要求这时没有其它的应用程序在使用它,否则就要一直等到最后一个使用它的函数也不再使用该动态链接库时才能去卸载它。
为了正确的调用库和给库函数分配内存空间,在编译和链接应用程序时,必须把重定位等一些消息插入到执行代码中去,以便载入正确的库,并给库函数分配正确的地址。
那么这些信息从哪里得到呢?引入库。引入库包含足够的信息,链接器从中抽取足够的信息(注意区别:静态链接库放入的是可执行代码)把它们放入到可执行文件中去。当WINDOWS的加载器装入应用程序查看到有DLL时,它会查找该库文件,如果没有查到,就报错退出,否则就把它映射进进程的地址空间,并修正函数调用语句的地址。
如果没有引入库呢?当然我们也可以调用动态链接库中的任意函数。只不过你必须知道调用的函数是否在库中而且是否在库的引出名字表中,另外还需要知道该函数的参数个数和参数的类型。
当你让系统的加载器为你加载动态库时,如果不能找到库文件,它就会提示一条“A required .DLL file, xxxxx.dll is missing”,这样你的应用程序就无法运行,即使该库对你的应用程序来说并不重要。
如果你选择在程序运行时自己加载该库,就没有这种问题了。
如果你知道足够的信息,就可以调用系统未公开的函数。
如果你调用LoadLibrary函数加载库,就必须再调用GetProcAddress函数来得到每一个你想调用的函数的地址,GetProcAddress会在动态链接库中查找函数的入口地址。由于多余的步骤,这样你的程序执行起来会慢一点,但是并不明显。
明白了LoadLibrary函数的优缺点,下面我们就来看看如何产生一个动态链接库。下面的代码是一个动态链接库的框架:
;--------------------------------------------------------------------------------------
; DLLSkeleton.asm
;--------------------------------------------------------------------------------------
.386
.model flat,stdcall
option casemap:none
include /masm32/include/windows.inc
include /masm32/include/user32.inc
include /masm32/include/kernel32.inc
includelib /masm32/lib/user32.lib
includelib /masm32/lib/kernel32.lib
.data
.code
DllEntry proc hInstDLL:HINSTANCE, reason:DWORD, reserved1:DWORD
mov eax,TRUE
ret
DllEntry Endp
;---------------------------------------------------------------------------------------------------
;下面是一个空函数,你可以象下面一样插入你的函数。
;----------------------------------------------------------------------------------------------------
TestFunction proc
ret
TestFunction endp
End DllEntry
;-------------------------------------------------------------------------------------
; DLLSkeleton.def
;-------------------------------------------------------------------------------------
LIBRARY DLLSkeleton
EXPORTS TestFunction
上面是一个动态链接库的框架,每一个DLL必须有一个入口点函数,WINDOWS每一次在做下面的动作时会调用该入口点函数:
当动态链接库被加载时
当动态链接库卸载时
同一进程的线程生成时
同一进程的线程退出时
DllEntry proc hInstDLL:HINSTANCE, reason:DWORD, reserved1:DWORD
mov eax,TRUE
ret
DllEntry Endp
入口点函数的名称无所谓只要你让语句“END<函数名>”中的函数名和前面的相同就可以了。该函数共有三个参数,只有前面两个是重要的。
hInstDLL是该动态链接库模块的句柄。它和进程的实例句柄不一样。如果你以后要用,可以保存它,因为以后再要获得它不容易。
根据不同的时机,reason传入的值可能是下面的四个值中的一个:
DLL_PROCESS_ATTACH 动态链接库第一次插入进程的地址空间时。当传入的参数是该值时,你可以做一些初始化的工作。
DLL_PROCESS_DETACH 动态链接库从进程的地址空间卸出时。你可以在此做一些清理的工作。譬如:释放内存等。
DLL_THREAD_ATTACH 新线程生成。
DLL_THREAD_DETACH 线程销毁。
如果想要库中的代码继续执行,返回TRUE,否则返回FALSE,那样动态链接库就不会加载了。譬如:你想分配一块内存,如果不成功的话就退出,这时你就可以返回FALSE。那样动态链接库就不会加载了。
你可以加入的函数,它们的位置并不重要,把它们放在入口点函数的前面或后面都可以。只是如果你想要它们能被其它的程序调用的话,就必须把它们的名字放到模块定义文件(.def)中去。
动态链接库在它们自己的编译过程就需要,而不只是提供给其它要引用它的程序参考。他们如下:
LIBRARY DLLSkeleton
EXPORTS TestFunction
第一行是必须的。LIBRARY 定义了DLL的模块名称。它必须和动态链接库的名称相同。
EXPORTS关键字告诉链接器该DLL的引出函数,也就是其它程序可以调用的函数。举个例子:其它的程序想要调用函数TestFunction ,我们就把它
放到EXPORTS中。
还有就是,链接器的选项中必须放入开关项:/DLL 和/DEF<DLL文件名>,就像下面这样:
link /DLL /SUBSYSTEM:WINDOWS /DEF:DLLSkeleton.def /LIBPATH:c:/masm32/lib DLLSkeleton.obj
编译器的开关选项是一样的,即:/c /coff /Cp。在你链接好后,链接器会生成.lib 和.dll文件。前者是引入库,当其它的程序要调用你的动态链接库中的函数时就需要该引入库,以便把必要的信息加入到其可执行文件中去。
接下来我们来看看如何使用LoadLibrary函数来加载一个DLL。
;---------------------------------------------------------------------------------------------
; UseDLL.asm
;----------------------------------------------------------------------------------------------
.386
.model flat,stdcall
option casemap:none
include /masm32/include/windows.inc
include /masm32/include/user32.inc
include /masm32/include/kernel32.inc
includelib /masm32/lib/kernel32.lib
includelib /masm32/lib/user32.lib
.data
LibName db "DLLSkeleton.dll",0
FunctionName db "TestHello",0
DllNotFound db "Cannot load library",0
AppName db "Load Library",0
FunctionNotFound db "TestHello function not found",0
.data?
hLib dd ? ; 动态链接库的句柄 (DLL)
TestHelloAddr dd ? ; TestHello 函数的地址
.code
start:
invoke LoadLibrary,addr LibName
;---------------------------------------------------------------------------------------------------------
; 调用LoadLibrary,其参数是欲加载的动态链接库的名称。如果调用成功,将返回该DLL的句柄。 否则返回NULL。该句柄可以传给 :library函数和其它需要动态链接库句柄的函数。
;-----------------------------------------------------------------------------------------------------------
.if eax==NULL
invoke MessageBox,NULL,addr DllNotFound,addr AppName,MB_OK
.else
mov hLib,eax
invoke GetProcAddress,hLib,addr FunctionName
;-----------------------------------------------------------------------------------------------------------
; 当你得到了动态链接库的句柄后,把它传给GetProcAddress函数,再把你要调用的函数的名称 也传给该函数。如果成功的话,它:会返回想要的函数的地址,失败的话返回NULL。除非卸载该 动态链接库否则函数的地址是不会改变的,所以你可以把它保存到一个:全局变量中以备后用。
;-----------------------------------------------------------------------------------------------------------
.if eax==NULL
invoke MessageBox,NULL,addr FunctionNotFound,addr AppName,MB_OK
.else
mov TestHelloAddr,eax
call [TestHelloAddr]
;-----------------------------------------------------------------------------------------------------------
; 以后你就可以和调用其它函数一样调用该函数了。其中要把包含函数地址信息的变量用方括号括起来。
;-----------------------------------------------------------------------------------------------------------
.endif
invoke FreeLibrary,hLib
;-----------------------------------------------------------------------------------------------------------
;调用FreeLibrary卸载动态链接库。
;-----------------------------------------------------------------------------------------------------------
.endif
invoke ExitProcess,NULL
end start
使用LoadLibrary函数加载动态链接库,可能要自己多做一些工作,但是这种方法确实是提供了许多的灵活性。
=====================================================================================
=====================================================================================
Dynamic linking allows a module to include only the information the system needs at load time or run time to locate the code for an exported DLL function. Dynamic linking differs from the more familiar static linking, in which the linker copies a library function's code into each module that calls it.
Types of Dynamic Linking
There are two methods for calling a function in a DLL:
1. In load-time dynamic linking, a module makes explicit calls to exported DLL functions. This requires you to link the module with the import library for the DLL. An import library supplies the operating system with the information needed to load the DLL and locate the exported DLL functions when the application is loaded. For more information, see Load-Time Dynamic Linking.
2. In run-time dynamic linking, a module uses the LoadLibrary or LoadLibraryEx function to load the DLL at run time. After the DLL is loaded, the module calls the GetProcAddress function to get the addresses of the exported DLL functions. The module calls the exported DLL functions using the function pointers returned by GetProcAddress. This eliminates the need for an import library. For more information, see Using Run-Time Dynamic Linking.
DLLs and Memory Management
Every process that loads the DLL maps it into its virtual address space. After the process loads the DLL into its virtual address, it can call the exported DLL functions.
The system maintains a reference count for each DLL. When a thread loads the DLL, its reference count is incremented by one. When the process terminates, or when the reference count goes to 0 (run-time dynamic linking only), the DLL is unloaded from the virtual address space.
Like any other function, an exported DLL function runs in the context of the thread that calls it. Therefore, the following conditions apply:
1.The threads of the process that called the DLL can use handles opened by a DLL function. Similarly, handles opened by any thread of the calling process can be used in the DLL function.
2.The DLL uses the stack of the calling thread and the virtual address space of the calling process.
3.The DLL allocates memory from the virtual address space of the calling process.
Only one thread at a time can call the entry-point function.