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 unload dynamic link libraries, of course, there is no other application that uses it, otherwise, it is necessary to wait until the last function that uses its function, no longer 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 function called is in the library and whether the number of parameters and parameters of the function are needed in the library.
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 cannot run, even if The library is not important to 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 unprecedented 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 slow, 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, StdCallOption Casemap: NoneInClude / masm32 / include / windows.incinclude /masm32/include/user32.incinclude /masm32/include/kernel32.incincludelib /masm32/lib/user32.libincludelib /masm32/lib/kernel32.lib.data.codeDllEntry proc hInstDLL: HINSTANCE, reason: DWORD, RESERVED1: DWORDMOV EAX, TRUERERETDLLENTRY ENDP; ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- ---------; Below is an empty function, you can insert your function like the following. ; ------------------------------------------------- -------------------------------------------------- -TestFunction ProcrettestFunction endpend dllenTry; -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------; Dllskeleton.def; ---- -------------------------------------------------- ------------------------------- Library Dllskeletonexports TestFunction is a framework of a dynamic link library, each DLL must have an entry Point function, Windows each time the following action is called: 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: DWORDMOV EAX, TRUERETDLLETRY ENDP END Point Function Name It doesn't matter, as long as you make the function name in the statement "End
.dataLibName db "DLLSkeleton.dll", 0FunctionName db "TestHello", 0DllNotFound db "Can not load library", 0AppName db "Load Library", 0FunctionNotFound db "TestHello function not found", 0.data hLib dd;?? dynamic link library Handle (DLL) Testhelloaddr DD?; Testhello function address. CodeStart: Invoke LoadLibrary, Addr libname; -------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- ---------------------------; Call LoadLibrary, its parameters are the name of the dynamic link library 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 == Nullinvoke Messagebox, Null, Addr Dllnotfound, Addr Appname, MB_ok.elsemov Hlib, EaxInvoke GetProcaddress, Hlib, Addr functionName; ------------- -------------------------------------------------- --------------------------------------------; when you get a dynamic After the handle of the link library, pass it to the getProcAddress function, 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 gestational variable. ; ------------------------------------------------- -------------------------------------------------- -------. if eax == nullinvoke messagebox, null, addr functionNotfound, Addr Appname, MB_ok.elsemov Testhelloaddr, EaxCall [Testhelloaddr]; ------------------------------------------ -------------------------------------------------- ----------------------------------------- After you can and call other functions The function is called. Among them, it is necessary to enclose the variables containing the function address information. ; ------------------------------------------------- -------------------------------------------------- -------. Endifinvoke free freetiBRARY, HLIB; ------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------- ---------------------; call freeelibrary to uninstall the dynamic link library.
; ------------------------------------------------- -------------------------------------------------- --------. EndifInvoke EXITPROCESS, NULLEND Start Using the LoadLibrary function loads a dynamic link library, you may have to do more work, but this method is indeed a lot of flexibility. ============================================================================================================================================================================================================= ====================================
============================================================================================================================================================================================================= ====================================
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. 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.