Click here to Skip to main content
6,630,586 members and growing! (17,260 online)
Email Password   helpLost your password?
General Programming » DLLs & Assemblies » Beginners     Beginner

DLLs are Simple! Part 3

By Mahmoud Komeily

This article describes how to create a DLL with a DEF file and use it.
VC6, VC7Win2K, WinXP, Dev
Posted:30 Sep 2004
Views:152,745
Bookmarked:94 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
95 votes for this article.
Popularity: 8.40 Rating: 4.25 out of 5
2 votes, 2.1%
1
5 votes, 5.3%
2
4 votes, 4.2%
3
7 votes, 7.4%
4
77 votes, 81.1%
5

Introduction

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'
EXPORTS
   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:
    public:
     int Multiply(int PartOne,int PartTwo);
     CDefExportedApp();
  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'
    EXPORTS
     ; 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, int);
    

    Sometimes you have to convert some variable types. For more information about this conversion, see Microsoft Support Article ID: "http://support.microsoft.com/default.aspx?scid=kb;en-us;117428">Q117428.

  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.
    m_Rslt=lpfnMuliply(m_PartOne,m_PartTwo);
  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 );

History

  • 30 September, 2004 -- Original version posted

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

About the Author

Mahmoud Komeily


Member
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, محمود کمیلی
Occupation: Web Developer
Location: Iran, Islamic Republic Of Iran, Islamic Republic Of

Other popular DLLs & Assemblies articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 27 (Total in Forum: 27) (Refresh)FirstPrevNext
GeneralReally Simple but the best DLL Explanation... PinmemberAjitsurana4:08 26 Oct '07  
GeneralRe: Really Simple but the best DLL Explanation... PinmemberMahmood Komeily21:11 26 Oct '07  
GeneralUsing dll and vectors Pinmembergwyn2g20:42 24 Oct '06  
GeneralRe: Using dll and vectors Pinmembercvilasis26:24 26 Mar '07  
QuestionIt builds all, BUT... PinmemberIgen12:22 11 Aug '06  
AnswerRe: It builds all, BUT... PinmemberAnurag Chaudhary21:13 6 Sep '06  
GeneralRe: It builds all, BUT... Pinmembergmw_ee7:18 7 Nov '06  
GeneralWhere to put MFCAppWizard(exe) codes? Pinmemberxlav22214:55 17 Jul '06  
GeneralThis is the best article I have read so far.. PinmemberPrasanna Joshi20:31 27 Mar '06  
GeneralHow can i do? PinmemberChengxi Deng1:23 12 Dec '05  
GeneralRe: How can i do? PinmemberMR186352023:25 25 Nov '07  
GeneralVery good article Pinmemberakarwa11:07 24 Aug '05  
Generalsetup .dll Pinmemberrlaley2:23 8 Nov '04  
GeneralRe: setup .dll Pinmemberrlaley23:34 10 Nov '04  
GeneralRe: setup .dll PinmemberMahmood Komeily9:32 12 Nov '04  
Generalautomatically linking Pinmembermonarch1122:48 9 Oct '04  
GeneralRe: automatically linking PinmemberTholkiem9:00 23 Oct '04  
GeneralWhat is in a dll PinsussRobert Metcalf3:05 5 Oct '04  
GeneralRe: What is in a dll PinsussAnonymous12:12 5 Oct '04  
GeneralRe: What is in a dll PinsussAnonymous22:59 5 Oct '04  
GeneralRe: What is in a dll PinmemberPeter Molnar14:57 6 Oct '04  
GeneralRe: What is in a dll Pinmembervwood8:35 10 Jun '05  
Generalsimplest DLL tutorial Pinmemberjunas tafiro11:50 1 Oct '04  
GeneralNice article PinmemberRajesh Pillai8:04 1 Oct '04  
GeneralRe: Nice article PinmemberMahmood Komeily11:20 1 Oct '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 Sep 2004
Editor: Genevieve Sovereign
Copyright 2004 by Mahmoud Komeily
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project