You are on page 1of 6

DLL

By Jorge Daniel Salcedo Del Pino 20121187I

Dynamic-link library (or DLL) is the Microsoft's


implementation of the shared library concept
in the Microsoft Windows operating systems.
This type of libraries usually has the file
extension DLL, OCX (for libraries that contain
ActiveX controls), or DRV (for legacy system
drivers). The file formats for DLLs are the This has the advantage of using less memory
same as for Windows EXE files. As with EXEs, than having the same library for each of the
DLLs can contain data, code, and resources, in applications running, but the disadvantage of
any combination. being a huge security risk, because a
compromised dll could execute malicious
The following list describes some of the files functions while being called from a genuine
that are implemented as DLLs in Windows application, even a secured Windows core
operating systems: application.
 ActiveX Controls (.ocx) files It is important to understand that
 Control Panel (.cpl) files
An example of a .cpl file is an item There are two types of DLLs, which are
that is in the Control Panel. Each item defined by the methods of linking that let you
is a specialized DLL. call the exported DLL functions.
 Device driver (.drv) files Load-time dynamic linking
An example of a device driver is a
printer driver that controls the When you use this type of linking the DLL
printing capabilities of a printer. functions are imported like local functions in
the application. For this, you need to provide
Another type of data files with the same file a header (.h) and an import library (.lib) when
format as a DLL, but with different file you compile and link the application. When
extensions and generally containing only you do this, the linker will provide the
resource sections, can be called resource information about the DLLs at load time.
DLLs. Examples this type of DLLs include icon
libraries, sometimes with the extension ICL,
and font files, with the extensions FON and Run-time dynamic linking
FOT.
In this type of linking the application call the
A shared library is a library that contains code LoadLibrary function to load the DLL at run
and data and can be used from different time. After the DLL is loaded you can use the
applications at the same time: function GetProcAddress to obtain the
address of the DLL function that you need. In
this case you do not need an import library
file.

There are several considerations we can take


into consideration when deciding which type
of linking we are going to use. Essentially, we  Application logic
can list them as: If you need your app to branch and
load different modules, like in a
 Startup performance
multilanguage app, then run-time
If the initial startup performance is
dynamic linking is a better fit
important to your application, you
should use run-time dynamic linking
 Ease of use One last point to consider is the DLL’s entry
Using load-time dynamic linking lets point, which is a function you can optionally
you call the DLL’s function like it were specify that is called each time a process
local functions, making them easier to attaches or detaches from the DLL
use
Example

Here we present an implementation of a DLL using load-time dynamic linking. In it we develop a DLL
which has all the functions needed to calculate a Fibonacci series recursively and an application
which uses it to show the series in a command prompt.

// MathLibrary.cpp : Defines the exported functions for the DLL.


#include "pch.h" // use pch.h in Visual Studio 2019
#include <utility>
#include <limits.h>
#include "MathLibrary.h"

// DLL internal state variables:


static unsigned long long previous_; // Previous value, if any
static unsigned long long current_; // Current sequence value
static unsigned index_; // Current seq. position

// Initialize a Fibonacci relation sequence


// such that F(0) = a, F(1) = b.
// This function must be called before any other function.
void fibonacci_init(
const unsigned long long a,
const unsigned long long b)
{
index_ = 0;
current_ = a;
previous_ = b; // see special case when initialized
}

// Produce the next value in the sequence.


// Returns true on success, false on overflow.
bool fibonacci_next()
{
// check to see if we'd overflow result or position
if ((ULLONG_MAX - previous_ < current_) ||
(UINT_MAX == index_))
{
return false;
}

// Special case when index == 0, just return b value


if (index_ > 0)
{
// otherwise, calculate next sequence value
previous_ += current_;
}
std::swap(current_, previous_);
++index_;
return true;
}

// Get the current value in the sequence.


unsigned long long fibonacci_current()
{
return current_;
}

// Get the current index position in the sequence.


unsigned fibonacci_index()
{
return index_;
}

// MathLibrary.h - Contains declarations of math functions


#pragma once

#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif

// The Fibonacci recurrence relation describes a sequence F


// where F(n) is { n = 0, a
// { n = 1, b
// { n > 1, F(n-2) + F(n-1)
// for some initial integral values a and b.
// If the sequence is initialized F(0) = 1, F(1) = 1,
// then this relation produces the well-known Fibonacci
// sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

// Initialize a Fibonacci relation sequence


// such that F(0) = a, F(1) = b.
// This function must be called before any other function.
extern "C" MATHLIBRARY_API void fibonacci_init(
const unsigned long long a, const unsigned long long b);

// Produce the next value in the sequence.


// Returns true on success and updates current value and index;
// false on overflow, leaves current value and index unchanged.
extern "C" MATHLIBRARY_API bool fibonacci_next();

// Get the current value in the sequence.


extern "C" MATHLIBRARY_API unsigned long long fibonacci_current();

// Get the position of the current value in the sequence.


extern "C" MATHLIBRARY_API unsigned fibonacci_index();

// MathClient.cpp : Client app for MathLibrary DLL.


// #include "pch.h" Uncomment for Visual Studio 2017 and earlier
#include <iostream>
#include "MathLibrary.h"

int main()
{
// Initialize a Fibonacci relation sequence.
fibonacci_init(1, 1);
// Write out the sequence values until overflow.
do {
std::cout << fibonacci_index() << ": "
<< fibonacci_current() << std::endl;
} while (fibonacci_next());
// Report count of values written before overflow.
std::cout << fibonacci_index() + 1 <<
" Fibonacci sequence values fit in an " <<
"unsigned 64-bit integer.\nEsta prueba fue hecha por Jorge Daniel
Salcedo Del Pino" << std::endl;
}

For this example we used Visual Studio 2019 and its default C++ compiler
Bibliography

 What are Drivers & DLLs? [Byte Size] | Nostalgia Nerd. (2016, October 11). Retrieved from
https://www.youtube.com/watch?v=5vLaqjR7DKI
 [TPSC], T. P. (2018, April 26). DLL vs EXE | Windows DLL Hell. Retrieved May 5, 2019, from
https://www.youtube.com/watch?v=HVigruKph74
 Dynamic-link library. (2019, April 26). Retrieved May 5, 2019, from
https://en.wikipedia.org/wiki/Dynamic-link_library
 What is a DLL? (n.d.). Retrieved May 5, 2019, from https://support.microsoft.com/en-
us/help/815065/what-is-a-dll

You might also like