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

Friday, 5 October 2012

Create a Static Library using Visual C++

Procedure to develop, implement Static Library and procedure to use a static library in the Visual C++ MFC application.

Please download source code from below link:


Step 1: Run Visual Studio 2008 Application
Step 2: File->New->Project
Step 3: Now Please add the Static Lib name and click on Ok button you can see below screen

Step 4: Select Static Library radio button click on Finish button. Empty project will be created as below:


Step 5: Now add the staticlib.h and staticlib.cpp files and please write your own functions prototypes in staticlib.h:

int Add(int iFirst,int iSecond);
int Subtract(int iFirst,int iSecond);

Step 6: Please implement the declared functions in staticlib.cpp:

#include "stdafx.h"
#include "staticlib.h"
int Add(int iFirst,int iSecond)
{
return (iFirst+iSecond);
}

int Subtract(int iFirst,int iSecond)
{
return (iFirst-iSecond);
}


Step 7: Now build the application your Static lib will be ready at application path in release or debug folder depending on build settings.


Now static library is ready. now how to test this library..?

Use Static Library in the Application

Step 1: Right click on Solution "StaticLib" and add new MFC Dialog based Application with name MathsApp




Step 2: After Project name is entered click on Ok button and your can see the below dialog from which please select Dialog Based  and click on finish.


Step 3: Now in the dialog please add labels and text boxes as  shown below screen shot


Step 4: Please write below code in the Add button click event handler. Please see the ADD function of static lib is called.


void CMathsAppDlg::OnBnClickedAdd()
{
// TODO: Add your control notification handler code here
CString strInputA = _T("");
CString strInputB = _T("");
CString strResult = _T("");

CEdit* pEdit = NULL;
int iInputA =0x00;
int iInputB =0x00;
int iResult =0x00;

pEdit = (CEdit*)GetDlgItem(IDC_EDT_INPUT_A);
pEdit->GetWindowTextW(strInputA);

pEdit = (CEdit*)GetDlgItem(IDC_EDT_INPUT_B);
pEdit->GetWindowTextW(strInputB);

iInputA = (int)_tcstol(strInputA,0,10);

iInputB = (int)_tcstol(strInputB,0,10);

iResult = Add(iInputA,iInputB);
strResult.Format(_T("%d"),iResult);
pEdit = (CEdit*)GetDlgItem(IDC_EDT_RESULT);
pEdit->SetWindowText(strResult);

}


Step 5: Please write below code in the Subtract button click event handler.Please see the Subtract function of static lib is called

void CMathsAppDlg::OnBnClickedSubract()
{
// TODO: Add your control notification handler code here
CString strInputA = _T("");
CString strInputB = _T("");
CString strResult = _T("");

CEdit* pEdit = NULL;
int iInputA =0x00;
int iInputB =0x00;
int iResult =0x00;

pEdit = (CEdit*)GetDlgItem(IDC_EDT_INPUT_A);
pEdit->GetWindowTextW(strInputA);

pEdit = (CEdit*)GetDlgItem(IDC_EDT_INPUT_B);
pEdit->GetWindowTextW(strInputB);

iInputA = (int)_tcstol(strInputA,0,10);

iInputB = (int)_tcstol(strInputB,0,10);

iResult = Subtract(iInputA,iInputB);
strResult.Format(_T("%d"),iResult);
pEdit = (CEdit*)GetDlgItem(IDC_EDT_RESULT);
pEdit->SetWindowText(strResult);
}

Step 6:
now if you compile you will get below errors 
Error 1 error C3861: 'Add': identifier not found e:\samples\staticlib\mathsapp\mathsappdlg.cpp 176 MathsApp
Error 2 error C3861: 'Subtract': identifier not found e:\samples\staticlib\mathsapp\mathsappdlg.cpp 207 MathsApp

Please do the below settings to compile application successfully:
 a. Please include the header path on top of the cpp file
        #include "staticlib.h"
b. Please set the header path as below screen shot


c. In the linker settings please do the below settings to run and call static lib functions successfully from application.
Set the Library directory path:
In Input settings add staticlib.lib as shown below screen shot

Now run the application and test the static lib functions and the Add function out put screen :


Subtract function output screen :


Now you have learned how implement a static library and how use the static library in the application.

Source code for above sample is available in the below link:

https://sites.google.com/site/staticlibsample/system/app/pages/admin/attachments