__cdecl和__declspec调用约定混乱 [英] __cdecl and __declspec calling conventions confusion
问题描述
我正在为第三方应用程序编写DLL。首席软件工程师提到该应用程序使用__cdecl(/ Gd)调用约定。
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.
此外,第三方还向我提供了一个C ++ DLL框架,该框架导出的功能如下:
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);
....
....
我有点困惑。为什么使用__declspec约定而不是__cdedl导出函数? __declspec支持_cdecl吗?
I am kind of confused. Why the functions are being exported using __declspec convention instead of __cdedl?? does __declspec support _cdecl?
谢谢。
推荐答案
__ declspec()
和 __ cdecl
解决了调用函数的两个不同方面,您可以(在这种情况下,通过
__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
指定调用约定,该约定指定如何将参数传递给函数通过堆栈,而且很重要的是,谁随后清理堆栈(在 __ cdecl
的情况下,是清理程序的调用方)。
__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)
用于简化从DLL导出函数的定义:您不需要使用它们,而另一个导出函数的方法非常笨拙。
__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.
明确使用 __ cdecl
而不是做什么可能是一个好主意。您已完成的代码段,即依靠编译器选择调用约定。您可以使用我认为的命令行开关来覆盖它,并且我相信默认值会有所不同,具体取决于您是编译C还是C ++代码,因此(像往常一样)更明确是更好的选择。
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.
这篇关于__cdecl和__declspec调用约定混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!