Windows 上的 __cdecl 或 __stdcall?

发布于 2024-11-17 14:13:00 字数 1302 浏览 6 评论 0 原文

我目前正在为 Windows 开发一个 C++ 库,它将作为 DLL 分发。我的目标是最大化二进制互操作性;更准确地说,我的 DLL 中的函数必须可以从使用多个版本的 MSVC++ 和 MinGW 编译的代码中使用,而无需重新编译 DLL。但是,我对哪种调用约定最好感到困惑,cdeclstdcall

有时我会听到诸如“C 调用约定是唯一保证跨编译器相同的调用约定”之类的说法,这与“cdecl 的解释存在一些变化,特别是在如何返回值方面”。这似乎并没有阻止某些库开发人员(例如 libsndfile)使用 C 调用约定在他们分发的 DLL 中,没有任何明显的问题。

另一方面,stdcall 调用约定似乎定义良好。据我所知,所有 Windows 编译器基本上都必须遵循它,因为它是 Win32 和 COM 使用的约定。这是基于这样的假设:没有 Win32/COM 支持的 Windows 编译器不会很有用。论坛上发布的许多代码片段都将函数声明为 stdcall,但我似乎找不到一篇文章可以清楚地解释原因

那里有太多相互冲突的信息,我运行的每次搜索都会给我不同的答案,这并不能真正帮助我在两者之间做出决定。我正在寻找一个清晰、详细、有争议的解释,解释为什么我应该选择一个而不是另一个(或者为什么两者是等效的)。

请注意,这个问题不仅适用于“经典”函数,还适用于虚拟成员函数调用,因为大多数客户端代码将通过“接口”、纯虚拟类(以下描述的模式,例如 此处那里)。

I'm currently developing a C++ library for Windows which will be distributed as a DLL. My goal is to maximize binary interoperability; more precisely, the functions in my DLL must be usable from code compiled with multiple versions of MSVC++ and MinGW without having to recompile the DLL. However, I'm confused about which calling convention is best, cdecl or stdcall.

Sometimes I hear statements like "the C calling convention is the only one guaranteed to be the same accross compilers", which contrasts with statements like "There are some variations in the interpretation of cdecl, particularly in how to return values". This doesn't seem to stop certain library developers (like libsndfile) to use the C calling convention in the DLLs they distribute, without any visible problems.

On the other hand, the stdcall calling convention seems to be well-defined. From what I've been told, all Windows compilers are basically required to follow it because it's the convention used for Win32 and COM. This is based on the assumption that a Windows compiler without Win32/COM support would not be very useful. A lot of code snippets posted on forums declare functions as stdcall but I can't seem to find one single post which clearly explains why.

There's too much conflicting information out there, and every search I run gives me different answers which doesn't really help me decide between the two. I'm searching for a clear, detailed, argumented explanation as to why I should choose one over the other (or why the two are equivalent).

Note that this question not only applies to "classic" functions, but also to virtual member function calls, since most client code will interface with my DLL through "interfaces", pure virtual classes (following patterns described e.g. here and there).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那伤。 2024-11-24 14:13:00

我刚刚做了一些实际测试(使用 MSVC++ 和 MinGW 编译 DLL 和应用程序,然后混合它们)。看来,我使用 cdecl 调用约定得到了更好的结果。

更具体地说:stdcall 的问题是 MSVC++ 会破坏 DLL 导出表中的名称,即使使用 extern "C" 也是如此。例如 foo 变为 _foo@4。这只发生在使用 __declspec(dllexport) 时,而不是使用 DEF 文件时;然而,DEF 文件在我看来维护起来很麻烦,我不想使用它们。

MSVC++ 名称修饰会带来两个问题:

  • 使用 GetProcAddress DLL 上变得稍微复杂一些;
  • 默认情况下,MinGW 不会在修饰名称前添加取消划线(例如,MinGW 将使用 foo@4 而不是 _foo@4),这会使链接变得复杂。此外,它还引入了出现与“下划线版本”不兼容的 DLL 和应用程序的“非下划线版本”的风险。

我尝试过 cdecl 约定:MSVC++ 和 MinGW 之间的互操作性完美地工作,开箱即用,并且名称在 DLL 导出表中保持未修饰。它甚至适用于虚拟方法。

由于这些原因,cdecl 对我来说显然是赢家。

I just did some real-world testing (compiling DLLs and applications with MSVC++ and MinGW, then mixing them). As it appears, I had better results with the cdecl calling convention.

More specifically: the problem with stdcall is that MSVC++ mangles names in the DLL export table, even when using extern "C". For example foo becomes _foo@4. This only happens when using __declspec(dllexport), not when using a DEF file; however, DEF files are a maintenance hassle in my opinion, and I don't want to use them.

The MSVC++ name mangling poses two problems:

  • Using GetProcAddress on the DLL becomes slightly more complicated;
  • MinGW by default doesn't prepend an undescore to the decorated names (e.g. MinGW will use foo@4 instead of _foo@4), which complicates linking. Also, it introduces the risk of seeing "non-underscore versions" of DLLs and applications pop up in the wild which are incompatible with the "underscore versions".

I've tried the cdecl convention: interoperability between MSVC++ and MinGW works perfectly, out-of-the-box, and names stay undecorated in the DLL export table. It even works for virtual methods.

For these reasons, cdecl is a clear winner for me.

一梦浮鱼 2024-11-24 14:13:00

两种调用约定的最大区别在于“_cdecl”将函数调用后平衡堆栈的负担置于调用者身上,这允许函数具有可变数量的参数。 “__stdcall”约定本质上“更简单”,但在这方面不太灵活。

另外,我相信托管语言默认使用 stdcall 约定,因此如果您使用 cdecl,则任何使用 P/Invoke 的人都必须明确声明调用约定。

因此,如果所有函数签名都将被静态定义,我可能会倾向于 stdcall,如果不是 cdecl。

The biggest difference in the two calling conventions is that "__cdecl" places the burden of balancing the stack after a function call on the caller, which allows for functions with variable amounts of arguments. The "__stdcall" convention is "simpler" in nature, however less flexible in this regard.

Also, I believe managed languages use stdcall convention by default, so anyone using P/Invoke on it would have to explicitly state the calling convention if you go with cdecl.

So, if all of your function signatures are going to be statically defined I would probably lean toward stdcall, if not cdecl.

野稚 2024-11-24 14:13:00

就安全性而言,__cdecl 约定“更安全”,因为需要释放堆栈的是调用者。 __stdcall 库中可能发生的情况是,开发人员可能忘记正确释放堆栈,或者攻击者可能通过破坏 DLL 的堆栈(例如通过 API 挂钩)来注入一些代码,然后不进行检查由来电者。
我没有任何 CVS 安全示例来证明我的直觉是正确的。

In terms of security, the __cdecl convention is "safer" because it is the caller that needs to deallocate the stack. What may happen in a __stdcall library is that the developer might have forgotten to deallocate the stack properly, or an attacker might inject some code by corrupting the DLL's stack (e.g. by API hooking) which is then not checked by the caller.
I don't have any CVS security examples that show my intuition is correct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文