Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Gauge for SharePoint
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » DLL/ActiveX » DLL Tutorial Part III : Extension DLLs

DLL Tutorial Part III : Extension DLLs

This Tutorial explains how to create an extension dll and use it a VC++ client application.

Author Rank :
Page Views : 31317
Downloads : 446
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
extndll.zip | extndll_client.zip
 
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
Gret by Duane On July 12, 2007
Great! But I personally need a tutorial program to go along with the explanations.
Reply | Email | Modify 
Re: Gret by Mahesh On July 12, 2007
Unfortunately, I stopped working with C++ in 2000 and have no interest in going back from C#.
Reply | Email | Modify 
super by Viswanathan On December 17, 2007
Your Tutorial part is very good
Reply | Email | Modify 
vc++ by Viswanathan On December 17, 2007
I am working vc++ technology. I like vc++ and c++. Now tried to learn c#.so please suggest how to start c#.
Reply | Email | Modify 
Rupesh by rupesh On January 6, 2010
Thank you so much mahesh now i learn extension dll frm your topic...i m lots of search that topic of example...but here i got it...thank u so much...
Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.