Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
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
Nevron Gauge for SharePoint
Search :       Advanced Search »
Home » DLL/ActiveX » DLL Tutorial Part I : MFC DLL Basics

DLL Tutorial Part I : MFC DLL Basics

This tutorial explains basics of MFC DLL class.

Author Rank :
Page Views : 46687
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

I have distributed this tutorial in three parts. First part will explain basics of MFC Dlls. Second and third part will guide you towards developing your Regular and Extension DLLs.

What is a DLL?

DLL stands for Dynamic Link Library. A dll is a binary file which contains some functions. It allows another process to call its functions. More than one processes can call a dll simultaneously.

MFC support two types of dlls. Extension dlls and regular dlls. Each has its advantages and disadvantages.

Advantages of DLLs:

Dlls provide modularity to your application. You can break your big program into small modules, write some common functions in a separate module and compile it as a dll. Let all other modules call this dll. For example, A database application has three modules Input, Output, Processing. All three modules need to access a database. You can make a dll with database functions and let all three module call the dll to access the database. If you need to add more functions, you can add those functions to the dll. You don't have to change the entire application. Since multiple processes can share a dll in memory so dll saves memory, disk space and execution time because of swapping.

Extension DLLs

Extension DLLs implements reusable classes from MFC classes. They can export entire class. Client applications can create objects of that class and call its functions.

An MFC extension DLL has the following features and requirements:

  • The must implement DllMain and initialization can be done here.
  • Extension DLLs should be compiled with _AFXEXT defined. You need to add AFX_EXT_CLASS in dll's class.
  • MFC applications with _AFXDLL defined can only use Extension dlls.
  • Extension DLLs should not instantiate a class derived from CWinApp, but should rely on the client application (or DLL) to provide this object.
    See part III of this tutorial for how to develop and use extension dlls and more details.

Regular DLLs

If you need a DLL that can be used by Win32 or MFC applications then you need to create Regular dlls. Regular dlls can only export a C function. They can't export a class or its members as Extension dlls do.

You can link MFC library in two ways to the regular dlls. Either dynamically linked or statically liked. In static linking, your dll will include MFC library copy inside your code. Static linking make your dll size bigger. In dynamic linking, you dll doesn't copy MFC code but then you have to ship MFC with your dll.

See part II of this tutorial for how to develop and use regular dlls.

Dynamic Linking Vs. Static Linking

Dynamic linking allows an exe or dll to use required information at run time to call a DLL function. In static linking, the linker gets all the referenced functions from the static link library and places it with your code into your executable. Using DLLs instead of static link libraries makes the size of the executable file smaller. Dynamic linking is faster than static linking.

What kind of DLL you want?

This table show what kind of dll is your requirement.

DLL Requirements Clients DLL Selection
DLL does not use MFC -- non-MFC Win32 DLL
DLL will use MFC Clients may or may not be MFC applications MFC Regular DLL with dynamically link to MFC
DLL will use MFC All clients are MFC ( dynamically linked) and you want to export MFC derived classes Extension DLL

Building a DLL that dynamically links to MFC is faster than building a DLL that statically links to MFC because it is not necessary to link MFC itself. But then you must distribute the shared DLLs MFCx0.DLL and MSVCRT.DLL with your dll and that's a big pain.

How Clients find a DLL?

You can use LoadLibrary to load a dll from a specific path. If you link implicitly, Windows searches in these paths:

  • Current directory of exe.
  • Processes current directory.
  • Windows System Dir.
  • Windows Dir
  • Directories listed in the Path 

The best way is to copy your dll into systems directory but I don't like this approach because then your systems dir is a mess. Copying in exe 's dir is not a bad idea.

Steps to Use an Extension DLL

There are following steps required to perform by a client to use an extension dll.

1. Copy your extension class's header file to your project directory.
2. Link your project to the lib file of your dll.
3. Include header file of your class in your project's stdafx.h or cpp file.
4. Create Object of the class and call its member functions.

See how to create an extension dll in third part of this tutorial for more details.

Steps to Use a Regular DLL

1. Link to the Library. Copy your lib file 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.

2. Import functions. Import functions using __declspec. Write this code in the beginning of your cpp file.

extern "C" __declspec(dllimport) long AddTwoNumbers( long val1, long val2);
extern "C" __declspec(dllimport) long MultiplyTwoNumbers( long val1, long val2);

3. Call Functions. Add these public members to your dialog's header class.

int lSum = AddTwoNumbers( 12, 65 );
int lMultiplyRes = MultiplyTwoNumbers(12, 65) ; 

See how to create a regular dll in second part of this tutorial for more details. 

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:
Nevron Gauge for SharePoint
Become a Sponsor
 Comments
Calling a dll function in a function of a class by Dolly On December 26, 2007
i'm calling a dll function MakeLog(x,y,z) in a function MakeLog(y,z) of CMeter class. But its giveing me error 'x' unidefined identifier. DB_INT32 CMeter::MakeLog(DB_INT32 y, DB_INT32 z) { DB_INT32 dbi32RetVal = FAIL; dbi32RetVal = makeLog(x,y,z); return dbi32RetVal; }
Reply | Email | Modify 
Comment on DLL Tutorial by palla On October 13, 2008
I never saw like this clear explanation about DLLs in any other website. Thank you to Mahesh Chand. Can you sugest the link for Threads using MFC
Reply | Email | Modify 
Re: Comment on DLL Tutorial by Mahesh On April 7, 2009
I stopped working with C++ 9 years ago :) Not sure.
Reply | Email | Modify 

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.