Make a dynamic link library

Source: Internet
Author: User

Now the program, needless to say, are in the call of others do a good job in the dynamic link library functions, can write their own dynamic link library it? The answer is YES! Let's get started!

;-------------------------------------------------------
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", this is the latest version of Aogo MASM for editplus requirements, only to specify the compilation mode, you can only press ctrl+1 to compile all the patterns, it is really convenient.
.386
. Model Flat,stdcall
Option Casemap:none

Include Windows.inc

. Code
;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 is generated/exited
Of course, the function name does not have to be this, but to be consistent with the last end dllentry.
dllentry proc Hinstdll:hinstance, Reason:dword, Reserved1:dword
MOV eax,true; If you return false, the dynamic-link library will not load
Ret
Dllentry ENDP

To convert the value in Edx:eax to a decimal output form string, which is familiar, as in the previous example!
OUTEDXEAX proc \; For example: edx=0,eax=01234567h, the converted string is:
Uses ebx esi edi,lpstring; -> ' 19088743 ', 0
mov edi,lpstring; point to address where results are stored
MOV esi,lpstring

mov ecx,10; convert to 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
Ret
Outedxeax ENDP
End Dllentry
-------------------------------------------------------------------
; FileName: Mydll.def, to be saved in the same directory as Mydll.asm
A module definition file that defines a function name that can be called by another program

Library Mydll The name of the dynamic link library
Exports outedxeax; Function name that can be called
-------------------------------------------------------------------
; file name: mydll.inc, function declaration

Outedxeax PROTO:D Word; a buffer pointer parameter that holds the converted substring, noting that the buffer is sufficient to store the result.
-------------------------------------------------------------------
Compile Link:

Open the file mydll.asm with EditPlus, execute "tools \ Compile & link & Run" or use "ctrl+1" directly, as long as the program has not entered the wrong, you can see the compiled file. As follows:

D:\masm7>dir Mydll

Volume in Drive D has no label
Volume Serial number is 18f0-186b
Directory of D:\MASM7

Mydll ASM 1,675 02-17-03 21:12 mydll.asm
Mydll DEF 02-17-03 20:48 mydll.def
Mydll INC 02-17-03 21:28 mydll.inc
Mydll DLL 2,560 02-17-03 20:49 MyDll.DLL
Mydll LIB 2,064 02-17-03 20:49 MyDll.lib
Mydll EXP 516 02-17-03 20:49 Mydll.exp
6 file (s) 6,879 bytes
0 dir (s) 2,398,564,352 bytes free

Note: Copy the mydll.inc to the \masm32\include directory, copy the mydll.lib to the \masm32\lib directory, and copy the MyDLL.DLL to the same directory as the program or ....
-------------------------------------------------------------------
Call the generated dynamic link library and see how it works!

; 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 Kernel32.inc

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

. Data?
Charout db DUP (?)

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

Open file 10.asm with EditPlus, execute "tools \ Compile & link & Run" or use "ctrl+1" directly to see the results of execution: 1311768467139281697, right?

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.