I am writing a DLL for a third party application. The main software engineer mentions that the application uses the __cdecl (/Gd) calling convention. That I need to make sure that I use that.
Additionally, the third party has provided to me a C++ DLL skeleton which exports the functions as follows:
#ifdef _EXPORTING
#define DECLSPEC __declspec(dllexport)
#else
#define DECLSPEC __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C" {
#endif
DECLSPEC int ICD_Create(char* id);
....
....
I am kind of confused. Why the functions are being exported using __declspec convention instead of __cdedl?? does __declspec support _cdecl?
Thanks.
__declspec()
and __cdecl
are addressing two different aspects of calling the function, and you can (and in this case by the sounds of it you should) use both.
__cdecl
specifies the calling convention, which specifies how parameters are passed to the function via the stack, and, very importantly, who cleans up the stack afterwards (in the case of __cdecl
it is the caller who tidies up).
__declspec(dllimport/dllexport)
is used to simplify exporting function definitions from a DLL: you don't need to use them, but the other ways of exporting functions are quite klunky.
It is probably a good idea to explicitly use __cdecl
rather than doing what the code snippet you have has done, which is rely on the compiler to choose the calling convention. You can override this with command-line switches I think, and I believe the default is different depending on whether you are compiling C or C++ code, so more explicit is (as usual) better.