Chapter 17 Dynamic Link Library
In this lesson, we will learn DLLs, what they are, and how to create them.
theory:
If your programming is very long, you will find a lot of repetition code between many programs. Rewriting each of the programs over and over again, there is no need to waste time. In the DOS era, the general approach is to write these duplicate code into one function, then put them in different library files in different library files. When you use these functions, just link your target file (.obj) file and previously stored in the library file, the linker will take the relevant information from the library file and insert them into executable. Document in the file. This process is called a static link. C The runtime library is a good example. The disadvantage of such libraries is that you must embed the copy of the same function in the program of each call library function, which is obviously a waste of disk. After all in the DOS era, there is only one program in every moment, so waste is just a disk. In the multi-task's Windows era, you must not only waste disk, but also waste valuable memory.
In Windows, because there are multiple programs to run at the same time, if your program is very large, it will consume considerable memory. Windows's solution is to use a dynamic link library. The dynamic link library is also a common function on the surface, but even if there are multiple programs to call it, only the only copy of the dynamic link library is only in memory. Windows is made by paging mechanisms. Of course, the code of the library has only one, but every application has its own separate data segment, or it will be chaotic. Not like an old-time static link library, it does not place the executable code of these functions into the application, but when the program has been run in memory, if you need to call the function, you will transfer memory. link. This is why it is called "dynamic". In addition, you can also dynamically uninstall the dynamic link library, of course, there is no other application that uses it, otherwise it will continue to use its function to use the dynamic link library to uninstall it. In order to correct memory spaces and library functions, you must insert some messages such as reconciliation to the execution code to load the correct library, and assign the correct address to the library function. . So where did this information get? The library is introduced. The introduction library contains sufficient information, and the linker draws enough information from it (Note that the static link library is put in executable) put them into the executable. When the Windows loader is loaded into the application, when there is a DLL, it will find the library file. If you do not find it, you will report the error exit, otherwise you map the address space of the process and correct the address of the function call statement. If there is no introduction library? Of course, we can also call any of the functions in the dynamic link library. Only, you must know if the called function is in the library and whether the number of parameters and parameters of the function are needed in the library.
(Translator plus: Say this, let me think of a very famous thing. <<
Undocunted Windows >> Author Angel Schudleman used this method to track unapproved functions in the Microsoft Win3x system dynamic link library, because Microsoft provides prototypes for these functions in the introduction library of the system dynamic link library provided by Microsoft. So you can't link the information of these functions to the executable when you link, and you can load the dynamic link library when you execute the dynamic link library and get the address of these functions when you execute. Use these unusless functions like calling other library functions. Due to the huge impact of this book, many programmers have called unobtrusives in their programs, even when writing business programs. This peak practice caused Microsoft's dislike, and later Microsoft no longer included those unapproved functions in the system dynamic link library in its Win3X's improvement version, which cannot be used to use this method. Call an unapproved function. When you let the system's loader load a dynamic library, if you can't find the library file, it will prompt a "a required.dll file, XXXXX.DLL IS MISSING" so your application can not run, even This library is not important for your app. If you choose to load the library while the program is running, there is no such problem. If you know enough information, you can call the system unapproved function. If you call the LoadLibrary function to load the library, you must call the getProcAddress function to get the address of each function you want to call, and getProcAddress will find the entry address of the function in the dynamic link library. Due to the extra step, your program will be slower, but it is not obvious. Understand the advantages and disadvantages of the LoadLibrary function, let's take a look at how to generate a dynamic link library. The following code is a framework for a dynamic link library:
; ------------------------------------------------- -------------------------------------; Dllskeleton.asm; -------- -------------------------------------------------- ---------------------------- .386 .Model flat, stdcall option casemap: none includ /masm32/include/windows.inc include / Masm32 / include / user32.inc include /masm32/include/kernel32.inc includelib /masm32/lib/user32.lib incruDelib /masm32/lib/kernel32.lib
Sign, Reserved1: DWord, Reserved1: DWord Mov EAX, True Ret DLlentry Endp; ----------------------------------------------------------------------------------- -------------------------------------------------- ------------------------; below is an empty function, you can insert your function like the following. ; ------------------------------------------------- -------------------------------------------------- - TestFunction Proc Ret TestFunction ENDP
End dllenTry
; ------------------------------------------------- ------------------------------------; Dllskeleton.def; --------- -------------------------------------------------- -------------------------- Library Dllskeleton Exports TestFunction is a framework of a dynamic link library, each DLL must have an entry point function, Windows The entry point function is called every time you do the following action:
When the dynamic link library is loaded when the dynamic link is uninstalled, the same process is generated when the same process is generated.
DLlentry Proc Hinstdll: Hinstance, Reason: DWORD, RESERVED1: DWORD MOV EAX, TRUE RET DLLENTRY ENDP
The name of the entry point function doesn't matter, as long as you make the function name in the statement "END The DLL_PROCESS_ATTACH Dynamic Link Library When the address space is inserted into the process. When the incoming parameter is this value, you can do some initialization work. The DLL_PROCESS_DETACH Dynamic Link Library When you remove the address space of the process. You can do some cleaning work here. Such as: release memory, etc. DLL_THREAD_ATTACH new thread generation. DLL_THREAD_DETACH thread is destroyed. If you want the code in the library to continue, return true, otherwise returning false, then the dynamic link library will not load. For example, if you want to assign a memory, if you are not successful, you will return it, then you can return false. Then, the dynamic link library will not load. You can join the function, the location is not important, put them in front of the entry point function or back. Just if you want them to be called by other programs, they must put their names in the module definition file (.def). Dynamic link libraries need in their own compilation process, not just providing procedures to reference to it. They are as follows: Library Dllskeleton Exports TestFunction The first line is necessary. Library defines the DLL module name. It must be the same as the name of the dynamic link library. The exports keyword tells the linker that the DLL leads function, that is, other programs that can be called. For example: Other programs want to call the function TestFunction, let it put it in Exports. Also, the switches must be placed in the options: / dll and / def LINK / DLL / SUBSYSTEM: Windows /Def: DLLSKELETON.DEF / LIBPATH: C: / MASM32 / LIB DLLSKELETON.OBJ The switch option of the compiler is the same, namely: / c / coff / cp. After your link is good, the linker generates .lib and .dll files. The former is an introduction library. When other programs need to call functions in your dynamic link library, you need to introduce the library to add the necessary information to its executable. Let's take a look at how to use the loadLibrary function to load a 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 loading library", 0 Appname DB "Load Library", 0 functionNotFound DB "Testhello function not found", 0 .DATA? HLIB DD?; Dynamic Link Library Handle (DLL) Testhelloaddr DD?; Testhello function address . Code Start: Invoke LoadLibrary, AddR Libname; ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------- ---------------; Call LoadLibrary, its parameter is the name of the dynamic link library that wants to load. If the call is successful, the handle of the DLL will be returned. Otherwise returns NULL. The handle can be passed to: library function and other functions that require dynamic link library handles. ; ------------------------------------------------- -------------------------------------------------- ------ .if Eax == Null Invoke Messagebox, Null, Addr Dllnotfound, Addr Appname, MB_ok .else Mov Hlib, Eax Invoke GetProcaddress, Hlib, Addr FunctionName; ---------- -------------------------------------------------- ----------------------------------------------; After getting the handle of the dynamic link library, pass it to the getProcAddress function, and then pass the name of the function you want to call to the function. If successful, it: will return the address of the function you want, return NULL. Unless the address of the dynamic link library does not change, you can save it to a: global variables are used after preparation. ; ------------------------------------------------- -------------------------------------------------- ------- .if Eax == Null Invoke Messagebox, Null, Addr FunctionNotfound, Addr Appname, MB_OK .ELSE MOV TESTHELLOADDR, EAX CALL [TESTHELLOADDR]; ------------- -------------------------------------------------- -------------------------------------------- After you can Calling the function like calling other functions. Among them, it is necessary to enclose the variables containing the function address information. ; ------------------------------------------------- -------------------------------------------------- ------ .ndif Invoke Freelibrary, Hlib; ----------------------------------- -------------------------------------------------- ----------------------; call freeElibrary to uninstall the dynamic link library. ; ------------------------------------------------- -------------------------------------------------- ------ .ndif Invoke EXITPROCESS, NULL End Start Using the LoadLibrary function Loading a dynamic link library, you may have to do more work, but this method is indeed a lot of flexibility.