Saturday, 13 July 2013

Dynamic Link Library..!

Dynamic Link Library
            A dynamic-link library (DLL) is an executable file that acts as a common store of functions. Dynamic linking provides a way for a process to call a function that is not part of its executable code. The executable code for the function is located in a DLL, which contains one or more functions that are compiled, linked, and stored separately from the processes that use them. DLLs also facilitate the sharing of data and resources. Multiple applications can simultaneously access the contents of a single copy of a DLL in memory.
Dynamic linking differs from static linking in that it allows an executable module (either a .dll or .exe file) to include only the information needed at run time to locate the executable code for a DLL function. In static linking, the linker gets all of the referenced functions from the static link library and places it with your code into your executable.
Using dynamic linking instead of static linking offers several advantages. DLLs save memory, reduce swapping, save disk space, upgrade easier, provide after-market support, provide a mechanism to extend the MFC library classes, support multilanguage programs, and ease the creation of international versions.
Create a dynamic link library (DLL) :
  1. On the menu bar, choose FileNewProject.
  2. In the left pane of the New Project dialog box, expand InstalledTemplatesVisual C++, and then select Win32.
  3. In the center pane, select Win32 Console Application.
  4. Specify a name for the project—for example, MathFuncsDll—in the Name box. Specify a name for the solution—for example, DynamicLibrary—in the Solution name box. Choose the OK button.
  5. On the Overview page of the Win32 Application Wizard dialog box, choose the Next button.
  6. On the Application Settings page, under Application type, select DLL.
  7. Choose the Finish button to create the project.

To add a class to the dynamic link library
// MathFuncsDll.h
ifdef MATHFUNCSDLL_EXPORTS
ifdef MATHFUNCSDLL_EXPORTS
##define MATHFUNCSDLL_API __declspec(dllexport)
#else
#define MATHFUNCSDLL_API __declspec(dllimport)
#endif
namespace MathFuncs
{
// This class is exported from the MathFuncsDll.dll
class MyMathFuncs
{
public:
// Returns a + b
static MATHFUNCSDLL_API double Add(double a, double b);

// Returns a - b
/
static MATHFUNCSDLL_API double Subtract(double a, double b);

// Returns a * b

static MATHFUNCSDLL_API double Multiply(double a, double b);

// Returns a / b

// Throws const std::invalid_argument& if b is 0
static MATHFUNCSDLL_API double Divide(double a, double b);
};
}
// MathFuncsDll.cpp : Defines the exported functions for the DLL application. 
//
include "stdafx.h"
include "stdafx.h"
#
include "MathFuncsDll.h"
#
include <stdexcept>
#
using namespace std;
namespace MathFuncs

{
double MyMathFuncs::Add(double a, double b)
{
return a + b;
}

double MyMathFuncs::Subtract(double a, double b)
{
return a - b;
}

double MyMathFuncs::Multiply(double a, double b)
{
return a * b;
}

double MyMathFuncs::Divide(double a, double b)
{
if (b == 0)
{
throw invalid_argument("b cannot be zero!");
}
return a / b;
}
}


To create an app that references the DLL
To use the functionality from the class library in the app
// MyExecRefsDll.cpp 
// compile with: /EHsc /link MathFuncsDll.lib

include <iostream>
#include "MathFuncsDll.h" 
using namespace std;

int main()
{
double a = 7.4;
int b = 99;


cout << "a + b = " <<
MathFuncs::MyMathFuncs::Add(a, b) << endl;
cout << "a - b = " <<
MathFuncs::MyMathFuncs::Subtract(a, b) << endl;
cout << "a * b = " <<
MathFuncs::MyMathFuncs::Multiply(a, b) << endl;
cout << "a / b = " <<
MathFuncs::MyMathFuncs::Divide(a, b) << endl;

try
{
cout << "a / 0 = " <<
MathFuncs::MyMathFuncs::Divide(a, 0) << endl;
}
catch (const invalid_argument &e)
{
cout << "Caught exception: " << e.what() << endl;
}

return 0;
}
To run the application
a + b = 106.4
a - b = -91.6
a / b = 0.074
a * b = 732.6
7475
cannot be zero!
Caught exception:
b
Reference : http://msdn.microsoft.com/en-us/library/vstudio/ms235636.aspx