Compartir a través de


Exporting from a DLL Using __declspec(dllexport)

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at Exporting from a DLL Using __declspec(dllexport).

Microsoft introduced __export in the 16-bit compiler version of Visual C++ to allow the compiler to generate the export names automatically and place them in a .lib file. This .lib file can then be used just like a static .lib to link with a DLL.

In newer compiler versions, you can export data, functions, classes, or class member functions from a DLL using the __declspec(dllexport) keyword. __declspec(dllexport) adds the export directive to the object file so you do not need to use a .def file.

This convenience is most apparent when trying to export decorated C++ function names. Because there is no standard specification for name decoration, the name of an exported function might change between compiler versions. If you use __declspec(dllexport), recompiling the DLL and dependent .exe files is necessary only to account for any naming convention changes.

Many export directives, such as ordinals, NONAME, and PRIVATE, can be made only in a .def file, and there is no way to specify these attributes without a .def file. However, using __declspec(dllexport) in addition to using a .def file does not cause build errors.

To export functions, the __declspec(dllexport) keyword must appear to the left of the calling-convention keyword, if a keyword is specified. For example:

__declspec(dllexport) void __cdecl Function1(void);  

To export all of the public data members and member functions in a class, the keyword must appear to the left of the class name as follows:

class __declspec(dllexport) CExampleExport : public CObject  
{ ... class definition ... };  

Note

__declspec(dllexport) cannot be applied to a function with the __clrcall calling convention.

When building your DLL, you typically create a header file that contains the function prototypes and/or classes you are exporting and add __declspec(dllexport) to the declarations in the header file. To make your code more readable, define a macro for __declspec(dllexport) and use the macro with each symbol you are exporting:

#define DllExport   __declspec( dllexport )   

__declspec(dllexport) stores function names in the DLL's export table. If you want to optimize the table's size, see Exporting Functions from a DLL by Ordinal Rather Than by Name.

Note

When porting DLL source code from Win16 to Win32, replace each instance of __export with __declspec(dllexport).

As a reference, search through the Win32 Winbase.h header file. It contains examples of __declspec(dllimport) usage.

What do you want to do?

What do you want to know more about?

See Also

Exporting from a DLL