HOWTO: Create Visual C++ Internal Makefile for MFC AFXDLL

ID: Q134797


The information in this article applies to:


SUMMARY

An MFC extension DLL is a DLL containing classes and functions written to embellish the functionality of the MFC classes. The MFC OLE DLL (MFCO25x.DLL) and the MFC ODBC DLL (MFCD25x.DLL) are examples of MFC extension DLLs. In addition, the DLLHUSK sample shipped with Visual C++ is specifically designed to illustrate how to create MFC extension DLLs (also known as AFXDLLs). However, the project file for the DLLHUSK could only be used as an external makefile within the Visual Workbench.

This article provides information to help you create internal makefiles for the DLLHUSK sample, including the steps needed to create the AFXDLL makefile and the DLL's calling application's makefile. The steps can be used to create any AFXDLLs and their calling applications in general.


MORE INFORMATION

How to Create an Internal Makefile for AFXDLL

  1. In the Visual Workbench, on the Project menu, click New to bring up the New Project dialog box.

    1. In the Project Name: field, click the Browse button, change to a proper directory, and then give a project name.


    2. In the Project Type: field, select "Windows dynamic link library(DLL)" as the project type.


    3. Clear the "Use Microsoft Foundation Classes" check box.


    4. Click OK to bring up the Edit-project.mak dialog box. Add the source files (.cpp), the module definition file (.def), and the necessary resource file (.rc) to the project. Close the dialog box. For example, to build the Testdll1.dll for the DLLHUSK example, add the Testdll1.cpp, Testdll1.def, and Testdll1.rc files to the project.




  2. In the Visual Workbench, on the Options menu, click Project to bring up the Project Options dialog box.

    1. Click the Compiler button to bring up the C/C++ Compiler Options dialog box.


    2. Select Memory Model under Category. Then change the memory model from Medium to Large, and make sure "SS!=DS, DS NOT loaded on function entry" is selected.


    3. Select Preprocessor under Category, and add the _AFXDLL preprocessor identifier to "Symbols and Macros to Define."


    4. Select Windows Prolog/Epilog under Category, and select the Generate for __far Functions check box. Make sure the "Protected Mode DLL Functions" option is selected.


    5. Click the OK button to save the settings, and exit the dialog box.


    In general, the compiler switches you must have when building the AFXDLLs are:
    
          /ALw /D "_AFXDLL" /GD /GEf 
    Add any other compiler switches used by the Visual Workbench.


  3. In the Project Options dialog box, click the Linker button to bring up the Linker Options dialog box. Add the MFC DLL's import library at the HEAD of the Input Libraries list. For example, if you build the DLL in the debug mode with Visual C++ version 1.5x, you should add mfc250d, and in the release, you should add mfc250. Click OK to save the settings, and exit the dialog boxes.

    The library files you should have are:
    
       Mfc250(d).lib
       Oldnames.lib
       Libw.lib
       Ldllcew.lib 


  4. Build the DLL.


  5. DLLHUSK's TESTDLL1 exports only one C interface function called InitTestDll1. This is done by placing an entry in the DLL's Exports section in the file Testdll1.def. See this file for an example. It may be a good idea to export C++ interfaces as well. This can be done by copying the C++ mangled names from the DLL's .map file to the .def file after the build, but before using implib. See "Inside Visual C++" by David J. Kruglinski (reference below) for more information.


  6. After you successfully build the DLL, use the following command line to create an import library that is accessible to the calling project link step:
    
       implib import_lib_name.lib project_name.def 
    NOTE: implib is located in your Msvc\Bin directory.

    You might want to make a batch file for this job. You can also add a tool to your Visual Workbench's Tools menu to accomplish this task instead of going to the command prompt every time. For information on how to modify the tools menu, please click Help in the Visual Workbench, then select "Visual Workbench" and "Modifying the Tools Menu."

    This step is essential. Even though the Visual Workbench generates an import library, that library is generated using the DLL instead of the the project's .def file. Therefore it is not usable in the calling project. This is because the AFXDLLs exports are done by ordinal without a string name in the resident or non-resident name table.

    To create the import library for the TESTDLL1.DLL in the DLLHUSK example, you can use this command:
    
       implib testdll1.lib testdll1.def 


  7. Copy the AFXDLL to a directory that is on the path or to the \Windows\System directory. Copy the import library to the calling project's directory or a directory that is on the LIB path.


  8. For the DLLHUSK sample, repeat steps 1 through 7 for TESTDLL2.


How to Create an Internal Makefile for an Application that Uses an AFXDLL

  1. In the Visual Workbench, on the Project menu, click New to bring up the New Project dialog box.

    1. In the Project Name: field, click the Browse button, change to a proper directory, and then give a project name.


    2. In the Project Type: field, select "Windows application (.EXE)" as the project type.


    3. Clear the "Use Microsoft Foundation Classes" check box.


    4. Click OK to bring up the Edit-project.mak dialog box. Add the source files (.cpp), the module definition file (.def), and the necessary resource file (.rc) to the project. Close the dialog box. For example, to build the Dllhusk.exe in the DLLHUSK example, add the Dllhusk.cpp, Dllhusk.def, and dllhusk.rc files to the project.




  2. In the Visual Workbench, on the Options menu, click Project to bring up the Project Options dialog box.

    1. Click the Compiler button to bring up the C/C++ Compiler Options dialog box.


    2. Select Memory Model under Category. Then change the memory model from Medium to Large, and make sure SS=DS is selected.


    3. Select Preprocessor under Category, and add the _AFXDLL preprocessor identifier to "Symbols and Macros to Define."


    4. Select Windows Prolog/Epilog under Category, and select the "Generate for __far Functions" check box. Also make sure the "Protected Mode Application Functions" option is selected.


    5. Click OK to save the settings, and exit the dialog box.


    In general, the compiler switches you must have when building the AFXDLL's calling projects are:
    
       /AL /D "_AFXDLL" /GA /GEf 
    Add any other compiler switches used by the Visual Workbench.


  3. In the Project Options dialog box, click the Linker button to bring up the Linker Options dialog box. Add MFC DLL's import library at the HEAD of the Input.Libraries list as you would for the DLL. Also add the import library of the AFXDLL just built in the "How to Create an Internal Makefile for AFXDLL" section of this article. Save the settings by clicking OK, and exit the dialog boxes.

    The library files you should have are:
    
       mfc250(d).lib
       afxdll_name.lib
       oldnames.lib
       libw.lib
       llibcew.lib 


  4. Build the application.



REFERENCES

For more information on creating an MFC AFXDLL, please see MFC Technical Note 33: DLL Version of MFC. For any compiler and linker switch information, please see the Help menu, or "Command-Line Utilities User's Guide" in Books Online.

For an additional AFXDLL sample, please see this book:


   Inside Visual C++
   Second Edition
   Version 1.5
   By David J. Kruglinski
   From Microsoft Press
   ISBN 1-55615-661-8 

Additional query words: 250 250D 2.0 2.5 2.00 2.50 2.51 2.52 2.52b


Keywords          : kb16bitonly kbnokeyword kbMFC kbVC 
Version           : WIN3X:1.0,1.5,1.51,1.52,1.52b;
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: July 30, 1999