CallingConvention 列舉

定義

指定呼叫 Unmanaged 程式碼中實作之方法所需的呼叫慣例。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 的呼叫函式,就可用於接受各種數目參數的方法,如 PrintfThis 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. 這是針對用平台叫用呼叫 Unmanaged 函式的預設慣例。This is the default convention for calling unmanaged functions with platform invoke.

ThisCall 4

第一個參數為 this 指標且儲存在 register ECX 中。The first parameter is the this pointer and is stored in register ECX. 其他參數會被推入至堆疊。Other parameters are pushed on the stack. 這個呼叫慣例是用來呼叫從 Unmanaged 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 列舉,以指定 managed 程式碼中的呼叫慣例。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 和數個類別都會使用 CallingConvention 列舉,以動態方式發出平台叫用簽章。The CallingConvention enumeration is used by DllImportAttribute and several classes in System.Reflection.Emit to dynamically emit platform invoke signatures.

適用於

另請參閱