Click here to Skip to main content
15,040,743 members
Articles / Programming Languages / C++
Posted 30 Sep 2004


110 bookmarked

DLLs are Simple! Part 3

Rate me:
Please Sign up or sign in to vote.
4.57/5 (101 votes)
30 Sep 20042 min read
This article describes how to create a DLL with a DEF file and use it.


In the third part of the assortment of articles "DLLs are Simple!" I describe how to create a DLL using a DEF file. What is a DEF file? It is a module-definition (*.def) file that is a text file containing one or more module statements that describe various attributes of a DLL, including:

LIBRARY statement statement identifies the DEF file as belonging to a DLL
EXPORTS statement lists the names of the functions exported by the DLL
DESCRIPTION statement describes the purpose of the DLL (optional)
LIBRARY      "DefExported"
DESCRIPTION  'DefExported For Present in CodeProject'
   Multiply @1

How to Build a DLL Using a DEF File

  1. Run VC++.
  2. Choose File>New.
  3. In the dialog box, choose "MFC AppWizard (DLL)" and name it, e.g. DefExported.
  4. Declare a member function:
     int Multiply(int PartOne,int PartTwo);
  5. Then define it:
    int CDefExportedApp::Multiply(int PartOne, int PartTwo)
     return PartOne*PartTwo;
  6. In the FileView tab, click "Source Files" and double click on DefExported.def.
  7. After the EXPORT statement, enter [function name] @[number] like this:
    LIBRARY      "DefExported"
    DESCRIPTION  'DefExported For Present in CodeProject'
     ; Explicit exports can go here
     Multiply @1
  8. Click the Build Button.
  9. Bring the DLL out of the oven!!

How to Use a DLL

To use a DLL dynamically, there are three simple API functions:

  • LoadLibrary ( [path of DLL] ) Loads a DLL into the process address, returning a handle to the DLL.
  • GetProcAddress ( [loaded library] , [function name] )
    Returns a handle of a function so that it can be used in your application.
  • FreeLibrary( [handle of loaded DLL] )
    Releases the memory allocated when the DLL was loaded.
  1. Run VC++.
  2. Choose from the menu File>New.
  3. In the dialog box, choose "MFC AppWizard (EXE)" and name it, e.g. DynamicLoad.
  4. Select "Dialog Based" and click the Finish button.
  5. Place a button control on the dialog and double click on it to create its click event.
  6. Before typing the code for the button click (BN_CLICKED) event, we must define a new function pointer with the correct number of parameters. This is done according to the parameters of the function we exported above.
    typedef int (CALLBACK* LPFNMLTPLY)(int,<span lang="en-us"> int);<span lang="en-us">
    <p>Sometimes you have to convert some variable types. For more information about this conversion, see Microsoft Support Article ID: <a href=";en-us;117428">Q117428</a>. </p>
  7. Enter the code for the button click event:
    HINSTANCE hClcltr=LoadLibrary("DefExported.dll");
    LPFNMLTPLY lpfnMuliply;
    lpfnMuliply = (LPFNMLTPLY)GetProcAddress(hClcltr,"Multiply");
  8. Now we can use the Multiply function by calling lpfnMultiply and storing the return value.
  9. When you are finished using the library, you must call the FreeLibrary API to release the memory allocated from the LoadLibrary method.
    FreeLibrary( hClcltr );


  • 30 September, 2004 -- Original version posted


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


About the Author

Mahmoud Komeily
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
I born in tehran at 1975
and began programming with commodore 64
I established "Pishro Narmafzar Iran" Corporation in 2001.
i'm expert in VC++, VC#, MS SQL Server, ASP .NET & have developed :
1-Persian Photoshop (fully localized)
2-Persian Freehand (fully localized)
3-Pardis (persian/arabic typing in graphical and video editing programs include : Ulead video studio,Ulead media studio,Pinnacle studio, Premiere, Flash, Freehand, 3D Max, Auto CAD, Photoshop, CorelDraw, Ulead Cool 3D,...)
Pardis is only persian/arabic typing tool for Ulead Video Studio in world.
Mahmoud komeily, mahmood komeili, محمود کمیلی

Comments and Discussions

QuestionHow to pass dynamilly created object to dll? Pin
dufduf23-May-10 22:47
Memberdufduf23-May-10 22:47 
GeneralMy vote of 2 Pin
imagiro20-May-10 9:11
Memberimagiro20-May-10 9:11 
GeneralReally Simple but the best DLL Explanation... Pin
Ajitsurana26-Oct-07 3:08
MemberAjitsurana26-Oct-07 3:08 
GeneralRe: Really Simple but the best DLL Explanation... Pin
Mahmoud Komeily26-Oct-07 20:11
MemberMahmoud Komeily26-Oct-07 20:11 
GeneralUsing dll and vectors Pin
gwyn2g24-Oct-06 19:42
Membergwyn2g24-Oct-06 19:42 
GeneralRe: Using dll and vectors Pin
cvilasis226-Mar-07 5:24
Membercvilasis226-Mar-07 5:24 
QuestionIt builds all, BUT... Pin
Igen111-Aug-06 1:22
MemberIgen111-Aug-06 1:22 
AnswerRe: It builds all, BUT... Pin
Member 23094206-Sep-06 20:13
MemberMember 23094206-Sep-06 20:13 
GeneralRe: It builds all, BUT... Pin
gwdog7-Nov-06 6:18
Membergwdog7-Nov-06 6:18 
QuestionWhere to put MFCAppWizard(exe) codes? Pin
xlav22217-Jul-06 13:55
Memberxlav22217-Jul-06 13:55 
GeneralThis is the best article I have read so far.. Pin
Prasanna Joshi27-Mar-06 19:31
MemberPrasanna Joshi27-Mar-06 19:31 
QuestionHow can i do? Pin
Chengxi Deng12-Dec-05 0:23
MemberChengxi Deng12-Dec-05 0:23 
AnswerRe: How can i do? Pin
MR186352025-Nov-07 22:25
MemberMR186352025-Nov-07 22:25 
GeneralVery good article Pin
akarwa24-Aug-05 10:07
Memberakarwa24-Aug-05 10:07 
Generalsetup .dll Pin
rlaley8-Nov-04 1:23
Memberrlaley8-Nov-04 1:23 
GeneralRe: setup .dll Pin
rlaley10-Nov-04 22:34
Memberrlaley10-Nov-04 22:34 
GeneralRe: setup .dll Pin
Mahmoud Komeily12-Nov-04 8:32
MemberMahmoud Komeily12-Nov-04 8:32 
Generalautomatically linking Pin
monarch119-Oct-04 21:48
Membermonarch119-Oct-04 21:48 
GeneralRe: automatically linking Pin
Tholkiem23-Oct-04 8:00
MemberTholkiem23-Oct-04 8:00 
QuestionWhat is in a dll Pin
Robert Metcalf5-Oct-04 2:05
MemberRobert Metcalf5-Oct-04 2:05 
AnswerRe: What is in a dll Pin
Anonymous5-Oct-04 11:12
MemberAnonymous5-Oct-04 11:12 
GeneralRe: What is in a dll Pin
Anonymous5-Oct-04 21:59
MemberAnonymous5-Oct-04 21:59 
AnswerRe: What is in a dll Pin
Peter Molnar6-Oct-04 13:57
MemberPeter Molnar6-Oct-04 13:57 
AnswerRe: What is in a dll Pin
vwood10-Jun-05 7:35
Membervwood10-Jun-05 7:35 
Generalsimplest DLL tutorial Pin
junas tafiro1-Oct-04 10:50
Memberjunas tafiro1-Oct-04 10:50 

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.