I am trying to write a dll,this is how looks my header file:
#ifndef _DLL_H_
#define _DLL_H_
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
DLLIMPORT void HelloWorld (void);
#endif /* _DLL_H_ */
In the .cpp file I include this header file,and I try declaring a dll import procedure this way:
DLLIMPORT void HelloWorld ()
{
MessageBox (0, "Hello World from DLL!n", "Hi", MB_ICONINFORMATION);
}
But the compiler ( I have mingw32 on windows 7 64 bit) keeps giving me this error:
E:\Cpp\Sys64\main.cpp|7|error: function 'void HelloWorld()' definition is marked dllimport|
E:\Cpp\Sys64\main.cpp||In function 'void HelloWorld()':|
E:\Cpp\Sys64\main.cpp|7|warning: 'void HelloWorld()' redeclared without dllimport attribute: previous dllimport ignored|
||=== Build finished: 1 errors, 1 warnings ===|
And I don't understand why.
DllImport attribute uses the InteropServices of the CLR, which executes the call from managed code to unmanaged code. It also informs the compiler about the location of the implementation of the function used.
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.
The dllexport and dllimport storage-class attributes are Microsoft-specific extensions to the C and C++ languages. You can use them to export and import functions, data, and objects to or from a DLL.
The declspec(dllimport)
generates entries in the module import table of the module. This import table is used to resolve the referneces to the symbols at link time. At load time these references are fixed by the loader.
The declspec(dllexport)
generates entries in the DLL export table of the DLL. Further you need to implement symbols (function, variables) that are declare with it.
Since you you implement the DLL, you must define BUILDING_DLL. This could be done with #define
but this should be better set in the project settings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With