Click here to Skip to main content
11,434,695 members (45,610 online)
Click here to Skip to main content

Creating and consuming MFC DLLs for Beginners

, 29 Jun 2002
Rate this:
Please Sign up or sign in to vote.
The objective of this tutorial is to demonstrate in a step by step manner how to build and call methods present in a DLL using Visual C++ 6.0. We are going to develop a DLL that takes in a string as input parameter and returns the string prefixed with a "Hello".
This is an old version of the currently published article.

Sample Image

Introduction

What is a DLL? DLL stands for Dynamic Link Library. Using DLL's offers several advantages as mentioned below:
  1. They simplify project management. If different groups work on different modules during the development process, the project is easier to manage.
  2. They help conserve memory. If two or more applications use the same DLL, the DLL has its pages in RAM once and the pages are shared by all of the applications. The C/C++ run-time library is a perfect example. Many applications use this library.
  3. They extend the features of an application. Since DLLs can be dynamically loaded into a process's address space, an application can determine at run time what actions to perform and then load the code to execute those actions on demand.
  4. They can be written in many programming languages. You can choose the best language for the job at hand. Perhaps your application's user interface is best programmed with Microsoft Visual Basic, but the business logic is better handled by C++.

Writing the application using a DLL

There are two parts to writing this application.
  1. Writing the DLL whose functions we are going to call
  2. Writing a test client for the DLL

Creating the DLL

  1. Fire up Visual C++ & choose the Project as MFC AppWizard(Dll) and type in Project name as MyDll
  2. Let the default selection for DLL type remain, i.e "Regular DLL using Shared MFC DLL"
  3. Click Finish and then Ok to get Visual Studio to generate the necessary files.
  4. Go to the class view, right click on “MyDll classes” and choose “New Class”.
  5. Now choose Class type as "Generic Class" .
  6. Type in the Class name CMyClass, the wizard automatically fills in the rest of the names.
  7. Go to the class view again
  8. Right click on the CMyClass and choose “Add Member Function”
  9. Type in the name of the function type as CString and fill in the function declaration as SayHello (CString strName). Choose Access type of the function as public. Note : CString is a MFC class that makes manipulation of strings very easy.
  10. Now go to file view and type in the following code into CMyClass.cpp as shown below in Code Snippet 2

  11. To be able to call the functions from an external application we have to prefix all function signatures with __declspec(dllexport) . This change is made in the CMyClass.h file as shown in Code Snippet 1

Compile the application and your DLL is ready

// CMyClass.h 
//{-----------     Code Snippet 1 --------------------------
class CMyClass  
{
public:
	__declspec(dllexport)  CString SayHello (CString strName);
	__declspec(dllexport)  CMyClass();
	__declspec(dllexport)  virtual ~CMyClass();

};

//{-----------     Code Snippet 1 --------------------------

// CMyClass.cpp
//{-----------     Code Snippet 2 --------------------------

CString CMyClass::SayHello(CString strName)
{
	//Return the input string with a Hello prefixed 
	 return "Hello " + strName; 
}

//{-----------     Code Snippet 2 --------------------------

Creating the DLL Client

Now we write a 'Client' to test our DLL This is a MFC dialog based application with a edit box and two buttons.
  1. Select New MFC AppWizard(exe) from the project menu and type in the project name TestDLL
  2. Choose Dialog based application and click Finish
  3. You would now be looking at the application in the resource view. Add an edit box to the application by selecting the edit box, next click on the dialog box and drag.
  4. Also create a CString (value) variable associated with it, call this variable m_edit i.e Click on the edit box and press CTRL and W to bring up the class wizard. Choose the member variables tab and choose IDC_EDIT1 and click on “Add Variable”, of type "Value" and type m_edit
  5. Now a file called TestDLLDlg.cpp would have been generated .
  6. Double click the “Ok” button on the dialog, the wizard pops up a box asking a name for the function, choose the default name “OnOk” to go to the TestDLLDlg.cpp file
  7. Modify TestDLLdlg.cpp to look like the Code Snippet 1, the code entered has no effect and the project at this point will not compile. (Basically we are calling a method of a class object, this object has not been defined as yet.)
  8. Goto the file TestDLLDlg.h and include the header file for your class i.e MyClass.h .
  9. In the file TestDLLDlg.h declare an object of your class objMyClass present in the DLL
  10. After steps 8 & 9 the code looks as in Code Snippet 2
  11. We need to modify the project settings to compile the project, Click on "Project->Settings->Link" and in the "Object/Library Modules" enter the complete or relative path to the DLL's library file. i.e. A .lib file is generated in the same folder as your DLL, I have entered "..\MyDll\Debug\MyDll.lib" here.
  12. Compile the application and if it has compiled successfully. Run it. 

    Hey, Why are we getting this stupid box saying "Unable to located DLL" ? 
    Solution: Copy the MyDll.dll to the same folder as the TestDll.exe

Run the application, enter a name in the text box and click the okay button, this will show a message box containing the same text prefixed with a hello.

//TestDLLdlg.cpp
// ----------------- Code Snippet 1 ----------------------------
void CTestDLLDlg::OnOK() 
{
		
	UpdateData(true);
	CString strResult = objMyClass.SayHello(m_edit);
	AfxMessageBox (strResult);
	//CDialog::OnOK();
	
}
// ----------------- Code Snippet 1 ----------------------------
// ----------------- Code Snippet 2 ----------------------------
//TestDLLDlg.h
/////////////////////////////////////////////////////////////////////////////
// CTestDLLDlg dialog


 #include "..\MyDll\MyClass.h" 

class CTestDLLDlg : public CDialog
{
// Construction
public:
	CTestDLLDlg(CWnd* pParent = NULL);	// standard constructor
	 CMyClass objMyClass; 

// Dialog Data

// ----------------- Code Snippet 2 ----------------------------

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Imran Ebrahim
Product Manager
India India
Imran is a software professional with 9 years of development experience and has worked on JAVA, VB6, VC6, C# & some JavaScript.

He is interested in Hi-Performance code, OO Methodology, OS API's, Optimizing Windows, Tweaking IE.

For the last few years Imran has specialized in developing robust, hi-performance applications & GUI's (Swing) for MNC's.

http://techspot121.blogspot.com/

Comments and Discussions


Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
 
QuestionMFC Extension Dll Pin
Taulie28-Nov-14 3:05
memberTaulie28-Nov-14 3:05 
QuestionI have a Dll file of 3rd party, how to using it [modified] Pin
Mạnh Lê4-Nov-13 16:50
memberMạnh Lê4-Nov-13 16:50 
AnswerRe: I have a Dll file of 3rd party, how to using it Pin
Taulie28-Nov-14 3:06
memberTaulie28-Nov-14 3:06 
GeneralRe: I have a Dll file of 3rd party, how to using it Pin
Mạnh Lê1-Dec-14 0:13
memberMạnh Lê1-Dec-14 0:13 
GeneralGood "how" starting point, needs more "why" Pin
Vaclav_Sal30-Mar-13 7:59
memberVaclav_Sal30-Mar-13 7:59 
GeneralRe: Good "how" starting point, needs more "why" Pin
Imran Ebrahim30-Mar-13 8:34
memberImran Ebrahim30-Mar-13 8:34 
SuggestionWrong information [modified] Pin
Edd_14-Aug-12 12:58
memberEdd_14-Aug-12 12:58 
GeneralRe: Wrong information Pin
Imran Ebrahim17-Aug-12 22:09
memberImran Ebrahim17-Aug-12 22:09 
QuestionWhat happens if shared MFC DLL is missing? Which are files linking to it? Pin
djninjarocks27-Jul-12 0:10
memberdjninjarocks27-Jul-12 0:10 
AnswerRe: What happens if shared MFC DLL is missing? Which are files linking to it? Pin
ruiwng21-Aug-12 3:21
memberruiwng21-Aug-12 3:21 
QuestionPlease Build Step-BY-Step project to use open source DLL in VC++ 2010 MFC Application Pin
Sumit Makhe8-Mar-12 22:15
memberSumit Makhe8-Mar-12 22:15 
GeneralMy vote of 1 Pin
goldenrose91-Mar-11 4:36
membergoldenrose91-Mar-11 4:36 
QuestionNice, but how do you "debug" this DLL? Pin
rbid29-Jul-09 23:14
memberrbid29-Jul-09 23:14 
AnswerRe: Nice, but how do you "debug" this DLL? Pin
KarstenK19-Aug-09 22:23
memberKarstenK19-Aug-09 22:23 
Questioncannot convert from 'const char [7]' to 'ATL::CStringT<BaseType,StringTraits>' Pin
Igen19-Aug-06 23:04
memberIgen19-Aug-06 23:04 
Generalcrash Pin
i_a_z9-Aug-06 1:44
memberi_a_z9-Aug-06 1:44 
Generalgett errors on compile test app Pin
ehauer19-Apr-06 9:41
memberehauer19-Apr-06 9:41 
Generalit works excelent Pin
nazg11-Mar-06 9:05
membernazg11-Mar-06 9:05 
GeneralRe: it works excelent Pin
stevan.t11-Jul-08 12:56
memberstevan.t11-Jul-08 12:56 
GeneralLinker error Pin
GrahamLuks29-Jan-06 15:33
memberGrahamLuks29-Jan-06 15:33 
GeneralRe: Linker error .. wherein I answer my own question. Pin
GrahamLuks29-Jan-06 16:31
memberGrahamLuks29-Jan-06 16:31 
GeneralConverting Console Exe to DLL Pin
santosh.vispute15-Jan-06 21:44
membersantosh.vispute15-Jan-06 21:44 
QuestionDLL entry point not found error Pin
Jaywant.Kulkarni26-Dec-05 3:27
memberJaywant.Kulkarni26-Dec-05 3:27 
AnswerRe: DLL entry point not found error Pin
cbm32cbm27-Sep-07 8:27
membercbm32cbm27-Sep-07 8:27 
AnswerRe: DLL entry point not found error Pin
cbm32cbm27-Sep-07 8:27
membercbm32cbm27-Sep-07 8:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150428.2 | Last Updated 30 Jun 2002
Article Copyright 2002 by Imran Ebrahim
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid