Creating DLLs That Use MFC

Microsoft Corporation

October 1996

Introduction

A dynamic-link library (DLL) is a binary file that acts as a shared library of functions that can be used simultaneously by multiple applications. DLLs are used for a variety of reasons, primarily for either sharing common code between different executable files or breaking an application into separate components, thus allowing easy upgrades.

The Microsoft® Foundation Class Library (MFC) supports three different DLL development scenarios:

Building a Regular DLL That Statically Links MFC

Use AppWizard to create a starting point for a regular DLL that statically links MFC. In MFC AppWizard (DLL) Step 1 of 1, select "Regular DLL With MFC Statically Linked."

If your DLL is statically linked to MFC, functions in your DLL can be called by any Win32® application, as well as by programs that also use MFC. Before version 4.0 of MFC, USRDLLs provided this type of functionality. Special variants of the MFC static-link libraries were used when building USRDLLs. These variants no longer exist. To create your statically linked regular DLL, use the standard MFC static-link libraries.

In Visual C++® version 4.0, you can now dynamically link your regular DLL to a shared MFC DLL. By dynamically linking to the MFC DLL, you can share the class library between multiple executable files to save disk and memory usage.

Building a Regular DLL That Dynamically Links MFC

Use AppWizard to create a starting point for a regular DLL that dynamically links MFC. In MFC AppWizard (DLL) Step 1 of 1, select "Regular DLL Using Shared MFC DLL."

In Visual C++ version 4.0, you can now dynamically link your DLL to the shared MFC DLL. To build this type of regular DLL, do NOT define the _USRDLL preprocessor symbol when building your DLL. (If the _USRDLL preprocessor symbol is defined in your DLL compile line, you will obtain a static link to the MFC libraries.) Instead, define the _AFXDLL preprocessor system when building your DLL to get dynamic linking. (AppWizard takes care of defining these symbols for you.)

If your regular DLL is dynamically linked to the shared MFC DLL, functions in your DLL can be called by any Win32 application, as well as by programs that also use MFC.

The MFC libraries needed to build this type of DLL follow the naming convention described in the article titled "DLLs: Naming Conventions" (MSDN Library, Product Documentation, Languages, VC++ 4.2, VC++ Books, MFC 4.2, Programming with MFC Encyclopedia, Dynamic-Link Libraries).

If you are building a C++ extension to MFC and want to put it in a DLL, you must create an extension DLL.

Building an Extension DLL

Use AppWizard to create a starting point for an extension DLL. In MFC AppWizard (DLL) Step 1 of 1, select "MFC Extension DLL (Using Shared MFC DLL)."

Before version 4.0 of MFC, this type of DLL was called an AFXDLL. AFXDLL refers to an _AFXDLL preprocessor symbol that is defined when building the DLL.

If your extension DLL is dynamically linked to the shared MFC DLL, functions in your DLL can only be called by MFC applications that also dynamically link to MFC. By dynamically linking to the AFXDLL, your DLL can derive new custom classes from MFC in the shared DLL, and then offer this extended version of MFC to applications that call your DLL.

The import libraries for the shared version of MFC are named according to the convention described in the article titled "DLLs: Naming Conventions." Visual C++ supplies prebuilt versions of the MFC DLLs, plus a number of non-MFC DLLs that you can use and distribute with your applications. These are documented in REDISTRB.WRI, which is found in the \REDIST directory on the Visual C++ CD-ROM.

Note   In Visual C++ version 4.0, the term "USRDLL" is obsolete. In earlier versions, USRDLL described DLLs that use MFC internally, but typically export functions using the standard "C" interface. USRDLLs could be used by either MFC or non-MFC applications. In version 4.0, such DLLs are called "regular DLLs." Regular DLLs, statically linked to MFC, have the same characteristics as the former USRDLLs.