|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Importing and Exporting
You can import public symbols into an application or export functions from a DLL using two methods:
- Use a module definition (.DEF) file when building the DLL.
- Use the keywords __declspec(dllimport) or __declspec(dllexport) in a function definition in the main application.
Using a .DEF file
A module-definition (.DEF) file is a text file containing one or more module statements that describe various attributes of a DLL. If you do not use __declspec(dllimport) or __declspec(dllexport) to export a DLL's functions, the DLL requires a .DEF file.
The 32-bit edition of Visual C++ uses __declspec(dllimport) and __declspec(dllexport) to replace the __export keyword previously used in 16-bit versions of Visual C++.
You do not need to use __declspec(dllimport) for your code to compile correctly, but doing so allows the compiler to generate better code. The compiler is able to generate better code because it knows for sure whether a function exists in a DLL or not, so the compiler can produce code that skips a level of indirection that would normally be present in a function call that crossed a DLL boundary. However, you must use __declspec(dllimport) in order to import variables used in a DLL.
With the proper .DEF file EXPORTS section, __declspec(dllexport) is not required. __declspec(dllexport) was added to provide an easy way to export functions from an .EXE or .DLL without using a .DEF file.
The Win32 Portable Executable format is designed to minimize the number of pages that must be touched to fix imports. To do this, it places all of the import addresses for any program in one place called the Import Address Table. This allows the loader to modify only one or two pages when accessing these imports.