CallingConvention 枚举

定义

指定调用在非托管代码中实现的方法所需的调用约定。Specifies the calling convention required to call methods implemented in unmanaged code.

public enum class CallingConvention
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum CallingConvention
type CallingConvention = 
Public Enum CallingConvention
继承
CallingConvention
属性

字段

Cdecl 2

调用方清理堆栈。The caller cleans the stack. 这使你能够调用具有 varargs 的函数(如 Printf),使之可用于接受可变数目的参数的方法。This enables calling functions with varargs, which makes it appropriate to use for methods that accept a variable number of parameters, such as Printf.

FastCall 5

不支持此调用约定。This calling convention is not supported.

StdCall 3

被调用方清理堆栈。The callee cleans the stack. 这是使用平台 invoke 调用非托管函数的默认约定。This is the default convention for calling unmanaged functions with platform invoke.

ThisCall 4

第一个参数是 this 指针,它存储在寄存器 ECX 中。The first parameter is the this pointer and is stored in register ECX. 其他参数被推送到堆栈上。Other parameters are pushed on the stack. 此调用约定用于对从非托管 DLL 导出的类调用方法。This calling convention is used to call methods on classes exported from an unmanaged DLL.

Winapi 1

此成员实际上不是调用约定,而是使用了默认平台调用约定。This member is not actually a calling convention, but instead uses the default platform calling convention. 例如,在 Windows 上默认为 StdCall,在 Windows CE.NET 上默认为 CdeclFor example, on Windows the default is StdCall and on Windows CE.NET it is Cdecl.

示例

下面的示例演示如何应用Cdecl必须使用的调用约定, 因为堆栈由调用方清理。The following example demonstrates how to apply the Cdecl calling convention, which you must use because the stack is cleaned up by the caller.

using namespace System;
using namespace System::Runtime::InteropServices;

private ref class NativeMethods
{
public:

    // 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)]
    static int printf(String^ format, int i, double d);

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

int main()
{
    NativeMethods::printf("\nPrint params: %i %f", 99, 99.99);
    NativeMethods::printf("\nPrint params: %i %s", 99, "abcd");
}
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");
    }
}
Imports System.Runtime.InteropServices

Friend Class NativeMethods
    ' Visual Basic does not 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)>
    Friend Overloads Shared Function printf(
        ByVal format As String, ByVal i As Integer, ByVal d As Double) As Integer
    End Function

    <DllImport("msvcrt.dll", CharSet:=CharSet.Unicode, CallingConvention:=CallingConvention.Cdecl)>
    Friend Overloads Shared Function printf(
        ByVal format As String, ByVal i As Integer, ByVal s As String) As Integer
    End Function
End Class

Public Class App
    Public Shared Sub Main()
        NativeMethods.printf(ControlChars.CrLf + "Print params: %i %f", 99, 99.99)
        NativeMethods.printf(ControlChars.CrLf + "Print params: %i %s", 99, "abcd")
    End Sub
End Class

注解

始终使用CallingConvention枚举而不是CALLCONV枚举来指定托管代码中的调用约定。Always use the CallingConvention enumeration rather than the CALLCONV enumeration to specify a calling convention in managed code. 后者仅用于 COM 定义。The latter exists only for the sake of COM definitions. 枚举由使用和中System.Reflection.Emit的多个类来动态发出平台调用签名。 DllImportAttribute CallingConventionThe CallingConvention enumeration is used by DllImportAttribute and several classes in System.Reflection.Emit to dynamically emit platform invoke signatures.

适用于

另请参阅