Export Functions from a DLL
Each function that you want to export from the DLL must be an
external function, either by default or by being qualified with
the extern keyword. Otherwise, the linker will not find your
function references and will generate errors.
There are several ways to export the functions in a DLL so
that they are available to other programs:
- Using the _Export keyword in the source files
- With this method, you can only export the functions by
name.You cannot control which ordinal is assigned to a
particular function. This is the easiest method for
exporting functions, but it can cause problems if other
programs that use the DLL depend on a particular set of
ordinals. If the DLL has to be updated, the compiler may
assign different ordinals to the exported functions.
Using
_declspec(dllexport) in the source files
- _declspec(dllexport) is used in the same way as the
_Export keyword. As with _Export, you can only export
functions by name, and you cannot control which ordinal
is assigned to a function.
-
- Using #pragma export in the source files
- With this method, you can only export the functions by
name, but you have the option of choosing the ordinal for
a function yourself or letting the compiler choose it for
you. With this method, you can specify the assignment of
ordinals to exported functions. When you update a DLL,
you can keep these assignments. This means that programs
that use functions from this DLL will not have to be
updated when the DLL is updated.
Note that even if you use #pragma export or _Export to
export your function, there are some situations where you may
still need to create a module definition file.
- Using a Module Definition file.
- This method offers the most flexibility: functions can be
exported with or without a name. You have the option of
choosing the ordinal for a function yourself or allowing
the compiler to choose it.
-
- The advantage of using a .DEF file to export functions is
that changing the DLL will not affect other programs that
use functions in the DLL. However, it can be difficult to
write and maintain C++ .DEF files because you must use
the mangled names of the functions that you want to
export.
With
a module definition file, the load time can be greater
for the code that uses the DLL if you have chosen to keep
names resident in memory.

Create a Module Definition File

Examples Using _Export, #pragma
export and _declspec