DLLs are Simple! Part 3






4.57/5 (99 votes)
Sep 30, 2004
2 min read

237027

5859
This article describes how to create a DLL with a DEF file and use it.
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
- Run VC++.
- Choose File>New.
- In the dialog box, choose "MFC AppWizard (DLL)" and name it, e.g.
DefExported
. - Declare a member function:
public: int Multiply(int PartOne,int PartTwo); CDefExportedApp();
- Then define it:
int CDefExportedApp::Multiply(int PartOne, int PartTwo) { return PartOne*PartTwo; }
- In the FileView tab, click "Source Files" and double click on DefExported.def.
- 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
- Click the Build Button.
- 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.
- Run VC++.
- Choose from the menu File>New.
- In the dialog box, choose "MFC AppWizard (EXE)" and name it, e.g.
DynamicLoad.
- Select "Dialog Based" and click the Finish button.
- Place a button control on the dialog and double click on it to create its click event.
- 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: Q117428.
- Enter the code for the button click event:
HINSTANCE hClcltr=LoadLibrary("DefExported.dll"); LPFNMLTPLY lpfnMuliply; lpfnMuliply = (LPFNMLTPLY)GetProcAddress(hClcltr,"Multiply");
- Now we can use the
Multiply
function by callinglpfnMultiply
and storing the return value.m_Rslt=lpfnMuliply(m_PartOne,m_PartTwo);
- When you are finished using the library, you must call the
FreeLibrary
API to release the memory allocated from theLoadLibrary
method.FreeLibrary( hClcltr );
History
- 30 September, 2004 -- Original version posted