CallingConvention Enumeration

Definition

Gibt die Aufrufkonvention an, die zum Aufrufen von Methoden erforderlich ist, die in nicht verwaltetem Code implementiert sind.

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
Vererbung
CallingConvention
Attribute

Felder

Cdecl 2

Der Aufrufer entleert den Stapel. Dies aktiviert Aufruffunktionen mit varargs, wodurch die Verwendung für Methoden ermöglicht wird, die eine variable Anzahl von Parametern akzeptieren, beispielsweise Printf.

FastCall 5

Diese Aufruffunktion wird nicht unterstützt.

StdCall 3

Der Aufgerufene entleert den Stapel.

ThisCall 4

Der erste Parameter ist der this-Zeiger, der im Register ECX gespeichert wird. Weitere Parameter werden in den Stapel verschoben. Diese Aufrufkonvention wird zum Aufrufen von Methoden für Klassen verwendet, die aus einer nicht verwalteten DLL exportiert wurden.

Winapi 1

Dieser Member ist eigentlich keine Aufrufkonvention, sondern verwendet stattdessen die Standardkonvention für Plattformaufrufe.

Beispiele

Im folgenden Beispiel wird veranschaulicht, wie Sie die Cdecl Aufrufkonvention anwenden, die Sie verwenden müssen, da der Stapel vom Aufrufer bereinigt wird.

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

Hinweise

Verwenden Sie immer die CallingConvention -Enumeration anstelle der CALLCONV -Enumeration, um eine Aufrufkonvention in verwaltetem Code anzugeben. Letzteres ist nur für COM-Definitionen vorhanden. Die CallingConvention -Enumeration wird von DllImportAttribute und mehreren Klassen in System.Reflection.Emit verwendet, um Plattformaufrufsignaturen dynamisch ausgibt.

Gilt für:

Weitere Informationen