CallingConvention 枚举

指定调用在非托管代码中实现的方法所需的调用约定。

**命名空间:**System.Runtime.InteropServices
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Enumeration CallingConvention
用法
Dim instance As CallingConvention
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public enum CallingConvention
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public enum class CallingConvention
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public enum CallingConvention
SerializableAttribute 
ComVisibleAttribute(true) 
public enum CallingConvention

成员

  成员名称 说明
Cdecl 调用方清理堆栈。这使您能够调用具有 varargs 的函数(如 Printf),使之可用于接受可变数目的参数的方法。 
FastCall 不支持此调用约定。 
StdCall 被调用方清理堆栈。这是使用平台 invoke 调用非托管函数的默认约定。 
ThisCall 第一个参数是 this 指针,它存储在寄存器 ECX 中。其他参数被推送到堆栈上。此调用约定用于对从非托管 DLL 导出的类调用方法。 
由 .NET Compact Framework 支持 Winapi 此成员实际上不是调用约定,而是使用了默认平台调用约定。例如,在 Windows 上默认为 StdCall,在 Windows CE.NET 上默认为 Cdecl。 

备注

请始终使用 CallingConvention 枚举,而不是 CALLCONV 枚举在托管代码中指定一个调用约定。后者仅作 COM 定义之用。DllImportAttributeSystem.Reflection.Emit 中的若干类使用 CallingConvention 枚举,以动态发出平台调用签名。

示例

下面的示例演示了如何应用 Cdecl 调用约定,因为由调用方负责清理堆栈,所以必须使用此调用约定。

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", CallingConvention := CallingConvention.Cdecl)> _
Overloads Shared Function printf ( _
    format As String, i As Integer, d As Double) As Integer
End Function

<DllImport("msvcrt.dll", CallingConvention := CallingConvention.Cdecl)> _
Overloads Shared Function printf ( _
    format As String, i As Integer, 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 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.Ansi, CallingConvention=CallingConvention.Cdecl)]
public static extern int printf(String format, int i, double d); 

[DllImport("msvcrt.dll", CharSet=CharSet.Ansi, 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");
    }
}
using namespace System;
using namespace System::Runtime::InteropServices;
public ref class LibWrap
{
public:

   // 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::Ansi,CallingConvention=CallingConvention::Cdecl)]
   static int printf( String^ format, int i, double d );

   [DllImport("msvcrt.dll",CharSet=CharSet::Ansi,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" );
}
import System.*;
import System.Runtime.InteropServices.*;

public class LibWrap
{
    // VJ# 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]... )
    /** @attribute DllImport("msvcrt.dll", CharSet = CharSet.Ansi, 
        CallingConvention = CallingConvention.Cdecl)
     */
    public static native int printf(String format, int i, double d);

    /** @attribute DllImport("msvcrt.dll", CharSet = CharSet.Ansi, 
        CallingConvention = CallingConvention.Cdecl)
     */
    public static native int printf(String format, int i, String s);
} //LibWrap

public class App
{
    public static void main(String[] args)
    {
        LibWrap.printf("\nPrint params: %i %f", 99, 99.99);
        LibWrap.printf("\nPrint params: %i %s", 99, "abcd");
    } //main
} //App

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

System.Runtime.InteropServices 命名空间
DllImportAttribute
System.Reflection.Emit