Microsoft KB Archive/134797

{|
 * width="100%"|

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

 * }

Q134797

-

The information in this article applies to:


 * The Microsoft Foundation Classes (MFC), included with:
 * Microsoft Visual C++, versions 1.0, 1.5, 1.51, 1.52, 1.52b

-

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.

How to Create an Internal Makefile for AFXDLL
 In the Visual Workbench, on the Project menu, click New to bring up the New Project dialog box.

 In the Project Name: field, click the Browse button, change to a proper directory, and then give a project name. In the Project Type: field, select "Windows dynamic link library(DLL)" as the project type. Clear the "Use Microsoft Foundation Classes" check box. 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.   In the Visual Workbench, on the Options menu, click Project to bring up the Project Options dialog box.

 Click the Compiler button to bring up the C/C++ Compiler Options dialog box.</li> 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.</li> Select Preprocessor under Category, and add the _AFXDLL preprocessor identifier to "Symbols and Macros to Define."</li> 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.</li> Click the OK button to save the settings, and exit the dialog box.</li></ol>

In general, the compiler switches you must have when building the AFXDLLs are: <pre class="CODESAMP">     /ALw /D "_AFXDLL" /GD /GEf Add any other compiler switches used by the Visual Workbench. </li>  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: <pre class="CODESAMP">  Mfc250(d).lib Oldnames.lib Libw.lib Ldllcew.lib </li> Build the DLL.</li> 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.</li>  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: <pre class="CODESAMP">  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: <pre class="CODESAMP">  implib testdll1.lib testdll1.def </li> 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.</li> For the DLLHUSK sample, repeat steps 1 through 7 for TESTDLL2.</li></ol>

How to Create an Internal Makefile for an Application that Uses an AFXDLL
 In the Visual Workbench, on the Project menu, click New to bring up the New Project dialog box.

 In the Project Name: field, click the Browse button, change to a proper directory, and then give a project name.</li> In the Project Type: field, select "Windows application (.EXE)" as the project type.</li> Clear the "Use Microsoft Foundation Classes" check box.</li> <li>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.</li></ol> </li> <li> In the Visual Workbench, on the Options menu, click Project to bring up the Project Options dialog box.

<ol style="list-style-type: lower-alpha;"> <li>Click the Compiler button to bring up the C/C++ Compiler Options dialog box.</li> <li>Select Memory Model under Category. Then change the memory model from Medium to Large, and make sure SS=DS is selected.</li> <li>Select Preprocessor under Category, and add the _AFXDLL preprocessor identifier to "Symbols and Macros to Define."</li> <li>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.</li> <li>Click OK to save the settings, and exit the dialog box.</li></ol>

In general, the compiler switches you must have when building the AFXDLL's calling projects are: <pre class="CODESAMP">  /AL /D "_AFXDLL" /GA /GEf Add any other compiler switches used by the Visual Workbench. </li> <li> 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: <pre class="CODESAMP">  mfc250(d).lib afxdll_name.lib oldnames.lib libw.lib llibcew.lib </li> <li>Build the application.</li></ol>