Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Introduction In DLLs Creation Using MASM

0.00/5 (No votes)
4 Oct 2009 1  
Shows how to create a simple DLL and invoke it in another program

Introduction

In this article, I will talk about the creation of a DLL using Assembly (MASM) and the creation of a program that invokes that sample DLL.

Creation of the DLL

Steps

First of all, you need to do some things. Define the normal things (.386 and the includes), then you will need to declare the main procedure of a DLL (LibMain), the next will be all the other procedures of the DLL. In this tutorial, I will use only one (PrintMess), but you can use however many you need.

Here is the code of the sample DLL:

 .386
option casemap :none   ; case sensitive
 include \masm32\include\masm32rt.inc 
.code
LibMain proc instance:dword,reason:dword,unused:dword 
    mov     eax,1
    ret
LibMain     endp
PrintMess proc
    print "Test", 10   ; message that will be printed by another program
    inkey   ; like pause command in batch
    exit   ; exits the program
PrintMess endp
End LibMain

Very Brief Description

In the PrintMess procedure, I'm using print to show a message in the screen, that 10 after will move the cursor to a new line for the inkey function. Now let's go to the program that will use this DLL.

Creation of the Program

Steps

First of all, you need to do some things. Define the normal things (.386, .model and the includes), then you will declare some variables (hLib and hProc), the next will be the main program by using the DLL.

Here is the code of the sample program:

.386
.model stdcall,flat
 include \masm32\include\kernel32.inc
 includelib \masm32\lib\kernel32.lib
 
.data
    hLib dword ?
    hProc dword ?
.data
    lib byte "testdll.dll", 0
    function byte "PrintMess", 0
.code
start:
    push offset lib
    call LoadLibrary ; will load the dll
    mov hLib, eax
    push offset function
    push hLib
    call GetProcAddress ; will get the procedure to execute
    mov hProc, eax
    call hProc ; will call your function in your DLL
    push hLib
    call FreeLibrary ; free the resource
    ret
end start

Brief Description

Now let's explain the code very quickly. I've declared a variable called lib that will store where the DLL is to open it and another variable called function that will store what procedure the program will execute (remember that you can create many other variables to other procedures), then the program will load the DLL using LoadLibrary that is stored in hLib variable. Next, the GetProcAddress will get the address of the procedure (PrintMess). After this, we need to call the function that is in hProc and to end we need to free the DLL using the FreeLibrary function.

History

  • 4th October, 2009: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here