creating DLLs

IEOR 4500
Creating and using DLLs (pre-VS 2013)
(PLEASE SEE COMMENTS AT END REGARDING NEWER VERSIONS OF VS)
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
program.
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.
(next page)
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
Finish.
Now create a .cpp file that just contains the functions we wish to export. In our case, the file
looks like:
(next page)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MYPI2 6.28318531
double firstnormal(void);
double myrand(void)
{
double val;
val = ((double) rand())/((double) RAND_MAX);
return val;
}
double computechange(double mu, double sigma, double price, double deltat)
{
double drawn;
drawn = mu*deltat+ sigma*firstnormal()*sqrt(deltat);
return price*drawn;
}
double firstnormal(void)
{
double U1, U2, value;
U1 = myrand();
while(U1 < 1.0e-10)U1 = myrand();
U2 = myrand();
value = sqrt( -2*log(U1 ) )*cos(MYPI2*U2);
return value;
}
(next page)
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
easiest way.
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:
LIBRARY stockdll
EXPORTS
computechange @1
firstnormal @2
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.
(next page)
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:
#include <stdio.h>
#include <stdlib.h>
double computechange(double mu, double sigma, double price, double deltat);
void main(void)
{
double val;
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
picture below:
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.
DLLS Under newer versions of Visual Studio
Newer versions of VS try to encourage you to use more Microsoft-centric code. One way to
create a DLL project is to use the choices for the dialogue in VS that you use to choose which
project you want to create:
As you can see, the “precompiled header” choice is grayed out and cannot be changed. If you
choose this route, then after creation of the project, the “Solution Explorer” will look as follows:
The source files you see were all created by VS, and should be left alone (the name of this project
was “try1”). The project will compile now but will not do anything. In order to add your own
source code to the project, you need to modify each file by adding #include “stdafx.h” at the top.
Another way to build a DLL is to select “Empty project” in the above dialogue. This will also
work and will NOT create the additional files the picture above shows, and, also, in this case you
will NOT need to include stdafx.h in your source code.