The third part of DLL Tutorial explains how to create an extension dll and use it a VC++ client application.
Creating an MFC Extension Dll
Create Skeleton of the Dll
Use MFC AppWizard to create an extension dll's skeleton.

Select MFC Extension Dll

Click Finish. As you see on this dialog, Main Source code in mcExtnDll.h and cpp.

Examine the Files
| File |
Description |
| mcExtnDll.cpp |
Main code implementation file includes DllMain function |
| mcExtnDll.h |
Header definition file |
| mcExtnDll.def |
A definition file |
Adding a Class and Its Members
In my project I will add a new class called CMcCalculator. This class have two functions Add and Multiply.
Add a new class Right click on mcExtnDll Classes in Class View and Click New Class.

Select Generic Class from the drop-down, type your class name and click OK.

This action adds a new class CMcCalculator to your project. It has only a constructor and a destructor. Now lets add member functions to this class.
Add Member Functions Right click on CMcCalculator class and Click Add Member Functions

Here is definition of your Add and Multiply member functions.


Wizard adds code to these functions. Add returns sum of two values and Multiply returns multiplied value of two numbers.
int CMcCalculator::Add(int val1, int val2)
{
return val1+val2;
}
long CMcCalculator::Multiply(long lVal1, long lVal2)
{
return lVal1*lVal2;
}
From my client programs, I will export/Import this CMcCalculator class.
Edit mcExtnDll.h Class
Double click on mcExtnDll.h class and edit AFX_EXT_CLASS. See bold part here.
class AFX_EXT_CLASS CMcCalculator
{
public:
long Multiply ( long lVal1, long lVal2);
int Add( int val1, int val2 );
CMcCalculator();
virtual ~CMcCalculator();
};
Build the Project and Copy the Dll
Now compile and build the project in Release mode ( not necessary) and copy the dll mcExtnDll.dll from your Release directory to the Winnt\System32 ( Windwos\System for Windows 98 ) directory.
A DLL Test Client Program
I have built a dialog based application to test this mcExtnDll.dll. You can build your own dialog based or other applications. Here are the steps:
1. Create a dialog based application. Your dialog template should look like this.

2. Copy McCalculator.h (your extension class's header file) class to your project directory.
3. Link the Library. Copy mcExtnDll.lib from your Dll's Release ( or Debug ) directory to your test project directory. Or if you don't want to copy the lib file then enter your lib file with the path in Object/library modules text box.

4. Include header file. Include 'mcCalculator.h' in your project's stdafx.h file.
#endif // _AFX_NO_AFXCMN_SUPPORT
#include "mccalculator.h"
//{{AFX_INSERT_LOCATION}}
5. Create Object of the class. Add these public members to your dialog's header class.
class CDlgTestDlg : public CDialog
{
// Construction
public:
CDlgTestDlg(CWnd* pParent = NULL); // standard constructor
CMcCalculator cal;
long a1, a2 ;
6. Use the class. Now we are ready to use Add and Multiply member functions of extension dll's McCalculator class. Override OnClick of Add and Multiply buttons and write this code:
void CDlgTestDlg::OnAdd()
{
a1 = GetDlgItemInt( IDC_EDIT1);
a2 = GetDlgItemInt( IDC_EDIT2);
int a3 = cal.Add(a1,a2);
SetDlgItemInt( IDC_EDIT3, a3, TRUE );
}
void CDlgTestDlg::OnMultiply()
{
a1 = GetDlgItemInt( IDC_EDIT1);
a2 = GetDlgItemInt( IDC_EDIT2);
int a3 = cal.Multiply(a1,a2);
SetDlgItemInt( IDC_EDIT3, a3, TRUE );
}
7. Build and Run your project. Enter two numbers in edit box 1 and 2 and click Add or Multiply.

