CallingConvention 枚举

定义

指定调用在非托管代码中实现的方法所需的调用约定。

C#
public enum CallingConvention
C#
[System.Serializable]
public enum CallingConvention
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum CallingConvention
继承
CallingConvention
属性

字段

名称 说明
Cdecl 2

调用方清理堆栈。 这使你能够调用具有 varargs 的函数(如 Printf),使之可用于接受可变数目的参数的方法。

FastCall 5

不支持此调用约定。

StdCall 3

被调用方清理堆栈。

ThisCall 4

第一个参数是 this 指针,它存储在寄存器 ECX 中。 其他参数被推送到堆栈上。 此调用约定用于对从非托管 DLL 导出的类调用方法。

Winapi 1

此成员实际上不是调用约定,而是使用 默认的平台调用约定

示例

以下示例演示如何应用 Cdecl 调用约定,由于堆栈由调用方清理,因此必须使用该约定。

C#
using System;
using System.Runtime.InteropServices;

internal static class NativeMethods
{
    // C# doesn't support varargs so all arguments must be explicitly defined.
    // CallingConvention.Cdecl must be used since the stack is
    // cleaned up by the caller.

    // int printf( const char *format [, argument]... )

    [DllImport("msvcrt.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
    internal static extern int printf(String format, int i, double d);

    [DllImport("msvcrt.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
    internal static extern int printf(String format, int i, String s);
}

public class App
{
    public static void Main()
    {
        NativeMethods.printf("\nPrint params: %i %f", 99, 99.99);
        NativeMethods.printf("\nPrint params: %i %s", 99, "abcd");
    }
}

注解

始终使用 CallingConvention 枚举而不是 CALLCONV 枚举在托管代码中指定调用约定。 后者仅用于 COM 定义。 枚举 CallingConventionDllImportAttribute 和 中的 System.Reflection.Emit 多个类用于动态发出平台调用签名。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另请参阅