共用方式為


CallingConvention 列舉

定義

指定呼叫 Unmanaged 程式碼中實作之方法所需的呼叫慣例。

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
繼承
CallingConvention
屬性

欄位

Cdecl 2

呼叫端會清除堆疊。 這會啟用有 varargs 的呼叫函式,就可用於接受各種數目參數的方法,如 Printf

FastCall 5

不支援這個呼叫慣例。

StdCall 3

被呼叫端會清除堆疊。

ThisCall 4

第一個參數為 this 指標且儲存在 register ECX 中。 其他參數會被推入至堆疊。 這個呼叫慣例是用來呼叫從 Unmanaged DLL 匯出之類別上的方法。

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 列舉,在 Managed 程式代碼中指定呼叫慣例。 後者僅適用於 COM 定義。 列舉 CallingConvention 是由 DllImportAttribute 和中的 System.Reflection.Emit 數個類別用來動態發出平臺叫用簽章。

適用於

另請參閱