Tutorials to .com

Tutorials to .com » Software » Asm » WIN32 compilation: 17. Dynamic Link Library

WIN32 compilation: 17. Dynamic Link Library

Print View , by: iSee ,Total views: 10 ,Word Count: 2428 ,Date: Thu, 7 May 2009 Time: 4:56 AM

17th Division of Dynamic Link Library


This lesson, we will learn from DLLs, in the end they are what and how to create them.

Theory:

If you are programming is very long, you will find a lot of programs there are a considerable number of duplicate code. Each made a point of order on the code rewrite it neither necessary and a waste of time. In the DOS era, the general practice is to repeat the code written in such a function, and then put them into different categories according to the document to the Treasury. When to use these functions, as long as your target file (. Obj) files and library files previously stored in the function of the link, when the linker links the library files will be extracted from the relevant information and put them into the executable to document. This process is called the static link. c runtime library is a good example. The shortcomings of such a library is your library for each procedure call must be embedded in copies of the same function, which is obviously a waste of disk. In the DOS era, after all, every moment is only one program running, so waste no more than just the disk, in the era of multi-tasking on the WINDOWS disk is not only wasteful, but also a waste of valuable memory.

In WINDOWS, because there are multiple procedures running at the same time, if your program is very large, it will consume a considerable amount of memory. WINDOWS solution is: Use a dynamic link library. Dynamic Link Library is also a lot of the surface of the generic function, but even if there are multiple programs call it, in memory only has a dynamic link library a copy only. WINDOWS through the paging mechanism to do this. Of course, there is only one code base, but each application must have its own separate data segment, or will be out chaos.
Unlike the old static-link library, it does not share these functions into the executable code to the application, but when the process has been running in memory, if you need to call this function when transferred to the memory that is. That is why it is called "dynamic" of the reason why. In addition you can also dynamically unloading dynamic link library, of course, the requirements at this time no other applications use it, otherwise we must wait until the last one has to use it is no longer a function of the use of the dynamic link library can be to uninstall it.
In order to correctly call the library and the library functions to allocate memory space to compile and link in the application, must be re-positioning some of the news into the code to implement in order to load the correct library, and the distribution of library functions to the correct address .
Well, from where the information gained? The introduction of the Treasury. The introduction of the Treasury contain enough information, extracted from the linker enough information (Note the distinction: Add a static link library is executable code) Add them to the executable file. When the loader into the WINDOWS application view of the DLL, it will find the library file, if not found, exit on error, otherwise put it into the process of mapping the address space, and the amendment function call statement.
If there is no introduction of this library? Of course, we can also call the dynamic link library function of the arbitrary. But you must know whether to call a function library and whether or not the lead name in the database table, also need to know the parameters of the function of the number and type of parameters.

(The translator added: Here, let me recall a well-known things. << Undocumented Windows>> author of the book Angel Schudleman who use this method to track Microsoft Win3x system dynamic link library function does not open, because in Microsoft's programmers to provide the system dynamic-link library of the introduction of the Treasury did not provide these functions of the prototype , so you can not link the information to link these functions to the executable file in to, and for whatever purpose you have to use these functions, you can load in the implementation of dynamic-link library and the address of these functions, so and call other library functions, like the use of these functions were not disclosed. The book as a result of the tremendous impact that many programmers have in their program were not disclosed function call, even in writing to do so when business processes. This practice of fengcaused resentment Microsoft, then Microsoft in its improved version of Win3x no longer a public function to those who have not included in the system leads to a dynamic link library's name table, so that it can no longer use this method to call a function not disclosed. )

  • When you let the system loader to load dynamic library for you, if can not find the library file, it will prompt a "A required. DLL file, xxxxx.dll is missing", so your application can not run, Even if the library of your application is not important.
  • If you choose to run their own program to load the library, there will be no such problem.
  • If you know enough information, you can call a function of the system does not open.
  • If you call the LoadLibrary function to load the Treasury, it is necessary to re-call the GetProcAddress function to get each and every function call you want to address, GetProcAddress will be in the dynamic link library function to find the entrance to the address. Because of the extra steps of the procedure so that you will be slow to implement, but not obvious.
LoadLibrary function to understand the advantages and disadvantages of the following, we take a look at how to create a dynamic link library. The following code is a dynamic link library of the framework:

;------------------------------------------------- -------------------------------------
; 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
;------------------------------------------------- --------------------------------------------------
; The following is an empty function, you can insert the same as following your function.
;------------------------------------------------- -------------------------------------------------- --
TestFunction proc
ret
TestFunction endp

End DllEntry

