英語で読む

次の方法で共有


CallingConvention 列挙型

定義

アンマネージ コードで実装されたメソッドを呼び出すために必要な呼び出し規約を指定します。

C#
public enum CallingConvention
C#
[System.Serializable]
public enum CallingConvention
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum CallingConvention
継承
CallingConvention
属性

フィールド

名前 説明
Cdecl 2

呼び出し元がスタックを消去します。 これを使用すると、varargs で関数を呼び出すことができます。Printf など、受け取るパラメーターの数が可変のメソッドで使用します。

FastCall 5

この呼び出し規約はサポートされていません。

StdCall 3

呼び出し先がスタックを消去します。

ThisCall 4

最初のパラメーターは this ポインターで、レジスタ ECX に格納されます。 その他のパラメーターは、スタックにプッシュされます。 この呼び出し規約は、アンマネージ DLL からエクスポートしたクラスのメソッドを呼び出すために使用します。

Winapi 1

このメンバーは実際には呼び出し規約ではなく、 代わりに既定のプラットフォーム呼び出し規則を使用します。

次の例では、呼び出し規約を Cdecl 適用する方法を示します。これは、スタックが呼び出し元によってクリーンアップされるために使用する必要があります。

C#
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");
    }
}

注釈

マネージ コードで呼び出し規約を CallingConvention 指定するには、 CALLCONV 列挙ではなく、常に 列挙体を使用します。 後者は、COM 定義のためにのみ存在します。 列挙はCallingConvention、 および のいくつかのクラスSystem.Reflection.Emitによって使用されDllImportAttribute、プラットフォーム呼び出しシグネチャを動的に出力します。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

こちらもご覧ください