Execution time for dynamic libraries

Source: Internet
Author: User
Tags execution thread

This article is a series of special tutorials on the "from DOS to Win32" in the 8th article of the follow-up, read this article before please read the articles of the incense.
When a dynamic library is referenced in a program, is windows a long journey ahead of the program? Or do you want to load the dynamic library first?

To make sense of this problem, we will revise the mydll.asm and 10.ASM slightly as follows:

; ================mydll.asm================
An example: Converts a value in Edx:eax to a decimal output form string.
; FileName: Mydll.asm, this is the source of the dynamic link library
; compilation mode = "DLL"
.386
. Model Flat,stdcall
Option Casemap:none

Include Windows.inc
Include User32.inc
Include Kernel32.inc
Includelib User32.lib
Includelib Kernel32.lib

Outedxeax Proto:dword

. DATA
SzText0 db "hahaha ... Mydll load ", 0
SZTEXT1 DB "Mydll the first time the process's address space is inserted", 0
SZTEXT2 db "Mydll from the process's address space", 0
SZTEXT3 db "New thread generation for the same process", 0
SZTEXT4 db "thread destruction for the same process", 0
SZTEXT5 db "Mydll function Outedxeax call succeeded", 0

. Code

The

;D Llentry is the portal to a dynamic-link library, which is invoked when a dynamic-link library is loaded/unloaded,
, or when a thread of the same process generates/exits
; the function name, of course, is not necessarily this, but is consistent with the last end dllentry.
Dllentry proc Hinstdll:hinstance, Reason:dword, Reserved1:dword
. If reason==dll_process_attach  The dynamic link library inserts the process's address space for the first time
Invoke messagebox,null,addr sztext1,addr sztext0,mb_ok
mov eax,true
ret
. ElseIf reason==dll_process_detach  The dynamic link library out of the process's address space
Invoke MESSAGEBOX,NULL,ADDR sztext2,addr SZTEXT0,MB_OK
Mov eax,false
ret
. ElseIf reason==dll_thread_attach ; A new thread generation for the same process
Invoke Messagebox,null, Addr sztext3,addr sztext0,mb_ok
mov reason,true
ret
. ElseIf reason==dll_thread_detach ; The thread of the same process destroys the
Invoke messagebox,null,addr sztext4,addr sztext0,mb_ok
Mov eax,false
ret
. endif
Dllentry ENDP

To convert the value in Edx:eax to a decimal output form string, which is familiar, as in the previous example!
; For example: edx=0,eax=01234567h, the converted string is:
; -> ' 19088743 ', 0
OUTEDXEAX proc uses ebx esi edi,lpstring
MOV edi,lpstring points to the address where the results are stored
MOV esi,lpstring
mov ecx,10 converted into decimal
. While eax!=0 | | Edx!=0
Push EAX
MOV Eax,edx
XOR Edx,edx
div ECX
MOV ebx,eax
Pop eax
div ECX
Add DL, ' 0 '
mov [EDI],DL storage results
Inc EDI
MOV edx,ebx
. ENDW

mov BYTE ptr [edi],0; string ending with 0
Dec EDI

. While Edi>esi results before changing, before changing!
MOV Al,[esi]
Xchg Al,[edi]
mov [esi],al
Inc ESI
Dec EDI
. ENDW
Invoke Messagebox,null,addr sztext5,addr SZTEXT0,MB_OK
Ret
Outedxeax ENDP
End Dllentry

; ================10.asm================
; Example: File name: 10.asm
; Call MyDll.dll to see if it works

.386
. Model Flat,stdcall
Option Casemap:none

Include Windows.inc

Include Mydll.inc
Include Masm32.inc
Include User32.inc
Include Kernel32.inc

Includelib Mydll.lib
Includelib Masm32.lib
Includelib User32.lib
Includelib Kernel32.lib

. DATA
Sztext db "hahaha ... 10.exe Run ", 0

. Data?
Charout db DUP (?)

. Code
Start
Invoke Messagebox,null,addr sztext,addr SZTEXT,MB_OK
MOV edx,12345678h
MOV eax,87654321h
Invoke Outedxeax,addr charout; convert with our own program!
Invoke Stdout,addr Charout
Invoke Exitprocess,null
End Start

All right, you'll have to save the disk and compile the run. What did you see? Seems to understand some of the Dongdong ha!
Yes, we can see this: Windows loads the dynamic library first and then runs the program.

If we put 10. ASM then make the following modifications, parallel disk for 10_01.asm

; ================10_01.asm================
; File name: 10_01.asm
; Call MyDll.dll to see if it works

.386
. Model Flat,stdcall
Option Casemap:none

Include Windows.inc
Include Masm32.inc
Include User32.inc
Include Kernel32.inc

Includelib Masm32.lib
Includelib User32.lib
Includelib Kernel32.lib

. DATA
Sztext db "hahaha ... 10_01.exe Run ", 0
Mydllfilename db "E:\masm32\xlfancy\mydll.dll", 0
Callfuncname db "Outedxeax", 0

. Data?
Charout db DUP (?)
Mydllhandle DD?
OUTEDXEAXADDR DD?

. Code
Start
Invoke MessageBox, NULL, addr sztext, addr Sztext, MB_OK

Invoke Outedxeax, addr charout, convert with our own program!

Invoke LoadLibrary, offset mydllfilename
or EAX, eax
JZ Exitpro
mov mydllhandle, eax
Invoke GetProcAddress, eax, offset callfuncname
or EAX, eax
JZ Freelib
mov outedxeaxaddr, eax
Lea EAX, Charout
Push EAX
mov edx, 12345678h
mov eax, 87654321h
Call OUTEDXEAXADDR

Invoke Stdout,addr Charout

Freelib:
Invoke FreeLibrary, Mydllhandle

Exitpro:
Invoke ExitProcess, NULL
End Start

Compile run. What did you see then? Now, Windows runs the program first, then loads the dynamic library.

These are the two different invocation methods of the dynamic library. After these two different invocation methods, the
We can get a rough understanding and understanding of the order and difference of Windows loaded EXE and DLLs. Shit In fact, I was #@$#%#%$%.

All of the above are compiled in this machine, but you can't see the result. (Toss a half-day, not only So, JMP ...)
No offense, no mistakes on line ... (Horse horse, also dare to paste out $%$#%$%%&%&^%, "bang", how can a brick hit the head? ¥#¥%¥%......%)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.