CallingConvention Wyliczenie

Definicja

Określa konwencję wywoływania wymaganą do wywoływania metod zaimplementowanych w kodzie niezarządzanego.

public enum class CallingConvention
public enum CallingConvention
[System.Serializable]
public enum CallingConvention
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum CallingConvention
type CallingConvention = 
[<System.Serializable>]
type CallingConvention = 
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type CallingConvention = 
Public Enum CallingConvention
Dziedziczenie
CallingConvention
Atrybuty

Pola

Cdecl 2

Obiekt wywołujący czyści stos. Umożliwia to wywoływanie funkcji za pomocą varargspolecenia , co sprawia, że jest to odpowiednie dla metod, które akceptują zmienną liczbę parametrów, takich jak Printf.

FastCall 5

Ta konwencja wywoływania nie jest obsługiwana.

StdCall 3

Obiekt wywoływany czyści stos.

ThisCall 4

Pierwszy parametr jest wskaźnikiem this i jest przechowywany w rejestrze ECX. Inne parametry są wypychane na stos. Ta konwencja wywoływania służy do wywoływania metod dla klas wyeksportowanych z niezarządzanej biblioteki DLL.

Winapi 1

Ten element członkowski nie jest w rzeczywistości konwencją wywoływania, ale zamiast tego używa domyślnej konwencji wywoływania platformy.

Przykłady

W poniższym przykładzie pokazano, jak zastosować konwencję Cdecl wywoływania, której należy użyć, ponieważ stos jest czyszczony przez obiekt wywołujący.

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

Uwagi

Zawsze używaj CallingConvention wyliczenia, a nie CALLCONV wyliczenia, aby określić konwencję wywoływania w kodzie zarządzanym. Ten ostatni istnieje tylko ze względu na definicje COM. Wyliczenie CallingConvention jest używane przez DllImportAttribute kilka klas w System.Reflection.Emit programie do dynamicznego emitowania podpisów przez platformę.

Dotyczy

Zobacz też