CallingConvention CallingConvention CallingConvention CallingConvention Enum

定义

指定调用在非托管代码中实现的方法所需的调用约定。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
继承
CallingConventionCallingConventionCallingConventionCallingConvention
属性

字段

Cdecl Cdecl Cdecl 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 FastCall FastCall FastCall 5

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

StdCall StdCall StdCall StdCall 3

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

ThisCall ThisCall ThisCall 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 Winapi Winapi 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;
public ref class LibWrap
{
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()
{
   LibWrap::printf( "\nPrint params: %i %f", 99, 99.99 );
   LibWrap::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
Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices

Friend Class LibWrap
    ' 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 'LibWrap

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

注解

始终使用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. CallingConvention枚举由DllImportAttribute中的多个类和System.Reflection.Emit动态发出平台调用签名。The CallingConvention enumeration is used by DllImportAttribute and several classes in System.Reflection.Emit to dynamically emit platform invoke signatures.

适用于

另请参阅