Dynamic Link Library | create dynamic link library used to calculate interest with principle, no of years, rate of interest using vc++

Dynamic Link Library
OBJECTIVE:

To create a dynamic library which is used to calculate the simple interest with principle, no of years, rate of interest as the given parameters.
PROCEDURE:
1. Create a Dynamic link library:
1. Run MFC App Wizard(dll) to generate C:\DLL
2. In the step1 of the Appwizard select regular DLL using shared MFC DLL and click finish.
3. Create a new class by right clicking on the DLL classes, class type->generic class, Name->CMyclass.
4. A constructor and destructor will be created for the new class CMyClass.
5. Declare a member function in the MyClass.h file.
public:
float interest (float p,float n,float r);

6. Type the definition for the above member function in MyClass.cpp
float CMyClass::interest(float p,float n,float r)
{
return(p*n*r/100);
}
7. In the MyClass.h file include declspec(dllexport)in front of the constructor,destructor and the interest function so that these functions can exported to the client program.
8. New compile the Dll application,during the time of compilation,dll file will be created in the debug folder of the application.



2. Create the Client program:
1. Run MFC AppWizard(exe)to generate C:\Client.
2. Select dialog based in step1 of the wizard.
3. Select all the default settings & finish the wizard.
4. In the dialog create 4 edit controls and 2 command buttons.(IDC_BUTTON1,IDC_BUTTON2).The first 3 edit controls are used to get the input P,N,R and the 4th edit control to view the result.IDC_BUTTON1 to calculate &IDC_BUTTON2 to quit the application.
5. Using class wizard set the member variables for the edit controls of float data type m_p,m_n,m_r,m_res.
6. Using class wizard map message WM_LBUTTONDOWN to the object IDC_BUTTON1 & IDC_BUTTON2 and a notification hanker OnButton1,OnButton2 will be generated.
7. In ClientDlg.h include the path of the header file of the Dll which has the member function interest which is exported to client.
#include "C:\Dll\MyClass.h"
public:
CMyClass objclass;

8. Include the following lines in clientDlg.cpp
void CClientDlg::OnButton1()
{
UpdateData(true);
m_res=objclass.interest(m_p,m_n,m_r);
UpdateData(false);
}
void CClientDlg::OnButton2()
{
PostQuitMessage(0);
}
9. Go to project->settings->linktab->object/library modules->C:\DLL\Debug\Dll.lib.
10. Now copy the dll file which has been created in the C:\DLL\Debug to the client program in the path C:\Client.
11. Compile the client applications & run the application
12. .Click on the calculate button to view the result in the last edit control.


PROGRAM:
SOURCE FILES:

Myclass.cpp :
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Dll.h"
#include "MyClass.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMyClass::CMyClass()
{
}
CMyClass::~CMyClass()
{
}
float CMyClass::interest(float p,float n,float r)
{
return(p*n*r/100);
}

ClientDlg.cpp :
// ClientDlg.cpp : implementation file
#include "stdafx.h"
#include "Client.h"
#include "ClientDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClientDlg dialog
CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClientDlg)
m_p = 0.0f;
m_n = 0.0f;
m_r = 0.0f;
m_res = 0.0f;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClientDlg)
DDX_Text(pDX, IDC_EDIT1, m_p);
DDX_Text(pDX, IDC_EDIT2, m_n);
DDX_Text(pDX, IDC_EDIT3, m_r);
DDX_Text(pDX, IDC_EDIT4, m_res);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
//{{AFX_MSG_MAP(CClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CClientDlg message handlers

BOOL CClientDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < psysmenu =" GetSystemMenu(FALSE);">AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}
void CClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CClientDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags the minimized window.
HCURSOR CClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CClientDlg::OnButton1()
{
UpdateData(true);
m_res=objclass.interest(m_p,m_n,m_r);
UpdateData(false);
}
void CClientDlg::OnButton2()
{
PostQuitMessage(0);
}

HEADER FILES:

Myclass.h :
// MyClass.h: interface for the CMyClass class.
#if !defined(AFX_MYCLASS_H__0CAEEA43_9E9E_4D01_945E_9A006A0BED9A__INCLUDED_)
#define AFX_MYCLASS_H__0CAEEA43_9E9E_4D01_945E_9A006A0BED9A__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CMyClass
{
public:
_declspec(dllexport)float interest (float p,float n,float r);
_declspec(dllexport)CMyClass();
_declspec(dllexport)virtual ~CMyClass();
};
#endif // !defined(AFX_MYCLASS_H__0CAEEA43_9E9E_4D01_945E_9A006A0BED9A__INCLUDED_)

ClientDlg.h :
// ClientDlg.h : header file
#if !defined(AFX_CLIENTDLG_H__8EB6D5C3_51BC_4A2E_90A5_88E98325EB66__INCLUDED_)
#define AFX_CLIENTDLG_H__8EB6D5C3_51BC_4A2E_90A5_88E98325EB66__INCLUDED_
#include "C:\Dll\MyClass.h"
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/////////////////////////////////////////////////////////////////////////////
// CClientDlg dialog
class CClientDlg : public CDialog
{
// Construction
public:
CMyClass objclass;
CClientDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CClientDlg)
enum { IDD = IDD_CLIENT_DIALOG };
float m_p;
float m_n;
float m_r;
float m_res;
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CClientDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CClientDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnButton1();
afx_msg void OnButton2();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_CLIENTDLG_H__8EB6D5C3_51BC_4A2E_90A5_88E98325EB66__INCLUDED_)
output
Dynamic Link Library  create dynamic link library used to calculate interest with principle, no of years, rate of interest using vc++

Related post