CallingConvention Enumeração
Definição
Especifica a convenção de chamada necessária para chamar os métodos implementados em código não gerenciado.Specifies the calling convention required to call methods implemented in unmanaged code.
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
- Herança
- Atributos
Campos
| Cdecl | 2 | O chamador limpa a pilha.The caller cleans the stack. Isso permite chamar funções com |
| FastCall | 5 | Não há suporte para essa convenção de chamada.This calling convention is not supported. |
| StdCall | 3 | O computador chamado limpa a pilha.The callee cleans the stack. |
| ThisCall | 4 | O primeiro parâmetro é o ponteiro |
| Winapi | 1 | Este membro não é realmente uma convenção de chamada, mas usa a convenção de chamada da plataforma padrão.This member is not actually a calling convention, but instead uses the default platform calling convention. Por exemplo, no Windows x86, o padrão é StdCall e no Linux x86, é Cdecl.For example, on Windows x86 the default is StdCall and on Linux x86 it is Cdecl. |
Exemplos
O exemplo a seguir demonstra como aplicar a Cdecl Convenção de chamada, que você deve usar porque a pilha é limpa pelo chamador.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
Comentários
Sempre use a CallingConvention enumeração em vez da CALLCONV enumeração para especificar uma Convenção de chamada em código gerenciado.Always use the CallingConvention enumeration rather than the CALLCONV enumeration to specify a calling convention in managed code. A última opção existe apenas para fins de definições COM.The latter exists only for the sake of COM definitions. A CallingConvention enumeração é usada por DllImportAttribute e várias classes no System.Reflection.Emit para emitir dinamicamente assinaturas de invocação de plataforma.The CallingConvention enumeration is used by DllImportAttribute and several classes in System.Reflection.Emit to dynamically emit platform invoke signatures.