The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
We recommend using Visual Studio 2017

Importing into an Application Using __declspec(dllimport)


The new home for Visual Studio documentation is Visual Studio 2017 Documentation on

The latest version of this topic can be found at Importing into an Application Using __declspec(dllimport).

A program that uses public symbols defined by a DLL is said to import them. When you create header files for applications that use your DLLs to build with, use __declspec(dllimport) on the declarations of the public symbols. The keyword __declspec(dllimport) works whether you export with .def files or with the __declspec(dllexport) keyword.

To make your code more readable, define a macro for __declspec(dllimport) and then use the macro to declare each imported symbol:

#define DllImport   __declspec( dllimport )  
DllImport int  j;  
DllImport void func();  

Using __declspec(dllimport) is optional on function declarations, but the compiler produces more efficient code if you use this keyword. However, you must use __declspec(dllimport) for the importing executable to access the DLL's public data symbols and objects. Note that the users of your DLL still need to link with an import library.

You can use the same header file for both the DLL and the client application. To do this, use a special preprocessor symbol that indicates whether you are building the DLL or building the client application. For example:

#ifdef _EXPORTING  
   #define CLASS_DECLSPEC    __declspec(dllexport)  
   #define CLASS_DECLSPEC    __declspec(dllimport)  
class CLASS_DECLSPEC CExampleA : public CObject  
{ ... class definition ... };  

Importing into an Application