Monday, April 19, 2010

DLL

DLL
DLL:
1.Run MFC AppWizard (dll)– check SDI and accept “Regular DD Using Shared MFC DLL” from the AppWizard page.
2.Add the code for the exported function DLLSquareRoot().
3.Build the project to create DLL.
Test:
1.Run MFC AppWizard(exe) – check SDI
3.InsertResourceDialog-IDD_DIALOG1.
4.Use class wizard to add data members and to create a test dialog class.
5.Define OnSqrt() to call the DLL’s exported function and declare.
6.Call DoModal() from view to open the dialog.
7.Project  Settings  Link TabObject/Library – specify the path to dllsquare.lib file.
8.copy the dllsquare.dll file from “dllsquare” project to the “test” project folder.
Build and test.
Source code:
a) Dllsquare Project:
dllsquare.cpp
#include “math.h”
// add the following code
extern "C" _declspec(dllexport) double DLLSquareRoot(double d)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());

if (d>=0.0)
return sqrt(d);
AfxMessageBox("Invalid Square Root");
return 0.0;
}
// Build the project and copy the file dllsquare.dll from the “dllsquare” project dir to the “test project” //directory.
b) DLLTest Project:
TestDialog.h
extern "C" __declspec(dllimport) double DLLSquareRoot(double d);


TestDialog.cpp
// Class Wizard
void CTestDialog::OnSqrt()
{
UpdateData(TRUE);
m_result=DLLSquareRoot(m_input);
UpdateData(FALSE);
}
dllTestView.cpp
#include "TestDialog.h"
//Class Wizard
void CDllTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
CTestDialog dlg;
dlg.DoModal();
}



a) Dllsquare Project:
dllsquare.cpp
// add the following code
extern "C" _declspec(dllexport) double DLLSquareRoot(double d)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
if (d>=0.0)
return d*d;
AfxMessageBox("Invalid Square Root");
return 0.0;
}
// Build the project and copy the file dllsquare.dll from the “dllsquare” project dir to the “test project” //directory.
b) DLLTest Project:
TestDialog.h
extern "C" __declspec(dllimport) double DLLSquareRoot(double d);
TestDialog.cpp
// Class Wizard
void CTestDialog::OnSqrt()
{
UpdateData(TRUE);
m_result=DLLSquareRoot(m_input);
UpdateData(FALSE);
}
dllTestView.cpp
#include "TestDialog.h"
//Class Wizard
void CDllTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
CTestDialog dlg;
dlg.DoModal();
}

No comments:

Post a Comment