Teach Yourself Visual C++ 6 in 21 Days
- 17 -
Sharing Your Functionality with Other Applications--Creating DLLs
- Why Create DLLs?
- Creating and Using an MFC Extension DLL
- Creating the MFC Extension DLL
- Adapting the Test Application
- Changing the DLL
- Creating and Using a Regular DLL
- Creating the Regular DLL
- Adapting the Test Application
- Summary
- Q&A
- Workshop
Yesterday you learned how you could create a set of functionality that might be useful
for multiple applications and how you could package it in a library file that could
be linked into those applications. Today you will learn how to do this same thing,
only with a much more dynamic package.
Often, a family of applications will have some functionality in common. When you
place this shared functionality into DLLs instead of library modules, all the applications
can use the same functionality with only a single copy of the functionality distributed
in the form of DLLs, instead of duplicating the same functionality in each of the
applications. This method saves disk space on any systems where the applications
are installed.
Today, you will learn
- About the different types of DLLs that you can create with Visual C++ and how
to determine which type best suits your needs.
- How to build two of these types of DLLs and the different approaches for the
various DLL types.
- How to use the functionality for both of these types of DLLs in a Visual C++
application.
- How to determine when an application needs to be relinked when you make modifications
to a DLL that is used by the application.
Why Create DLLs?
Dynamic link libraries (DLL) were introduced by Microsoft back in the early days
of Windows. DLLs are similar to library modules in that they both contain sets of
functionality that have been packaged for use by applications. The difference is
when the applications link to the library. With a library module (LIB), the application
is linked to the functionality in the library during the compile and build process.
The functionality contained in the library file becomes part of the application executable
file. With a DLL, the application links to the functionality in the library file
when the application is run. The library file remains a separate file that is referenced
and called by the application.
There are several reasons for creating DLLs instead of library module files. First,
you can reduce the size of the application executable files by placing functionality
that is used by multiple applications into DLLs that are shared by all of the applications.
You can update and modify functionality in the DLLs without having to update the
application executable (assuming that the exported interface for the DLL doesn't
change). Finally, you can use DLLs with just about any other Windows programming
language, which makes your functionality available to a wider number of programmers,
not just fellow Visual C++ programmers.
Creating and Using DLLs
DLLs are library files with compiled code that can be used by other applications.
The DLLs expose certain functions and classes to these applications by exporting
the function. When a function is exported, it is added to a table that is included
in the DLL. This table lists the location of all exported functions contained in
the DLL, and it is used to locate and call each of these functions. Any functions
that are not exported are not added to this table, and they cannot be seen or called
by any outside application or DLL.
An application can call the functions in the DLL in two ways. The more involved
method of calling these functions is to look up the location of the desired function
in the DLL and get a pointer to this function. The pointer can then be used to call
the function.
The other, much easier way (and the only way that you'll use in any of the examples
in this book) is to link the application with the LIB file that is created with the
DLL. This LIB file is treated by the linker as a standard library file, just like
the one that you cre-ated yesterday. However, this LIB file contains stubs for each
of the exported functions in the DLL. A stub is a pseudo-function that has the same
name and argument list as the real function. In the interior of the function stub
is a small amount of code that calls the real function in the DLL, passing all of
the arguments that were passed to the stub. This allows you to treat the functions
in the DLL as if they were part of the application code and not as a separate file.
NOTE: The LIB file for a DLL is automatically created for the DLL during
the compiling of the DLL. There is nothing extra that you need to do to create it.
TIP: Not only is it easier to create your applications using the LIB files
for any DLLs that you will be using, but also it can be safer when running the application.
When you use the LIB files, any DLLs that are used by your application are loaded
into memory the moment the application is started. If any of the DLLs are missing,
the user is automatically informed of the problem by Windows, and your application
does not run. If you don't use the LIB files, then you are responsible for loading
the DLL into memory and handling any errors that occur if the DLL cannot be found.
There are two types of DLLs that you can easily create using Visual C++. These
two types are MFC extension DLLs and regular DLLs.
NOTE: You can create other types of DLLs using Visual C++. All these other
types of DLLs involve a significant amount of ActiveX functionality, so they are
beyond the scope of this book. If you need to build ActiveX in-process server DLLs,
or other types of ActiveX DLLs, I recommend that you find an advanced book on Visual
C++ that provides significant coverage for these topics.
MFC Extension DLLs
MFC DLLs are the easiest to code and create because you can treat them just like
any other collection of classes. For any classes that you want to export from the
DLL, the only thing that you need to add is the AFX_EXT_CLASS macro in the class
declaration, as follows:
class AFX_EXT_CLASS CMyClass
{
.
.
.
};
This macro exports the class, making it accessible to Visual C++ applications.
You need to include this macro in the header file that is used by the applications
that will use the DLL, where it will import the class from the DLL so that it can
be used.
The one drawback to creating MFC extension DLLs is that they cannot be used by
any other programming languages. They can be used with other C++ compilers as long
as the compiler supports MFC (such as with Borland's and Symantec's C++ compilers).
Regular DLLs
The other type of DLL is a regular DLL. This type of DLL exports standard functions
from the DLL, not C++ classes. As a result, this type of DLL can require a little
more thought and planning than an MFC extension DLL. Once inside the DLL, you can
use class
|