C# 中非托管 DLL 的调用约定

Calling convention for an unmanaged DLL in C#

我用 C++ 创建了一个 DLL,并用 [DllImport] 在 C# 中加载。

  1. 在不调用约定的情况下导出时 __cdecl 在 C++ 中是默认值吗?

  2. 函数__cdecl可以使用CallingConvention.StdCall或CallingConvention.Winapi吗? (它在我的测试中有效,我不使用 'Variable Arguments')

Is __cdecl default in C++ when exporting without calling convention?

是的。

Is it okay to use CallingConvention.StdCall or CallingConvention.Winapi with the function __cdecl?

没有。使用 CallingConvention.Cdecl。使用错误的调用约定可能对某些函数有效,但这是错误的,在某些时候你会被抓到。