Importing into an Application Using __declspec(dllimport)
This page is specific to:.NET Framework Version:1.12.03.54.0
Visual C++
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)
#else
   #define CLASS_DECLSPEC    __declspec(dllimport)
#endif

class CLASS_DECLSPEC CExampleA : public CObject
{ ... class definition ... };
What do you want to do?

What do you want to know more about?

See Also

Concepts

Community Content

Unbelievable
Added by:LasGafas
Unbelievable, that in 2008 MS still forces us to pollute the code with completely unnecessary things like decspec(export). A simple compiler flag could help. Like /export_all.

Why isn't that present? The only reason I can think of is ... vendor lock-in.
© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View