Implicit Linking

Although I have mentioned that, in my opinion, implicit linking is less flexible, it is still necessary to provide an example of implicit linking. Here, consider only the calling program because the called program, naturally, doesn't change. As you can see, the source code of the program became somewhat easier. It is important to note that it is necessary, first, to declare the procedure called from the DLL as external and, second, to link a static libraryDLLP1.LIB.

Listing 16.3: Calling the dynamic link library: Implicit linking
image from book
 .586P ; Flat memory model IFDEF MASM  .MODEL FLAT, stdcall ELSE  .MODEL FLAT ENDIF ; Constants ; Prototypes of external procedures includelib dll1.lib IFDEF MASM ; MASM        EXTERN      DLLP1@0 :NEAR        EXTERN      ExitProcess@4:NEAR ; INCLUDELIB directives for the linker        includelib c:\masm32\lib\user32.lib        includelib c:\masm32\lib\kernel32.lib ELSE ; INCLUDELIB directives for the linker        includelib c:\tasm32\lib\import32.lib        EXTERN     DLLP1:NEAR        EXTERN     ExitProcess:NEAR        DLLP1@0 = DLLP1        ExitProcess@4 = ExitProcess ENDIF ;-------------------------------------------- ; Data segment _DATA SEGMENT _DATA ENDS ; Code segment _TEXT SEGMENT START:        PUSH  1 ; Parameter        CALL  DLLP1@0 ; Exit _EXIT:        PUSH  0        CALL  ExitProcess@4 _TEXT ENDS END START 
image from book
 

You probably want to ask a natural question: Where does the DLLP1.LIB library come from? Everything is clear and straightforward. MASM creates this library automatically, and TASM provides the IMPLIB utility that creates a static library directly from a DLL.

To translate the program from Listing 16.3, issue the following commands for MASM32:

 ml /c /coff /DMASM dllex.asm     link /subsystem:windows dllex.obj 

Issue the following commands for TASM32:

 tasm32 /ml dllex.asm     implib dll1.lib dll1.dll     tlink32 -aa dllex.obj 

Earlier, it was shown that a possible linking mechanism is determining the procedure address through the ordinal number. Now, consider how this mechanism can be implemented. First, it is necessary to assign some 2-byte number to the procedure that will be called from the DLL. This is achieved by adding the following line into the DEF file: EXPORTS DLLP1 @1 . Here, the DLLP1 procedure is mapped to the number 1. You have used DEF files when translating using TASM. When translating with MASM, they can be used the same way (the /DEF: name for the LINK.EXE program). After that, carry out translation, and the library will be created. Now, when calling the GetProcAddress function, specify the ordinal number (or, to be more precise, the double word whose least significant word contains the ordinal number and whose most significant word is zero) as the parameter. Everything will operate exactly as before, so I don't see any special need to use this approach.



The Assembly Programming Master Book
The Assembly Programming Master Book
ISBN: 8170088178
EAN: 2147483647
Year: 2004
Pages: 140
Authors: Vlad Pirogov

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net