I implied earlier that in writing your own library module, you're adding an extension to Windows—an extension that serves you in a manner similar to that of the standard KERNEL, USER, and GDI library modules. So, you ask, why do I have to list all the names of imported functions from my own dynamic libraries when I don't have to specifically import the KERNEL, USER, and GDI functions that I use? Well, in the early days of Windows programming (long before the introduction of the product), programmers had to do precisely that. They ended up with module definition files that looked like this:
IMPORTS USER.RegisterClass
USER.CreateWindow
[etc, etc, etc.]
This process was simplified greatly by the use of ”import libraries.“ Import libraries are much like object libraries in that LINK uses them to resolve function calls within a program. But the import library contains no (or very little) code, only a reference that reconciles the function name you use in your program with the library module containing this function and the actual function name. This is exactly what you do in the IMPORTS section of a .DEF file. An import library for STRLIB would allow LINK to know that a function call to AddString is really an imported function from STRLIB called AddString or an imported function from STRLIB with an ordinal number of 1.
When you link a Windows program or dynamic link library, the LIBW.LIB import library reconciles all the normal Windows functions you use in the program (mostly from KERNEL, USER, and GDI) and the ordinal numbers. That's why this import library must be specified in the library field of the LINK command line when linking. Think of it this way: LINK has to resolve all calls that a program makes to external functions. It can do this in one of three ways: extract the function itself from an object library, get a reference to a library module name and function name (or ordinal) from an import library, or get a library module name and function name (or ordinal) from the IMPORTS section of the module definition file.
You can create an import library for a dynamic library module by running the IMPLIB program included with the Windows Software Development Kit. The syntax is:
IMPLIB libname.LIB libname.DEF
IMPLIB looks only at the EXPORTS section of the module definition file. It creates a file with the extension .LIB. After the import library is created, you can add normal object modules to the .LIB file using the LIB.EXE program included with the Microsoft C Compiler.
Figure 19-3 shows a revised make file and module definition file for STRLIB; Figure 19-4 on the following page shows a revised make file and module definition file for STRPROG. The new STRLIB make file creates an import library called STRLIB.LIB. In the STRPROG make file, this import library must be specified in the library field of the LINK command line. The new STRPROG.DEF file requires no IMPORTS section.
STRLIB.MAK
#----------------------
# STRLIB.MAK make file
#----------------------
strlib.dll : strlib.obj strlib.def
link strlib libentry, strlib.dll /align:16, NUL, /nod sdllcew libw, strlib
rc strlib.dll
implib strlib.lib strlib.def
strlib.obj : strlib.c
cl -c -ASw -Gsw -Ow -W2 -Zp strlib.c
STRLIB.DEF
;-----------------------------------
; STRLIB.DEF module definition file
;-----------------------------------
LIBRARY STRLIB
DESCRIPTION 'DLL for STRPROG Program (c) Charles Petzold, 1990'
EXETYPE WINDOWS
CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE SINGLE
HEAPSIZE 1024
EXPORTS AddString @1
DeleteString @2
GetStrings @3
STRPROG.MAK
#-----------------------
# STRPROG.MAK make file
#-----------------------
strprog.exe : strprog.obj strprog.res strprog.def
link strprog, /align:16, NUL, /nod slibcew libw strlib, strprog
rc strprog.res
strprog.obj : strprog.c strprog.h
cl -c -Gsw -Ow -W2 -Zp strprog.c
strprog.res : strprog.rc strprog.h
rc -r strprog.rc
STRPROG.DEF
;------------------------------------
; STRPROG.DEF module definition file
;------------------------------------
NAME STRPROG
DESCRIPTION 'Program using STRLIB DLL (c) Charles Petzold, 1990'
EXETYPE WINDOWS
STUB 'WINSTUB.EXE'
CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE MULTIPLE
HEAPSIZE 1024
STACKSIZE 8192
EXPORTS WndProc
DlgProc
GetStrCallBack
EnumCallBack