Creating and using DLLs
DLLs (Dynamic link libraries) are a very useful feature, as they encourage code reuse and they
help keep programs tidy and simple. They are also very easy to setup and run.
Simply put, a DLL encapsulates executable code in a “library” so that other programs can use that
code without explicitly including (and having to recompile) the code itself.
Windows DLLs differ from, say, Unix libraries in that a DLL is created, one specifies which of
the functions defined in the DLL are actually “exported” and will be available for use by a calling
The creation of the DLL produces the DLL itself (a file with a .dll extension) and a “library” (a
file with a .lib extension). When we run a calling program, then (at runtime) the DLL will be
loaded as soon as the calling program requires the use of one of the exported functions. What is
being used at that point is the .dll file (i.e. it is loaded into memory and is integrated into the
executable code for the calling program which at that point is already loaded into memory). The
.lib file is used when compiling the calling program and is simply there to help the compilation
process – it tells the compiler that at runtime a DLL will be loaded containing the executable code
for the exported functions.
There are many ways to create and use a DLL. For a complete description of what a DLL is, see:
http://support.microsoft.com/kb/815065. The method we will see here for creating a DLL is the
one that requires the least number of Windows-specific steps. Furthermore, it requires no
changes whatsoever to the code that we want to export.
In this writeup we will show how to create a simple DLL. To fix ideas, we will create a DLL that
handles basic functions arising in the geometric Brownian motion model for the evolution of a
stock’s price (see Hull’s book). This DLL will export two functions: one function evaluates the
change in a stock’s price under the Brownian motion model, and the other function draws a
sample from a normal distribution.
How to create a DLL
To begin, you create a “Win32 project”.
Don’t forget to choose an appropriate name, and to check the location where the project will go.
Then choose “OK,” in the next screen, choose “Next,” and in the next screen, under “Application
Type” choose DLL and under “Additional Properties” choose an empty DLL project. Then
Now create a .cpp file that just contains the functions we wish to export. In our case, the file
#define MYPI2 6.28318531
val = ((double) rand())/((double) RAND_MAX);
double computechange(double mu, double sigma, double price, double deltat)
drawn = mu*deltat+ sigma*firstnormal()*sqrt(deltat);
double U1, U2, value;
U1 = myrand();
while(U1 < 1.0e-10)U1 = myrand();
U2 = myrand();
value = sqrt( -2*log(U1 ) )*cos(MYPI2*U2);
Note that there is no “main” function, but by itself this file should compile without errors: it
includes all appropriate header files, and it uses prototypes as needed. Use Project->Add
Existing Item to select and add this file to the project.
This file has all that we need from the point of view of the functionality we want to export, but it
is not yet enough to create a DLL. There are numerous ways of doing this; we will see next the
This consists of creating a “module definition file”, or .def file for short. Click on Project->Add
new item, and select “C++”, and choose a name with the .def extension:
Click on “Add”. Then type:
The purpose of this file is to tell the compiler to create a library named “stockdll” and to “export”
the functions computechange and firstnormal. You can export as many functions as you like.
The @1 and @2 are needed, but don’t worry about their exact purpose.
Click on “Save”. Then go: Project->properties, expand “Configuration Properties”, expand
“Linker”, and click on “Input”.
Then in the row for “Module Definition File” type .\xxxx.def, where “xxxx.def” is the name of
your .def file:
Now you can compile and link. If this happens without errors, then the Debug subdirectory will
contain, in addition to the usual files, a file named “stockdll.dll” and another one named
“stockdll.lib”. The .dll file contains the actual binary that will be used at runtime. On the other
hand, stockdll.lib is a “library” file (think of it as a set of instructions for another program on how
to use the dll).
One last step: copy these two files to an appropriate location. I keep a special directory for all my
.dll files and another one for all my .lib files.
Using the DLL
Creating the DLL is, of course, only half of the story. Now we want to use it. We will write a
simple program that exercises the DLL we just created above. We create a console application
that includes just the following file:
double computechange(double mu, double sigma, double price, double deltat);
val = computechange(1.0, 0.5, 4.0, 1/252.0);
printf("val = %.3e\n", val);
This program will compile without errors – but will not build, for the simple reason that there is
no “computechange” function defined (though we did provide its prototype, which is needed).
Instead, we will link this program with the DLL created above.
To do so, go to Tools > Options, expand “Projects and Solutions”, and click on “VC++
Directories”. In the “Show directories for” drop-down box choose “Executable files”. See the
Now you either scroll down to the very bottom of the window listing directories, or click on the
yellow folder, and you will be able to type in the path to the folder where you keep the DLLs. In
the above picture this has already been done. Now go back to the “Show directories for” and this
time choose Library files. In this case you will enter the location of the .lib file you created.
Next, you need to link with the DLL – this is the process by which the compiler sets up “hooks”
in your program so that, at runtime, your program and the DLL can be made to work together.
Go: Project->Properties, expand Linker, select Input, and on the “Additional Dependencies” line
type the name of the library:
Then hit OK.
Now the project will compile correctly.
But wait: one more step is needed. When you actually want to run this project, at runtime the
location of the .dll file must be known as part of your environment. You need to change your
PATH environment variable to include the path to the .dll file. To do this, use the Control Panel,
then System, then click on Advanced, then click on Environment Variables, and then go to the far
right in the PATH window to add the path to the dll – watch out for the semicolons.
Now the project will run.