There are three ways an application can import DLL functions:
Import implicitly at link time
Import explicitly at link time
Import dynamically at run time
In each case, dynamic-link information contained in the application identifies the name of the library and the function name or function's ordinal entry value. The implicit import is the most commonly used method, and it is discussed first.
An implicit import is performed by listing the import library for the DLL on the linker command line for an application. The import library is automatically created using the IMPLIBW utility. For more information, see “Creating the Project” on page 544.
The Windows Graphical Development system contains a set of libraries to allow linking to Windows DLLs. The following list names these files and the purpose of each.
CDLLCEW.LIB
Start-up code for Windows DLLs, C run-time library routines, and emulated math packages for compact-model DLLs.
CLIBCEW.LIB
Start-up code for Windows applications, C run-time library routines, and emulated math packages for compact-model applications.
LDLLCEW.LIB
Start-up code for Windows DLLs, C run-time library routines, and emulated math packages for large-model DLLs.
LIBW.LIB
Import information for USER.EXE, KERNEL.EXE, and GDI.EXE.
LLIBCEW.LIB
Start-up code for Windows applications, C run-time library routines, and emulated math packages for large-model applications.
MDLLCEW.LIB
Start-up code for Windows DLLs, C run-time library routines, and emulated math packages for medium-model DLLs.
MLIBCEW.LIB
Start-up code for Windows applications, C run-time library routines, and emulated math packages for medium-model applications.
SDLLCEW.LIB
Start-up code for Windows DLLs, C run-time library routines, and emulated math packages for small-model DLLs.
SLIBCEW.LIB
Start-up code for Windows applications, C run-time library routines, and emulated math packages for small-model applications.
WIN87EM.LIB
Import information for Windows' floating-point DLL.
Like an implicit import, an explicit import is perfomed at link time. An explicit import is performed by listing each routine in the IMPORTS section of the application's module-definition file. In the following example, there are three parts: the imported routine name (MinRoutine), the DLL name (MinDLL), and the ordinal entry value of the function in the library (1).
IMPORTS
MinRoutine=MinDLL.1
Due to performance and size considerations, it is strongly advised that application developers define ordinal entry values for all exported DLL routines. However, if you do not assign an ordinal entry value, you perform the explicit import as shown in the following example:
IMPORTS
MinDLL.MinRoutine
In dynamic run-time imports, the application must first load the library and explicitly ask for the address of the desired function. Once this is done, the application can call the function. In the following example, an application links dynamically with the CreateInfo function in the Windows library INFO.DLL.
HANDLE hLibrary;
FARPROC lpFunc;
hLibrary = LoadLibrary ("INFO.DLL");
if (hLibrary >= 32)
{
lpFunc = GetProcAddress (hLibrary, “CreateInfo”);
if (lpFunc != (FARPROC) NULL)
(*lpFunc) ((LPSTR) Buffer, 512);
FreeLibrary (hLibrary);
}
In this example, the LoadLibrary function loads the desired Windows library and returns a module handle to the library. The GetProcAddress function retrieves the address of the CreateInfo function by using the function's name, CreateInfo. The function address can then be used to call the function. The following statement is an indirect function call that passes two arguments (Buffer and the integer 512) to the function:
*(lpFunc) ((LPSTR) Buffer, 512);
Finally, the FreeLibrary function decrements the library's use count. When the use count becomes zero (that is, no application is using the library), the Windows library is removed from memory.
Slightly better performance would be obtained if the CreateInfo function had an ordinal value assigned in the library's module-definition file. The following is an example of such a .DEF file entry:
EXPORTS
CreateInfo @27
This statement defines the ordinal value of CreateInfo as 27. Using this value involves changing the call to GetProcAddress to the following:
GetProcAddress (hLibrary, MAKEINTRESOURCE(27));