;------------------------------------------------- ------------------------------------
; DLLSkeleton.def
;------------------------------------------------- ------------------------------------
LIBRARY DLLSkeleton
EXPORTS TestFunction

Above is a dynamic link library of the framework, each must have a DLL entry point function, WINDOWS each time doing the following actions will call the entry point function:

  • When the dynamic-link library to be loaded
  • When the dynamic link library when unloading
  • Generate the same process when the thread
  • The same process when the thread exits

DllEntry proc hInstDLL: HINSTANCE, reason: DWORD, reserved1: DWORD
mov eax, TRUE
ret
DllEntry Endp

Entry point function name does not matter as long as you let statement "END <function name>" The function of the same name and in front of it. There are three parameters of the function, only the first two is important.
hInstDLL is the dynamic link library module handle. Examples of it and handle the process differently. If you use, you can save it, because later it is not easy to obtain.
Depending on the time, reason may be introduced into the value of the following four values:

  • Dynamic Link Library DLL_PROCESS_ATTACH insertion process the first time the address space. When the incoming parameter is the value, you can do some initialization work.
  • DLL_PROCESS_DETACH dynamic link library from the process address space when unloaded. You can do a little bit of clean-up work. For example: the release of memory.
  • DLL_THREAD_ATTACH generate new thread.
  • Thread DLL_THREAD_DETACH destruction.

If you want the library to continue to implement the code to return TRUE, otherwise return FALSE, as a dynamic link library will not be loaded. For example: You want a piece of memory allocated, if not successful on the exit, then you can return FALSE. As a dynamic link library will not be loaded.
You can add a function of their location is not important, put them on the entry point function can be front or rear. However, if you want them to be calling other procedures, it is necessary to put the names of their module definition file (. Def) go.
Dynamic link library compiled in the course of their own need, not just to other procedures which should quote the reference. They are as follows:

LIBRARY DLLSkeleton
EXPORTS TestFunction

The first line is necessary. LIBRARY module defines the name of the DLL. It must be a dynamic link library of the same name.
EXPORTS keyword tells the linker leads to the DLL function, that is, other programs can call the function. For example: you want to call other function of the process TestFunction, we put it on in EXPORTS.
There is the option to linker switches Add items must be: / DLL and / DEF <DLL>, like the following:

link / DLL / SUBSYSTEM: WINDOWS / DEF: DLLSkeleton.def / LIBPATH: c: \ masm32 \ lib DLLSkeleton.obj

Compiler switch option is the same, that is: / C++ / coff / Cp. After you link, the linker generates. Lib and. Dll files. The former is the introduction of the Treasury, when other procedures have to call your DLL function in the introduction of the Treasury on the need to join the necessary information to its executable file to.
Now 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 load library", 0
AppName db "Load Library", 0
FunctionNotFound db "TestHello function not found", 0

. data?
hLib dd?; handle dynamic-link library (DLL)
TestHelloAddr dd?; TestHello function address

. code
start:
invoke LoadLibrary, addr LibName
;------------------------------------------------- -------------------------------------------------- ------
; Call LoadLibrary, its parameter is the wish to load the dynamic link library name. If the calls succeed, will return the handle of the DLL. Otherwise return NULL. The handle can be passed on: library function and other needs of a dynamic link library function handle.
;------------------------------------------------- -------------------------------------------------- --------
. if eax == NULL
invoke MessageBox, NULL, addr DllNotFound, addr AppName, MB_OK
. else
mov hLib, eax
invoke GetProcAddress, hLib, addr FunctionName
;------------------------------------------------- -------------------------------------------------- --------
; When you get a handle to a dynamic link library, the GetProcAddress function to it, and then call the function you want the name to the function. If successful, it is: the function will return to the address you want, what if it fails to return NULL. Except to uninstall the dynamic link library or function's address is not going to change, so you can save it to one thing: global variables in order to prepare for his own benefit.
;------------------------------------------------- -------------------------------------------------- --------
. if eax == NULL
invoke MessageBox, NULL, addr FunctionNotFound, addr AppName, MB_OK
. else
mov TestHelloAddr, eax
call [TestHelloAddr]
;------------------------------------------------- -------------------------------------------------- --------
; Later and call you can call the other function as a function. Function which contains the address information to the variable with square brackets.
;------------------------------------------------- -------------------------------------------------- --------
. endif
invoke FreeLibrary, hLib
;------------------------------------------------- -------------------------------------------------- --------
; Call unloading dynamic link library FreeLibrary.
;------------------------------------------------- -------------------------------------------------- --------
. endif
invoke ExitProcess, NULL
end start

The use of LoadLibrary function to load dynamic link library, you may want to do more work, but this method does is to provide a lot of flexibility.



Assembly Language Tutorial Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.