Click here to Skip to main content
13,597,248 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


14 bookmarked
Posted 4 Oct 2009
Licenced CPOL

Introduction In DLLs Creation Using MASM

, 4 Oct 2009
Rate this:
Please Sign up or sign in to vote.
Shows how to create a simple DLL and invoke it in another program


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


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:

option casemap :none   ; case sensitive
 include \masm32\include\ 
LibMain proc instance:dword,reason:dword,unused:dword 
    mov     eax,1
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


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:

.model stdcall,flat
 include \masm32\include\
 includelib \masm32\lib\kernel32.lib
    hLib dword ?
    hProc dword ?
    lib byte "testdll.dll", 0
    function byte "PrintMess", 0
    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
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.


  • 4th October, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Nathan Campos
Brazil Brazil
I develop for mobiles since 2005, using J2ME for those old color mobile phones(like the good Nokia ones), after this at 2006 I've started on WIndows Mobile development using VB and some C#, at 2007 I was totally focused on Windows Mobile development, developing things like Calendars, simple games and a fully-featured text editor. Aug 2008 I moved to Symbian S60 3rd development using Python and C++. After that I've started on the platform that I like most: Android. One week ago I bought my first Mac, so I was going to start on iOS development, but I saw that it's so much difficult(Objective-C is the problem)

You may also be interested in...


Comments and Discussions

QuestionCompilation command Pin
Jean-Louis GUENEGO28-Mar-14 3:31
memberJean-Louis GUENEGO28-Mar-14 3:31 
GeneralMy vote of 1 Pin
shobits128-Nov-09 23:48
membershobits128-Nov-09 23:48 
GeneralRe: My vote of 1 Pin
Nathan Campos31-May-10 6:27
memberNathan Campos31-May-10 6:27 
GeneralMore detail! Pin
cplas5-Oct-09 18:35
membercplas5-Oct-09 18:35 
GeneralRe: More detail! Pin
Nathan Campos6-Oct-09 6:11
memberNathan Campos6-Oct-09 6:11 
GeneralRe: More detail! Pin
cplas6-Oct-09 7:33
membercplas6-Oct-09 7:33 
GeneralRe: More detail! Pin
Nathan Campos6-Oct-09 7:54
memberNathan Campos6-Oct-09 7:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.180621.3 | Last Updated 4 Oct 2009
Article Copyright 2009 by Nathan Campos
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid