Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Fast Track Visual C++ 6.0 Programming
(Publisher: John Wiley & Sons, Inc.)
Author(s): Steve Holzner
ISBN: 0471312908
Publication Date: 09/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Chapter 10
The Power of Dynamic Link Libraries

In this chapter, we work with Dynamic Link Libraries (DLLs). As you may have noticed, Windows programs can be very big. In fact, they could be much bigger. Imagine that your program used the MFC classes and had to place all those classes in the .exe file itself. Such a program would be huge. To save some space, Microsoft created the idea of Dynamic Link Libraries. Now the MFC classes fit into a set of DLLs and don’t have to be packaged in every .exe file. Many .exe files use the MFC library by connecting to those DLLs, and the code in those DLLs doesn’t have to be duplicated in all .exe files.

Why are they called dynamic link libraries? DLLs are called dynamic because they link to your .exe file at runtime, not at compile time. When you create an .exe file, many library routines may be linked into the code and placed in that .exe file. However, the routines in DLLs are only linked to your .exe file when it actually runs. In that way, you can place code common to several programs in the same DLL and save a lot of space.

Not only do DLLs offer space advantages, some programming tasks can only be performed with DLLs, as we see in Chapter 11, “Putting Windows Hooks to Work,” when we need to inject code into another process.


Updating Your Distributed Programs with DLLs

An additional benefit of DLLs is that they can make updating programs you’ve already distributed to users easier. If you change the code in a DLL, the user just has to place that new DLL in the windows\system directory instead of reinstalling your whole program from scratch.


Creating DLLs used to be a difficult process that programmers avoided if they could, but now AppWizard makes it easy. We see how to create a basic DLL and then examine how to make DLLs share memory even though they are linked into different programs. We begin by creating a standard DLL with the AppWizard.

The Doubler DLL

In our first DLL example, we create a DLL that exports one function, Multiply-ByTwo(). When a DLL exports a function, it becomes publicly available, which means that you must export all the functions you want to access from other programs. This function, MultiplyByTwo(), takes an integer parameter and returns twice that integer’s value; for example, passing it 2 returns 4.

Note that it’s not enough to create a DLL—we need a program that calls the functions in that DLL (that is, you don’t run a DLL). For that reason, we create two files: DoublerDLL.dll, the dynamic link library that holds MultiplyByTwo(), and Doubler.exe, an MFC program that links to DoublerDLL.dll and calls MultiplyByTwo().

In fact, we create a third file: DoublerDLL.lib. We link this file directly into Double.exe so that the .exe file knows what DLL to search for MultiplyByTwo(). (By default, DLLs are stored in the c:\windows\system directory, and we place ours there, because MFC .exe files search for them there.) We see how this works soon.

For now, select New in the Visual C++ File menu and click the AppWizard(dll) (not AppWizard(exe)) entry in the New box, as shown in Figure 10.1, giving this project the name DoublerDLL. Click OK to bring up the first and only step of the AppWizard, as shown in Figure 10.2.

The option button “Regular DLL using shared MFC DLL” is already selected in the AppWizard. Leave that option selected. The first option, “Regular DLL with MFC statically linked,” links the MFC routines you use into your DLL when you build it. The “Regular DLL using shared MFC DLL” option means we dynamically link to MFC at runtime. The “MFC Extension DLL (using shared MFC DLL)” option saves you some space, but you can only link it into MFC-based programs, not general Win32 programs. (The first two types of DLLs can be linked into both general Win32 programs as well as MFC programs.) Click the Finish button to create the new DLL project.


Figure 10.1  Creating a DLL with AppWizard.


Figure 10.2  Selecting DLL options.

AppWizard creates the new DLL project, including ReadMe.txt, which holds an overview of the files in this project.

========================================================================
        MICROSOFT FOUNDATION CLASS LIBRARY : DoublerDLL
========================================================================

AppWizard has created this DoublerDLL DLL for you.  This DLL not only
demonstrates the basics of using the Microsoft Foundation classes but
is also a starting point for writing your DLL.

This file contains a summary of what you will find in each of the files that
make up your DoublerDLL DLL.

DoublerDLL.dsp
    This file (the project file) contains information at the project level
and
    is used to build a single project or subproject. Other users can share
the
    project (.dsp) file, but they should export the makefiles locally.

DoublerDLL.h
    This is the main header file for the DLL.  It declares the
    CDoublerDLLApp class.

DoublerDLL.cpp
    This is the main DLL source file.  It contains the class CDoublerDLLApp.


DoublerDLL.rc
    This is a listing of all of the Microsoft Windows resources that the
    program uses.  It includes the icons, bitmaps, and cursors that are
stored
    in the RES subdirectory.  This file can be directly edited in Microsoft
    Visual C++.

res\DoublerDLL.rc2
    This file contains resources that are not edited by Microsoft
    Visual C++.  You should place all resources not editable by
    the resource editor in this file.

DoublerDLL.def
    This file contains information about the DLL that must be
    provided to run with Microsoft Windows.  It defines parameters
    such as the name and description of the DLL.  It also exports
    functions from the DLL.

DoublerDLL.clw
    This file contains information used by ClassWizard to edit existing
    classes or add new classes.  ClassWizard also uses this file to store
    information needed to create and edit message maps and dialog data
    maps and to create prototype member functions.

/////////////////////////////////////////////////////////////////////////////
Other standard files:

StdAfx.h, StdAfx.cpp
    These files are used to build a precompiled header (PCH) file
    named DoublerDLL.pch and a precompiled types file named StdAfx.obj.

Resource.h
    This is the standard header file, which defines new resource IDs.
    Microsoft Visual C++ reads and updates this file.

/////////////////////////////////////////////////////////////////////////////
Other notes:

AppWizard uses “TODO:” to indicate parts of the source code you
should add to or customize.

/////////////////////////////////////////////////////////////////////////////

Now that we’ve created our project, it’s time to create our new function: MultiplyByTwo().


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.