A DLL is created from one or more source code files that define its functions, and from a module definition file (.def file) that lists the functions in the DLL that can be called by an application (or by another DLL). An application or DLL that wants to call a function in a DLL can link to an import library which provides information used to locate the DLL when the application is started up.
The source code files for a DLL can be written in any programming language. The source code files are compiled to produce object modules that are linked to create a .dll file containing the executable code of a dynamic link library. In addition to the code required by the DLL's exported functions, the source code files must include a LibMain function. This function is the entrypoint to the DLL, and it is called when a process attaches to and detaches from a DLL. While a process is attached to the DLL, the DLL's LibMain function is also called whenever a thread of that process starts up or terminates. LibMain does not have to do anything, but it can be useful for performing per process and per thread initialization and cleanup.
The module definition (.def) file for a DLL contains an EXPORTS statement that lists the names and ordinal values of the exported functions in the DLL that can be called from other modules The module definition file may also contain additional statements that specify other attributes of the executable file. For example, you could use the LIBRARY statement to specify a name for the DLL that can be used to get a handle to the library for run-time loading; or you could use the DATA or SECTIONS statements to specify attributes for the DLL's data to determine whether the data is shared by multiple processes. A DLL's module definition file is used to create both an import library (.lib file) and an export (.exp) file. The export file is used by the linker to build the .dll file; and the import library is used by the linker to build any executable module (.exe or .dll) that uses the DLL.
You can also create a module definition file for your application. An IMPORTS statement in an application's module definition file is used to provide the linker with a list of each of the DLLs and the functions within each DLL that your application uses. The linker uses this information to resolve the external references in the application's code. The linker inserts the data into the application's executable module, and the application uses it when it starts up to locate the external DLL functions.
An alternative to using a module definition file IMPORTS statement is to link with an import library when creating the application's executable (.exe) file. As with the IMPORTS statement, the linker uses the information in an import library to resolve external references and to provide the application with the information that it will need when it starts up. The advantage of an import library is that it relieves you of the need to specify a list of all the DLLs and imported functions used by your application. Like a conventional library, an import library combines information about a number of functions in one or more modules; but unlike a conventional library, an import library does not contain any object modules. Instead, an import library contains information about one or more DLL modules and their functions. The linker extracts from the import library the information about those functions referenced in your application's code.
You use import libraries every time you compile and link a program that uses the Win32 API. All the Win32 functions are implemented in dynamic-link libraries with import libraries that tell the linker where to find each function.