CallingConvention 枚举
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定调用在非托管代码中实现的方法所需的调用约定。
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
- 继承
- 属性
字段
Cdecl | 2 | 调用方清理堆栈。 这使你能够调用具有 |
FastCall | 5 | 不支持此调用约定。 |
StdCall | 3 | 被调用方清理堆栈。 |
ThisCall | 4 | 第一个参数是 |
Winapi | 1 | 此成员实际上不是调用约定,而是使用 默认的平台调用约定。 |
示例
以下示例演示如何应用 Cdecl
调用约定,由于堆栈由调用方清理,因此必须使用该约定。
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
注解
始终使用 CallingConvention 枚举而不是 CALLCONV 枚举在托管代码中指定调用约定。 后者仅用于 COM 定义。 枚举 CallingConvention 由 DllImportAttribute 和 中的 System.Reflection.Emit 多个类用于动态发出平台调用签名。