CallingConvention Enumerazione

Definizione

Specifica la convenzione di chiamata necessaria per chiamare i metodi implementati nel codice non gestito.

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
Ereditarietà
CallingConvention
Attributi

Campi

Cdecl 2

Il chiamante esegue la pulizia dello stack. Questa operazione attiva funzioni chiamanti con varargs, pertanto può essere utilizzata per metodi che accettano un numero variabile di parametri, ad esempio Printf.

FastCall 5

Questa convenzione di chiamata non è supportata.

StdCall 3

Il chiamato esegue la pulizia dello stack.

ThisCall 4

Il primo parametro è il puntatore this ed è archiviato in ECX del Registro di sistema. Altri parametri vengono inseriti nello stack. Questa convenzione di chiamata viene utilizzata per chiamare metodi su classi esportate da una DLL non gestita.

Winapi 1

Questo membro non è in realtà una convenzione di chiamata, ma usa invece la convenzione di chiamata della piattaforma predefinita.

Esempio

Nell'esempio seguente viene illustrato come applicare la Cdecl convenzione di chiamata, che è necessario usare perché lo stack viene pulito dal chiamante.

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

Commenti

Usare sempre l'enumerazione CallingConvention anziché l'enumerazione CALLCONV per specificare una convenzione di chiamata nel codice gestito. Quest'ultimo esiste solo per le definizioni COM. L'enumerazione CallingConvention viene usata da DllImportAttribute e da diverse classi in System.Reflection.Emit per generare in modo dinamico le firme di platform invoke.

Si applica a

Vedi anche