Enumeração CallingConvention
Observação
The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.
Especifica a convenção de chamada necessária para chamar os métodos implementados em código não gerenciado.
Namespace: System.Runtime.InteropServices
Assembly: mscorlib (em mscorlib.dll)
Sintaxe
[SerializableAttribute]
[ComVisibleAttribute(true)]
public enum CallingConvention
[SerializableAttribute]
[ComVisibleAttribute(true)]
public enum class CallingConvention
[<SerializableAttribute>]
[<ComVisibleAttribute(true)>]
type CallingConvention
<SerializableAttribute>
<ComVisibleAttribute(True)>
Public Enumeration CallingConvention
Membros
Nome do membro | Descrição | |
---|---|---|
Cdecl | O chamador limpa a pilha. Isso permite chamar funções com varargs, o que o torna apropriado para usar para métodos que aceitam um número variável de parâmetros, como Printf. |
|
FastCall | Não há suporte para essa convenção de chamada. |
|
StdCall | O computador chamado limpa a pilha. Essa é a convenção padrão para chamar funções não gerenciadas com a invocação da plataforma. |
|
ThisCall | O primeiro parâmetro é o ponteiro this e é armazenado no ECX de registro. Outros parâmetros são enviados por push na pilha. Esta convenção de chamada é usada para chamar métodos em classes exportadas de uma DLL não gerenciada. |
|
Winapi | Este membro não é realmente uma convenção de chamada, mas usa a convenção de chamada da plataforma padrão. Por exemplo, no Windows o padrão é StdCall e no Windows CE.NET é Cdecl. |
Comentários
Use sempre o CallingConvention enumeração em vez de CALLCONV enumeração para especificar uma convenção de chamada no código gerenciado. O último existe somente para definições de COM. O CallingConvention enumeração é usada pelo DllImportAttribute e várias classes em System.Reflection.Emit dinamicamente emitir assinaturas de invocação de plataforma.
Exemplos
O exemplo a seguir demonstra como aplicar o Cdecl chamando convenção, que deve ser usada porque a pilha é limpos pelo chamador.
using System;
using System.Runtime.InteropServices;
public class LibWrap
{
// 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)]
public static extern int printf(String format, int i, double d);
[DllImport("msvcrt.dll", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.Cdecl)]
public static extern int printf(String format, int i, String s);
}
public class App
{
public static void Main()
{
LibWrap.printf("\nPrint params: %i %f", 99, 99.99);
LibWrap.printf("\nPrint params: %i %s", 99, "abcd");
}
}
Imports System
Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices
Public Class LibWrap
' 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)> _
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)> _
Overloads Shared Function printf( _
ByVal format As String, ByVal i As Integer, ByVal s As String) As Integer
End Function
End Class 'LibWrap
Public Class App
Public Shared Sub Main()
LibWrap.printf(ControlChars.CrLf + "Print params: %i %f", 99, _
99.99)
LibWrap.printf(ControlChars.CrLf + "Print params: %i %s", 99, _
"abcd")
End Sub 'Main
End Class 'App
using namespace System;
using namespace System::Runtime::InteropServices;
public ref class LibWrap
{
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()
{
LibWrap::printf( "\nPrint params: %i %f", 99, 99.99 );
LibWrap::printf( "\nPrint params: %i %s", 99, "abcd" );
}
Informações de Versão
Plataforma Universal do Windows
Disponível desde 8
.NET Framework
Disponível desde 1.1
Biblioteca de Classes Portátil
Com suporte no: plataformas portáteis do .NET
Silverlight
Disponível desde 2.0
Windows Phone Silverlight
Disponível desde 7.0
Windows Phone
Disponível desde 8.1
Confira Também
DllImportAttribute
System.Reflection.Emit
Namespace System.Runtime.InteropServices
Retornar ao início