Using Extension DLL,Database/OLE/Sockets in Regular DLL

Visual Studio 6.0 Do IFAQDetailsSample

When using an extension DLL from a regular DLL, if the extension DLL is not wired into the CDynLinkLibrary object chain of the regular DLL, you may run into one or more of a set of related problems. Because the debug versions of the MFC Database, OLE, and Sockets support DLLs are implemented as extension DLLs, you might see similar problems if you are using these MFC features, even if you're not explicitly using any of your own extension DLLs. Some symptoms are:

  • When attempting to de-serialize an object of a type of class defined in the extension DLL, the message "Warning: Cannot load CYourClass from archive. Class not defined." appears in the TRACE debug window and the object fails to serialize.

  • An exception indicating 'bad class' may be thrown.

  • Resources stored in the extension DLL fail to load because AfxFindResourceHandle returns NULL or an incorrect resource handle.

  • DllGetClassObject, DllCanUnloadNow, and the UpdateRegistry, Revoke, RevokeAll, and RegisterAll member functions of COleObjectFactory fail to locate a class factory defined in the extension DLL.

  • AfxDoForAllClasses does not work for any classes in the extension DLL.

  • Standard MFC database, sockets, or OLE resources fail to load. For example, AfxLoadString(AFX_IDP_SQL_CONNECT_FAIL) returns an empty string, even when the regular DLL is properly using the MFC Database classes.

The solution to these problems is to create and export an initialization function in the extension DLL that creates a CDynLinkLibrary object. Call this initialization function exactly once from each regular DLL which uses the extension DLL.

MFC OLE, MFC Database (or DAO), or MFC Sockets Support

If you are using any MFC OLE, MFC Database (or DAO), or MFC Sockets support in your regular DLL, respectively, the MFC debug extension DLLs MFCOxxD.DLL, MFCDxxD.DLL, and MFCNxxD.DLL (where xx is the version number) are linked in automatically. You must call a predefined initialization function for each of  these DLLs that you are using.

For database support, add a call to AfxDbInitModule to your regular DLL's CWinApp::InitInstance function. Make sure this call occurs before any base-class call or any added code which accesses the MFCDxxD.DLL. This function takes no parameters and returns void.

For OLE support, add a call to AfxOleInitModule to your regular DLL's CWinApp::InitInstance. Note that the COleControlModule InitInstance function calls AfxOleInitModule already, so if you are building an OLE control and are using COleControlModule, you should not add this call to AfxOleInitModule.

For Sockets support, add a call to AfxNetInitModule to your regular DLL's CWinApp::InitInstance.

Note that release builds of MFC DLLs and applications do not use separate DLLs for database, sockets, or OLE support. However, it is safe to call these initialization functions in release mode.

CDynLinkLibrary Objects

During each of the operations mentioned at the beginning of this article, MFC needs to search for a desired value or object. For example, during de-serialization, MFC needs to search through all of the currently available runtime classes to match objects in the archive with their proper runtime class.

As a part of these searches, MFC scans through all of the extension DLLs in use by walking a chain of CDynLinkLibrary objects. CDynLinkLibrary objects attach automatically to a chain during their constructor and are created by each extension DLL in turn during initialization. In addition, every module (application or regular DLL) has its own chain of CDynLinkLibrary objects.

In order for an extension DLL to get wired into a CDynLinkLibrary chain, it must create a CDynLinkLibrary object in the context of every module that uses the extension DLL. Therefore, if an extension DLL is going to be used from regular DLLs, it must provide an exported initialization function that creates a CDynLinkLibrary object. Every regular DLL that uses the extension DLL must call the exported initialization function.

If an extension DLL is only going to be used from an MFC application (.EXE) and never from a regular DLL, then it is sufficient to create the CDynLinkLibrary object in the extension DLL's DllMain. This is what the AppWizard extension DLL code does. When loading an extension DLL implicitly, DllMain loads and executes before the application ever starts. Any CDynLinkLibrary creations are wired into a default chain that the MFC DLL reserves for an MFC application.

Note that it is a bad idea to have multiple CDynLinkLibrary objects from one extension DLL in any one chain, especially if the extension DLL will be dynamically unloaded from memory. Do not call the initialization function more than once from any one module.

Sample Code

This sample code assumes that the regular DLL is implicitly linking to the extension DLL. This is accomplished by linking to the import library (.LIB) of the extension DLL when building the regular DLL.

These lines should be in the source of the extension DLL:

// YourExtDLL.cpp:

// standard MFC extension DLL routines
#include "afxdllx.h"


extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
    if (dwReason == DLL_PROCESS_ATTACH)
        // extension DLL one-time initialization.
        if (!AfxInitExtensionModule(extensionDLL, hInstance))
           return 0;
    return 1;   // ok

// Exported DLL initialization is run in context of
// application or regular DLL
extern "C" void WINAPI InitYourExtDLL()
    // create a new CDynLinkLibrary for this app
    new CDynLinkLibrary(extensionDLL);

    // add other initialization here

Be sure to export the InitYourExtDLL function. This could be done using __declspec(dllexport), or in your DLL's .DEF file as follows:

// YourExtDLL.Def:

Add a call to the InitInstance member of the CWinApp-derived object in each regular DLL using the extension DLL:

// YourRegularDLL.cpp:

class CYourRegularDLL : public CWinApp
    virtual BOOL InitInstance(); // Initialization
    virtual int ExitInstance();  // Termination

    // nothing special for the constructor
    CYourRegularDLL(LPCTSTR pszAppName) : CWinApp(pszAppName) { }

BOOL CYourRegularDLL::InitInstance()
  // any DLL initialization goes here
    TRACE0("YOUR regular DLL initializing\n");

  // wire any extension DLLs into CDynLinkLibrary chain

    return TRUE;

What do you want to do?

What do you want to know more about?